I am new to Java EE, and I set up a learning case for myself where I can register users and persist the data to the database, I use Glassfish, EclipseLink JPA 2.0 and JSF framework. The database contains three tables: user(id[pk],name,gender,...), hobby(id[pk],hobby_name), and user_hobby which describes the many-to-many relationship and contains only user_id and hobby_id.
in the User.java entity class, I declare the relationship as such:
...
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(
name="user_hobby",
joinColumns=@JoinColumn(name="user_id"),
inverseJoinColumns=@JoinColumn(name="hobby_id")
)
private List<Hobby> hobbies;
public List<Hobby> getHobbies() {
return hobbies;
}
public void setHobbies(List<Hobby> hobbies) {
this.hobbies = hobbies;
}
...
and in the Hobby.java class, I declare bidirection as such:
...
@ManyToMany(mappedBy="hobbies",fetch=FetchType.EAGER)
private List<User> users;
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
...
In the JSF view page, I output all the hobbies from the hobby table which contain pre-inserted values like such:
<h:selectManyListbox id ="hobb" label="Hobbies" value="#{userController.user.hobbies}" >
<f:selectItems value="#{itemController.hobbyItems}"/>
</h:selectManyListbox>
and corresponding itemcontroller has this method:
public List<SelectItem> getHobbyItems(){
List<SelectItem> items=new ArrayList<SelectItem>();
//fetch all hobbies from database
List<Hobby> hobbies=hobbyFacade.findAll();
for(Hobby h:hobbies)items.add(new SelectItem(h,h.getHobby()));
return items;
}
Here you can see that I inject a list of hobby objects into the view and let user.hobbies get hold on it.
When I create users, if I don't select hobbies, then the creation succeeds and data is persisted to the database, but if I select any hobby, the server throws a null pointer exception, which I couldn't figure out why, I have spend almost a day to debug this, but could not make any progress. Could anybody maybe help me with this issue, I would much appreciate it. If you need any more information about settings, please let me know.
Update exception(first three lines):
WARNING: java.lang.NullPointerException
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.checkForUnregisteredExistingObject(UnitOfWorkImpl.java:745)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.discoverAndPersistUnregisteredNewObjects(Unit开发者_如何学PythonOfWorkImpl.java:4124)
after a long struggling with this issues, I finally solved the problem. the problem was about conversion between string in view and object in backing bean. I used mapping solution which is one of the methods proposed in this post: http://balusc.blogspot.com/2007/09/objects-in-hselectonemenu.html. thanks.
Please include your code to create users that is failing and include the full exception stack trace.
Are you adding a null value to a list?
Ensure that you are using the managed versions of the objects and merging your objects correctly.
精彩评论