micronaut-projects / micronaut-projects/micronaut-data

Support join table inheritance in JDBC

Open
#105 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: validated type: enhancement
Dominant language
Java
Stars
482
Forks
229
Avg merge
1d 7h
Merged PRs (30d)
32

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Student and RegularStudent mappings and the RegularStudentRepository shown in the issue, then inspect DefaultJdbcRepositoryOperations.persist near line 411 and the generated SQL for save. Determine how joined inheritance should split persistence across the two tables, and add coverage for the repository default methods. Done means a RegularStudent can be persisted without writing inherited columns to regular_student.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, postgresql
Domain
backend, database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.