Hello,
I have a problem with @Any annotations which i can't find an adequate solution to.
Let's say i have a Bus class (it is a part of a more complex structure, hence @Discriminator, but it works, so i won't post other Entitites):
Code:
@Entity
@DiscriminatorValue("bus")
public class Bus extends AbstractCar implements Car {
@Column(name = "engine_capacity")
private String engineCapacity;
@Any(metaColumn = @Column(name = "engine_type"))
@AnyMetaDef(idType = "big_integer", metaType = "string",
metaValues = {
@MetaValue(targetEntity = Engine.class, value = "firsttype"),
@MetaValue(targetEntity = EngineType2.class, value = "secondtype")
})
@Cascade({CascadeType.ALL})
@JoinColumn(name = "engine_id")
private EngineInterface engine;
And there are two types of engines:
Code:
@Entity
@Table(name = "engine")
public class Engine implements EngineInterface{
@Id
@Column(name = "engine_id")
private BigInteger id;
@Column(name = "model_no")
private String modelNo;
Quote:
@Entity
@Table(name = "engine_other")
public class EngineType2 implements EngineInterface{
@Id
@Column(name = "engine_id")
private BigInteger id;
@Column(name = "model_num")
private String modelNo;
Ofc it's all mapped in hbm.xlm properly, no issues with persist whatsoever and even gets work fine for individual entities.
The problem is that i cannot fetch Bus with Engine containing anything but a set of null fields.
I've tried using simple EntityManager.find(Bus.class, id), i tried using a native query with join (hibernate does not allow joins for @Any mapped entities), i tried unwrapping EntityManager for a session and doing it all manually. The only working solution i've found is to first initialize all of the Engine and EngineType2 entities with separate query, but it's too much of overhead for my liking:
Quote:
public Bus getBus(BigInteger carId) {
entityManager.createQuery("select u from Engine u").getResultList();
entityManager.createQuery("select u from EngineType2 u").getResultList();
Bus result = entityManager.find(Bus.class, carId);
return result;
}
Is there something i'm missing?