I read the book Java Persistence with Hibernate. In the section of Implementing POJO associations, it states that one can wrap business function by correlating fucntions between two domain objects.
For instance, in the Category - Item relations
Code:
public void addCategory(Category category) {
if (category == null)
throw new IllegalArgumentException("Null category");
category.getItems().add(this);
categories.add(category);
}
It is useful because these kind of functions allow reuse and help ensure correctness.
Now I have a similar situation. With my two classes, which have many to many association, I would like to add method as book describes.
Following is the relation of the domain classes and its functions.
Classes :
Code:
User <--- Many to Many --> Group
User
Code:
@ManyToMany(
fetch=FetchType.EAGER
)
@JoinTable(name="USERS_GROUPS",
joinColumns=
@JoinColumn(name="USER_ID"),
inverseJoinColumns=
@JoinColumn(name="GROUP_ID")
)
private List<Group> groups = new ArrayList<Group>();
public User(String id, String account, String name, String password){
this.id = id;
this.account = account;
this.name = name;
this.password = password;
}
public String getId(){
return this.id;
}
// setter is removed
// public void setId(String id){
// this.id = id;
// }
public void addGroup(GroupName group){
boolean hasGroup = false;
for(Group g : this.groups){
if(g.getName().equals(group)){
hasGroup = true;
break;
}
}
if(!hasGroup) {
String groupId = KeyGenerator.generate();
Group g = new Group(groupId, group);
this.groups.add(g);
g.getUsers().add(this);
}
}
Group
Code:
public enum GroupName{
Root, User;
}
@ManyToMany(mappedBy="groups",fetch=FetchType.EAGER)
private List<User> users = new ArrayList<User>();
public Group(String id, GroupName name){
this.id = id;
this.name = name;
}
public String getId(){
return this.id;
}
// setter is removed
//public String getId(){
// return this.id;
//}
My scenario is that I want to create a user, assign with a group name (e.g. GroupName.User) and then save it to the database.
The ideal code snippet from DAO class looks like
Code:
public String createDefaultUser(String account, String name, String password, Gender gender){
manager.getTransaction().begin();
String id = KeyGenerator.generate();
User user = new User(id, account, name, password);
user.setGender(gender);
user.addGroup(GroupName.User);
merge(user);
manager.flush();
manager.getTransaction().commit();
return id;
}
But this would throw error like
Code:
javax.persistence.EntityNotFoundException: Unable to find example.Group with id d8d8bff1-0037-492b-9f28-01476e7d6487
This can be solved by the following workaround in the DAO layer.
Code:
public String createDefaultUser(String account, String name, String password, Gender gender){
manager.getTransaction().begin();
String id = KeyGenerator.generate();
User user = new User(id, account, name, password);
user.setGender(gender);
List<Group> groups = super.manager.createQuery(" from Group ").getResultList();
if(null!=groups){
boolean hasGroup = false;
Group group = null;
for(Group g: groups){
if(g.getName().equals(GroupName.User)){
hasGroup = true;
group = g;
break;
}
}
String gid = KeyGenerator.generate();
if(!hasGroup){
List<Group> nglist = new ArrayList<Group>();
nglist.add(new Group(gid, GroupName.User));
user.setGroups(nglist);
}else{
List<Group> oglist = user.getGroups();
oglist.add(group);
user.setGroups(oglist);
}
}
merge(user);
manager.flush();
manager.getTransaction().commit();
return id;
This becomes the problem because I need to manage the code outside the domain object. However, when I try to move these code to User class. I notice that in User class, there is no way to obtain EntityManager, which is used to retrieve Group from database.
Is there any better way to get it worked as the book described?
I appreciate any suggestion.
Thank you very much.