持久的新实体不会加入JoinTable

时间:2013-06-07 11:25:23

标签: hibernate jpa persistence jointable

我有两个实体,一个叫做UserProfile,另一个叫Group。他们有以下@OneToMany@ManyToOne注释:

UserProfile实体类:

@ManyToOne
private Groups usersGroup; 

群组实体类:

@ElementCollection
@OneToMany
@JoinTable(name = "USER_GROUPS", 
           joinColumns = @JoinColumn(name = "GROUP_NAME", referencedColumnName = "GROUP_NAME"), 
           inverseJoinColumns = @JoinColumn(name = "USER_NAME", referencedColumnName = "USER_NAME"))
private Collection<UserProfile> user;

所以我现在有三个不同的表:USERPROFILE,GROUPS和USER_GROUPS(joinTable)。我有servlet持续发生。它有以下代码:

//Creating new User entity
UserProfile userProfile = new UserProfile();

//Instantiating Dao class of Groups, which have general persisting methods like
//persist, find, merge, remove and findByGroupName
GroupsDao userGroup = new GroupsDaoImpl();

//Instantiating Dao class of UserProfile, have same general methods like above but
//also findByUsername and findAllUsers (not admins)
UserProfileDao userDao = new UserProfileDaoImpl();

//Instantiating Group called users and its value is getting all users which belongs to
//USERS group.
Groups users = userGroup.findByGroupName("USERS").iterator().next();

//Setting required information for user entity
userProfile.setFirstName(request.getParameter("firstName"));
userProfile.setLastName(request.getParameter("lastName"));
userProfile.setUserName(request.getParameter("userName"));
userProfile.setPassword(request.getParameter("userPassword"));
userProfile.setEmail(request.getParameter("userEmail"));
userProfile.setUserType("USER");
userProfile.setUserGroup(users);

//Setting userProfile to users group. Because user variable is collection of
//UserProfile, that is why a collection must be created and add user to this list.
//Finally in users.setUser(userList) is done to set the new list for groups variable
//of users. (This is not definitely best way to do it) 
Collection<UserProfile> userList = new ArrayList<UserProfile>();
userList.add(userProfile);
users.setUser(userList);

//Persisting both entities.
userDao.save(user);
userGroup.save(users);

使用userAction.save(user)保持用户正常工作。它会将新用户持久保存到USERPROFILE表中,但是当我尝试使用userGroup.save(users);将用户添加到用户组时,它实际上会使用旧用户组重写新用户组,因此不会在USER_GROUPS join中添加任何组元素表。我怎么能解决这个问题?

我想加入这样的表:

|UserName| |Group|
------------------
| userA |  |USERS|
| userB |  |USERS|
| userC |  |ADMINS|

DAO的代码如下:

GroupsDaoImpl:

public GroupsDaoImpl(){
super(Groups.class);
}


@Override
public Collection<Groups> findByGroupName(String groupName) {
EntityManager em = getEntityManager();
Query q = em.createQuery("SELECT g FROM Groups g WHERE g.groupName = :gN");
q.setParameter("gN", groupName);
List<Groups> results = q.getResultList();
return results;
}

UserProfileDaoImpl:

public UserProfileDaoImpl(){
super(UserProfile.class);
}

@Override
public Collection<UserProfile> findByEmail(String userEmail) {
EntityManager em = getEntityManager();
Query q = em.createQuery("SELECT u FROM UserProfile u WHERE u.email = :uE");
q.setParameter("uE", userEmail);
List<UserProfile> results = q.getResultList();
return results;
}

@Override
public Collection<UserProfile> findByUsername(String userName){
EntityManager em = getEntityManager();
Query q = em.createQuery("SELECT u FROM UserProfile u WHERE u.userName = :uN");
q.setParameter("uN", userName);
List<UserProfile> results = q.getResultList();
return results;<
}

@Override
public Collection<UserProfile> findAllUsers() {
EntityManager em = getEntityManager();
Query q = em.createQuery("SELECT u FROM UserProfile u WHERE u.userType = 'USER'");
List<UserProfile> results = q.getResultList();
return results;
}

0 个答案:

没有答案