-->
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.  [ 1 post ] 
Author Message
 Post subject: Strange @ManyToOne error messages.
PostPosted: Mon May 06, 2013 5:24 pm 
Newbie

Joined: Mon May 06, 2013 5:14 pm
Posts: 1
I now realize that having multiple @ManyToOne entries in a single class which refer to the same join table is a problem because hibernate won't know how to save the object properly.

Nevertheless, the error messages it gives are mystifying until I puzzled out exactly what was going on.

Here is a test query:

Code:
    @Test
    public void crazyTest() {
        Query q = em.createQuery("from CrazyProfile sp join fetch sp.bundle")
            .setMaxResults(1);
        q.getResultList();
    }


Here is the corresponding entity class:

Code:
    @Entity
    @Table(name = "moc_profile")
    public class CrazyProfile {
   
        @Id
        private Integer id;
   
        @ManyToOne()
        @JoinTable(name ="productionprofile",
            joinColumns=@JoinColumn(name="profile_id",referencedColumnName="id"),
            inverseJoinColumns=@JoinColumn(name="prodlet_id", referencedColumnName="id"))
        private Prodlet prodlet;
   
        @ManyToOne()
        @JoinTable(name ="productionprofile",
            joinColumns=@JoinColumn(name="profile_id",referencedColumnName="id"),
            inverseJoinColumns=@JoinColumn(name="bundle_id", referencedColumnName="id"))
        private Bundle bundle;
    }

If you execute the test it complains:

org.hibernate.QueryException: could not resolve property: bundle of: ...CrazyProfile

Now, go ahead and change the variable name from "bundle" to "zbundle" in the query and the class and run again. It works fine.

Now change the variable name in the class and query to "abundle" and see that you get the error message again.

Ultimately, if the variable sorts alphabatically before the name of the other variable "prodlet" then you will get the error message. If it sorts afterward it'll work fine.

Also, you can't refer to both variables in the same query because the alphabetically later one will be the only one that hibernate knows about. I'm guessing that hibernate alphabetically iterates over variables and puts them into a Map with the join table name as the key.

Like I said at the start, this is because hibernate doesn't support multiple ManyToOne variables in the same class which go to the same table. This is fine, but hibernate should really report a helpful error message like "error: multiple variables refer to the same join table" or something like that. It took a long time before we discovered why the code worked when the variable was named "xyzzy", but not when it was named "damnitWorkAlready".


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

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.