从jpa到hibernate的单表继承的java迁移问题

ogsagwnx  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(295)

从标准jpa(eclipselink)迁移到hibernate时,由于子记录的数目不正确和/或子对象的类型无效,测试失败。我认为问题是hibernate生成的sql没有在inheritancetype.single表关系的where子句中使用鉴别器列。
对Map中的更改有什么建议吗?
提前谢谢,
提摩太
环境
博士后9.6.17
Spring Boot/jpa 2.4.0
postgres mvn依赖42.2.18
域背景
直流电动机最多可有2组导线。在实体中表示,每个实体都有自己的列表。这些导线是arm或eq类型。因为导线上的属性是相同的,所以dba选择了一个表,其中的鉴别器列名为coil\u type,值为arm或eq。
类别定义
父实体

@Entity 
@Table(name = "dc_motor")
public class DcMotor implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Version
    private Integer version;
  //other direct fields

    @OneToMany(orphanRemoval = true, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "motor_id", referencedColumnName = "id", nullable = false)
    @OrderColumn(name = "idx")
    private List<WireArm> armWires;

    @OneToMany(orphanRemoval = true, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "motor_id", referencedColumnName = "id", nullable = false)
    @OrderColumn(name = "idx")
    private List<WireEq> eqWires;

wirearm和wireeq的抽象基类

@Entity 
@Table(name = "dc_wire")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "coil_type", length = 10, discriminatorType = DiscriminatorType.STRING)
public abstract class DcWire implements IDcWire {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    // other direct fields
    // discriminator column coil_type is not explicitly listed as a field

混凝土儿童班

@Entity
@DiscriminatorValue(value = "ARM")
public class WireArm extends DcWire implements IDcWire {
    // just constructors
}

@Entity
@DiscriminatorValue(value = "EQ")
public class WireEq extends DcWire implements IDcWire {
    // just constructors
}

sql生成

select /*all fields*/ from dc_motor dcmotor0_ where dcmotor0_.id=1;
select /* all fields EXCEPT coil_type*/ from dc_wire armwires0_ where armwires0_.motor_id=1;
select /* all fields EXCEPT coil_type*/ from dc_wire eqwires0_ where eqwires0_.motor_id=1;
ehxuflar

ehxuflar1#

我认为这可以通过使用 org.hibernate.annotations.DiscriminatorOptions(force = true) .

相关问题