Why isn't this simple JPA ManyToOne relationship working?

泪湿孤枕 提交于 2019-12-25 08:49:39

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!