我拥有实体中有2个不同子实体的集合:
@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;
}
我希望这两个集合由相应子类的实例填充(2个前灯和4个轮子):
@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());
}
相反,第一个集合包含6个前灯(部分填充),第二个集合包含不正确的数据:
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)
输出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=?
我期望鉴别器列也包含在WHERE子句中:
AND headlights0_.type = 'HEADLIGHT'
,但它不存在。
我只能使用Hibernate @Where(子句=&#34; type =&#39; WHEEL&#39;)注释解决此问题。
为什么在这种情况下Hibernate无法正常工作?我希望它具有所有必要的元数据信息,以便能够发出正确的SQL。
答案 0 :(得分:0)
您可能想尝试一下 抽象类上的@DiscriminatorOptions(force = true)&#34; Part&#34;。
另请参阅:About the use of @ForceDiscriminator/@DiscriminatorOptions(force=true)