简体   繁体   English

Spring 引导中的 PostgreSQL Inheritance 命名问题

[英]PostgreSQL Inheritance naming issue in Spring boot

I am working on jpa with PostgreSQL, and I got an error that seems very odd.我正在使用 PostgreSQL 研究 jpa,我遇到了一个看起来很奇怪的错误。 I have the example ddl below.我有下面的示例 ddl。 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 Child2 表

@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当我使用 controller 在父实体上创建新记录时,它会返回如下错误

ERROR: UNION types double precision and boolean cannot be matched错误:UNION 类型双精度和 boolean 无法匹配

I know jpa are using union to find the matching result which causing this error.我知道 jpa 正在使用联合查找导致此错误的匹配结果。 For Joined and SingleTable, they are not working in here since I want the child can have the parent's attributes.对于 Joined 和 SingleTable,它们不在这里工作,因为我希望孩子可以拥有父母的属性。 Parent will have its own.父母会有自己的。 The table per class was the best choice for me.每个 class 的表对我来说是最佳选择。 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.您的Child1Child2类都定义了一个名为test的实体属性,该属性映射到具有相同名称的数据库列。 Hibernate can't handle that when you select from both tables in a polymorphic query.当您在多态查询中从两个表中获取 select 时,Hibernate 无法处理该问题。 You need to change the column name on one of the tables so that they don't collide in the UNION.您需要更改其中一张表上的列名,以免它们在 UNION 中发生冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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