问题
I'm using JPA with Java EE 5 (IBM RAD 7.5 and WebSphere 7). I've simplified my problem to the following example ...
I have a Project and a User. The tables look like this:
PROJECT
--
id
name
projectmanagerid // is a User
USER
--
id
username
My classes look like this:
@Entity
@Table(name="PROJECT")
class Project
@Id
@GeneratedValue(...)
Long id;
String name;
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="projectmanagerid");
User projectManager;
@Entity
@Table(name="USER")
class User
@Id
@GeneratedValue(...)
Long id;
String username;
To retrieve my Project I do this:
Query query = em.createQuery("select distinct p from Project p");
query.getResult...
Everything works except the project.projectManager
is always null
.
Shouldn't FetchType.EAGER
cause the user to get populated? What am I doing wrong?
Any suggestions are greatly appreciated!
Rob
回答1:
In the case you should just be able to map this with the @Column annotation as join column is for instance where the reference is on the other side of the relationship
Project -> (has a) User then Project has @Column with name="projectmanagerid"
if on the other had user referenced project then you would have
User -> (has a) Project then Project has @JoinColumn with name="projectid"
Presently you are stating the the user table has a reference column called projectmanagerid which is not the case.
I hope this helps, TomRed
回答2:
It's looks like I found the answer to my problem so I'll post it here. Hopefully it might help others.
In IBM RAD 7.5, the JPA project properties has a setting called "Platform". The default value is "RAD JPA Platform". When I changed it "Generic" and now it works.
Note: I also changed @JoinColum
to @Column
as suggested by tomred, not sure if that did anything, tho.
Thanks all!
Rob
来源:https://stackoverflow.com/questions/10609334/why-isnt-this-simple-jpa-manytoone-relationship-working