user1538543
user1538543

Reputation: 21

Why @OneToMany does not discriminate sub-entities in collections

I have 2 collections of different sub-entities in owning entity:

@Entity
@Table(name = "car")
public class Car {  
    @Id
    private Long id;

    @OneToMany(mappedBy = "owner", cascade = CascadeType.ALL, orphanRemoval = true, targetEntity = Headlight.class)
    private Collection<Headlight> headlights = new ArrayList<Headlight>();

    @OneToMany(mappedBy = "owner", cascade = CascadeType.ALL, orphanRemoval = true, targetEntity = Wheel.class)
    private Collection<Wheel> wheels = new ArrayList<Wheel>();      
}

@Entity
@Table(name = "part")
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING, length = 16)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class Part {    
    @Id
    private Long id;

    private String manufacturer;
}

@Entity
@DiscriminatorValue("HEADLIGHT")
public class Headlight extends Part {
    private Integer power;

    @ManyToOne
    @JoinColumn(name = "owner_id", referencedColumnName = "id")
    private Car owner;  
}

@Entity
@DiscriminatorValue("WHEEL")
public class Wheel extends Part {

    private Integer size;

    @ManyToOne
    @JoinColumn(name = "owner_id", referencedColumnName = "id")
    private Car owner;
}

I expect these two collections to be filled by instances of corresponding subclasses (2 headlights and 4 wheels):

@Test
public void testCar() {
    Car car = new Car();
    car.setId(1l);

    Headlight light1 = new Headlight();
    light1.setId(1l);
    light1.setManufacturer("Osram");
    light1.setPower(12);
    light1.setOwner(car);

    Headlight light2 = new Headlight();
    light2.setId(2l);
    light2.setManufacturer("Osram");
    light2.setPower(12);
    light2.setOwner(car);

    car.getHeadlights().add(light1);
    car.getHeadlights().add(light2);

    Wheel wheel1 = new Wheel();
    wheel1.setId(3l);
    wheel1.setManufacturer("Bridgestone");
    wheel1.setSize(16);
    wheel1.setOwner(car);

    Wheel wheel2 = new Wheel();
    wheel2.setId(4l);
    wheel2.setManufacturer("Bridgestone");
    wheel2.setSize(16);
    wheel2.setOwner(car);

    Wheel wheel3 = new Wheel();
    wheel3.setId(5l);
    wheel3.setManufacturer("Bridgestone");
    wheel3.setSize(16);
    wheel3.setOwner(car);

    Wheel wheel4 = new Wheel();
    wheel4.setId(6l);
    wheel4.setManufacturer("Bridgestone");
    wheel4.setSize(16);
    wheel4.setOwner(car);

    car.getWheels().add(wheel1);
    car.getWheels().add(wheel2);
    car.getWheels().add(wheel3);
    car.getWheels().add(wheel4);

    entityManager.persist(car);

    entityManager.flush();
    entityManager.clear();

    Car restoredCar = entityManager.find(Car.class, 1l);

    Assert.assertEquals(2, restoredCar.getHeadlights().size());
    Assert.assertEquals(4, restoredCar.getWheels().size());
}

Instead, first collection contains 6 headlights (partially filled) and second collection contains incorrect data:

org.hibernate.WrongClassException: Object with id: 1 was not of the specified subclass: com.commerzbank.tr.nonotc.repository.Wheel (loaded object was of wrong class class com.commerzbank.tr.nonotc.repository.Headlight)

Output SQL:

SELECT headlights0_.owner_id AS owner6_3_1_,
headlights0_.id            AS id2_7_1_,
headlights0_.id            AS id2_7_0_,
headlights0_.manufacturer  AS manufact3_7_0_,
headlights0_.owner_id      AS owner6_7_0_,
headlights0_.power         AS power4_7_0_
FROM part headlights0_
WHERE headlights0_.owner_id=?

I expected discriminator column to be included into WHERE clause as well:

AND headlights0_.type = 'HEADLIGHT'

,but it is not there.

I could only fix this issue using Hibernate @Where(clause = "type = 'WHEEL') annotation.

Why isn't Hibernate work correctly in this case? I expect it has all necessary metadata information to be able to issue correct SQL.

Upvotes: 1

Views: 1426

Answers (1)

Kirinya
Kirinya

Reputation: 245

You might want to try @DiscriminatorOptions(force = true) on your abstract class "Part".

See also: About the use of @ForceDiscriminator/@DiscriminatorOptions(force=true)

Upvotes: 2

Related Questions