0

When migrating from standard JPA (EclipseLink) to hibernate, tests are failing due to improper number of child records and/or invalid types for child objects. I think the issue is the SQL generated by hibernate is not using the discriminator column in the where clause on an InheritanceType.SINGLE_TABLE relationship.

Any suggestions on what to change in the mappings?

Thanks in advance,

Timothy

Environment

  • postgres 9.6.17
  • spring boot / JPA 2.4.0
  • postgres mvn dependency 42.2.18

Domain background

A DcMotor may have up to 2 sets of wires. Represented in the entity each in its own List. These wires are of either ARM or EQ type. Since the attributes on the wires are the same, a single table was chosen by the DBA with a discriminator column named coil_type with values of either ARM or EQ.

Class definitions

Parent Entity

@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;

Abstract base class for WireArm and 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

Concrete child class

@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 generated

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;

1 Answer 1

1

I think this can be solved by using org.hibernate.annotations.DiscriminatorOptions(force = true).

Sign up to request clarification or add additional context in comments.

1 Comment

With your solution and after figuring out how to clear hibernate cache (stackoverflow.com/questions/17814372/…) all tests are green!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.