用户(User)与角色(Role)多对多关系(UserRole)
下面是表的代码(配置为annotation)
User:
package sub;
import java.io.Serializable;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
@Entity
public class User implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 1L;
private Long id;
private Set<Role> roles;
@Id
@GeneratedValue
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
@ManyToMany
@JoinTable(name="UserRole",joinColumns={@JoinColumn(name="user_ID")},inverseJoinColumns={@JoinColumn(name="role_ID")})
public Set<Role> getRoles()
{
return roles;
}
public void setRoles(Set<Role> roles)
{
this.roles = roles;
}
}
Role:
package sub;
import java.io.Serializable;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
@Entity
public class Role implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 1L;
private Long id;
private Set<User> users;
@Id
@GeneratedValue
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
@ManyToMany
@JoinTable(name="UserRole",joinColumns={@JoinColumn(name="role_ID")},inverseJoinColumns={@JoinColumn(name="user_ID")})
public Set<User> getUsers()
{
return users;
}
public void setUsers(Set<User> users)
{
this.users = users;
}
}
UserRoleId:
package sub;
import java.io.Serializable;
import javax.persistence.Embeddable;
@Embeddable
public class UserRoleId implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 1L;
private Long userId;
private Long roleId;
public UserRoleId(Long userId, Long roleId)
{
this.userId = userId;
this.roleId = roleId;
}
public UserRoleId()
{
this(null,null);
}
@Override
public boolean equals(Object obj)
{
if(obj == this)return true;
if(obj instanceof UserRoleId)
{
final UserRoleId other = (UserRoleId) obj;
Long userT = getUserId();
Long userOther = other.getUserId();
Long roleT = getRoleId();
Long roleOther = other.getRoleId();
return ((userT == userOther) || (userT != null &&
userOther != null && userT.equals(userOther))) &&
((roleT == roleOther) || (roleT != null &&
roleOther != null && roleT.equals(
roleOther)));
}
return false;
}
@Override
public int hashCode()
{
int re = 17;
re += (userId == null?0:userId.hashCode());
re += (roleId == null?0:roleId.hashCode());
return 37*re;
}
public Long getUserId()
{
return userId;
}
public void setUserId(Long userId)
{
this.userId = userId;
}
public Long getRoleId()
{
return roleId;
}
public void setRoleId(Long roleId)
{
this.roleId = roleId;
}
}
UserRole:
package sub;
import java.io.Serializable;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
public class UserRole implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 1L;
private UserRoleId id;
private Role role;
private User user;
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name = "userId", column = @Column(name = "user_ID", unique = false, nullable = false, insertable = true, updatable = true)),
@AttributeOverride(name = "roleId", column = @Column(name = "role_ID", unique = false, nullable = false, insertable = true, updatable = true)) })
public UserRoleId getId()
{
return id;
}
@ManyToOne(cascade = {})
@JoinColumn(name = "role_ID", unique = false, nullable = false, insertable = false, updatable = false)
public Role getRole()
{
return role;
}
@ManyToOne(cascade = {}, fetch = FetchType.EAGER)
@JoinColumn(name = "user_ID", unique = false, nullable = false, insertable = false, updatable = false)
public User getUser()
{
return user;
}
public void setId(UserRoleId id)
{
this.id = id;
}
public void setRole(Role role)
{
this.role = role;
}
public void setUser(User user)
{
this.user = user;
}
}
在mysql数据库中,保存UserRole时出错。而完全相同的代码在oracle数据中正确。
操作如下:
User user;
Role role;
//经过多步操作以后,user与role都保存到数据库,与之相关联的session已关闭
Session s = null;//取得s的代码没有写出来
s.beginTransaction();
UserRole userRole = new UserRole();
userRole.setId(new UserRoleId(user.getId(),role.getId()));
userRole.setUser(user);
userRole.setRole(role);
s.persist(userRole);
s.getTransaction().commit();
//一提交就出错,提示user没有保存,但是奇怪的是为什么在oracle中完全正确。
//最后在mysql中作如下的修改可以正确保存
//把 userRole.setUser(user)与userRole.setRole(role)删除
有那位知道是什么问题,请告之