简体   繁体   中英

PostgreSQL Inheritance naming issue in Spring boot

I am working on jpa with PostgreSQL, and I got an error that seems very odd. I have the example ddl below. The child tables share the same name with two different data types, and they are inherited with the parent table.

CREATE TABLE Parent
(
    id  VARCHAR(20) NOT NULL
)

CREATE TABLE child1
(
    test       double precision
)
INHERITS (Parent);


CREATE TABLE child2
(
    test       boolean
)
INHERITS (Parent);

For Entity

@Entity
@Table(
    name = "parent",
)
@NamedQuery(
    name = "parent",
    query = "SELECT p FROM Parent p"
)
@Inheritance(
    strategy = InheritanceType.TABLE_PER_CLASS
)
@Data
@NoArgsConstructor
public class Parent implements Serializable {
   @Id
   @NotNull
   @Column(name = "id")
   String id;
}

Child table

@Entity
@Table(
    name = "child1"
)
@NamedQuery(
    name = "child1",
    query = "SELECT c FROM Child1 c"
)
@Data
@NoArgsConstructor
public class Child1 extend Parent {
    @Column(name = test)
    private Double test;
}

Child2 table

@Entity
@Table(
    name = "child2"
)
@NamedQuery(
    name = "child2",
    query = "SELECT c FROM Child2 c"
)
@Data
@NoArgsConstructor
public class Child2 extend Parent {
    @Column(name = test)
    private Boolean test;
}

When I use controller to create a new record on parent entity, it will return error like

ERROR: UNION types double precision and boolean cannot be matched

I know jpa are using union to find the matching result which causing this error. For Joined and SingleTable, they are not working in here since I want the child can have the parent's attributes. Parent will have its own. The table per class was the best choice for me. I am wondering any other way to fix this issue?

Your Child1 and Child2 classes both define an entity attribute with the name test which gets mapped to a database column with the same name. Hibernate can't handle that when you select from both tables in a polymorphic query. You need to change the column name on one of the tables so that they don't collide in the UNION.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM