micronaut-projects / micronaut-data

Ahead of Time Data Repositories
Apache License 2.0
469 stars 198 forks source link

Support join table inheritance in JDBC #105

Open bruno-lopes opened 5 years ago

bruno-lopes commented 5 years ago

Micronaut Version: 1.2.0 Micronaut Data Version: 1.0.0-BUILD-SNAPSHOT Postgres Version: 9.5

I'm trying to use Micronaut Data JDBC, but I'm having trouble with inheritance.

I have two classes: Student and RegularStudent as following:

@Entity
@Data
@Table(name = "student", schema = "student")
//@Inheritance(strategy = InheritanceType.JOINED)
@Introspected
public class Student {

    @Id
    @GeneratedValue
    @Column(name = "student_id")
    Long id;

    @NotNull
    @Column(nullable = false, name = "active")
    private Boolean active;

}

and:

@Entity
@Data
@Table(name = "regular_student", schema = "student")
@Introspected
public class RegularStudent extends Student {
    @Temporal(TemporalType.DATE)
    @Column(name = "entrance_date")
    private
    Date entranceDate;

}

And the following:

@JdbcRepository(dialect = Dialect.POSTGRES)
public interface RegularStudentRepository extends CrudRepository<RegularStudent, Long> {

}

When I try to persist a regular student object:

regularStudentRepository.save(newRegularStudent);

I get the following error:

Caused by: org.postgresql.util.PSQLException: ERROR: column "active" of relation "regular_student" does not exist
  Position: 467
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2468)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2211)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:309)
    at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:446)
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:370)
    at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:149)
    at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:124)
    at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61)
    at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java)
    at io.micronaut.data.jdbc.operations.DefaultJdbcRepositoryOperations.lambda$persist$8(DefaultJdbcRepositoryOperations.java:411)

It occurs because I have two tables in database:

Student -> fields (student_id and active)

Regular Student -> student_id and entrance_date

Is/Will it possible to map this situation in Micronaut and use the JDBC Repository default methods?

graemerocher commented 5 years ago

This would require a join on the tables, we will consider it. Thanks for the feedback.

bruno-lopes commented 5 years ago

This would require a join on the tables, we will consider it. Thanks for the feedback.

This would be very nice :smile:. I'm currently migrating an old system to a new Micronaut API, and it is the only thing that I'm missing in Micronaut Data JDBC. For now, I'm using Micronaut Data JPA.

Thanks for the wonderful work in Micronaut and Micronaut Data.

el-duderino5 commented 3 years ago

Good day. Is there any work around or solution to this issue, @graemerocher ? How do we go about creating an inheritance between mapped entities?

dstepanov commented 3 years ago

There is no solution at this moment. I think the only way is to have a table per entity mapping.

bruno-lopes commented 2 years ago

I tried to create an abstract class (parent class), and create a table per entity (child class). But Micronaut does not map attributes of the parent class. Is there a way to do this?