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".