-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Cannot find a way to initialize fields mapped with @Any
PostPosted: Mon Jan 16, 2017 9:41 am 
Newbie

Joined: Tue Aug 02, 2016 7:51 am
Posts: 6
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?


Top
 Profile  
 
 Post subject: Re: Cannot find a way to initialize fields mapped with @Any
PostPosted: Mon Jan 16, 2017 12:46 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
Check out the @Any section in the User Guide. The examples are on GitHub, so you can validate them against your own.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.