我有一个简单的User和Roles实体,具有一对多映射。一个具有多个角色的用户。我尝试保存用户实体时收到错误。我在调用persist之前设置了Role对象User。
以下是实体的快照。
用户:
package com.petpe.ejbadmin.entity;
import java.io.Serializable;
import java.util.Date;
import java.util.Set;
import javax.annotation.Generated;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
@Entity
@Table(name = "users")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "User.findAll", query = "SELECT u FROM User u"),
@NamedQuery(name = "User.findByUsernameandPassword", query = "SELECT u FROM User u WHERE u.username = :username AND u.password = :password"),
@NamedQuery(name = "User.findByUserfirstname", query = "SELECT u FROM User u WHERE u.userfirstname = :userfirstname"),
@NamedQuery(name = "User.findByUserlastname", query = "SELECT u FROM User u WHERE u.userlastname = :userlastname"),
@NamedQuery(name = "User.findByPhonenumber", query = "SELECT u FROM User u WHERE u.phonenumber = :phonenumber"),
@NamedQuery(name = "User.findByEmail", query = "SELECT u FROM User u WHERE u.email = :email"),
@NamedQuery(name = "User.findByErpid", query = "SELECT u FROM User u WHERE u.erpid = :erpid"),
@NamedQuery(name = "User.findByBuyerid", query = "SELECT u FROM User u WHERE u.buyerid = :buyerid"),
@NamedQuery(name = "User.findByCreatedDate", query = "SELECT u FROM User u WHERE u.createdDate = :createdDate"),
@NamedQuery(name= "User.findByfirstletter",query="SELECT u FROM User u join fetch u.RoleSet ur WHERE LOWER(u.userfirstname) LIKE :firstname"),
@NamedQuery(name= "User.findallusers",query="From User u join fetch u.RoleSet ur")})
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Basic(optional = true)
@Column(name = "USER_ID")
private Integer userId;
@Size(max = 120)
@Column(name = "USERNAME")
private String username;
@Size(max = 60)
@Column(name = "USERFIRSTNAME")
private String userfirstname;
@Size(max = 60)
@Column(name = "USERLASTNAME")
private String userlastname;
@Size(max = 50)
@Column(name = "PASSWORD")
private String password;
@Size(max = 20)
@Column(name = "PHONENUMBER")
private String phonenumber;
// @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation
@Size(max = 50)
@Column(name = "EMAIL")
private String email;
@Size(max = 250)
@Column(name = "ERPID")
private String erpid;
@Size(max = 250)
@Column(name = "BUYERID")
private String buyerid;
@Basic(optional = false)
@NotNull
@Column(name = "CREATED_DATE")
@Temporal(TemporalType.DATE)
private Date createdDate;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "user", fetch = FetchType.LAZY)
private Set<Role> RoleSet;
public User() {
}
public User(Integer userId) {
this.userId = userId;
}
public User(Integer userId, Date createdDate) {
this.userId = userId;
this.createdDate = createdDate;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserfirstname() {
return userfirstname;
}
public void setUserfirstname(String userfirstname) {
this.userfirstname = userfirstname;
}
public String getUserlastname() {
return userlastname;
}
public void setUserlastname(String userlastname) {
this.userlastname = userlastname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhonenumber() {
return phonenumber;
}
public void setPhonenumber(String phonenumber) {
this.phonenumber = phonenumber;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getErpid() {
return erpid;
}
public void setErpid(String erpid) {
this.erpid = erpid;
}
public String getBuyerid() {
return buyerid;
}
public void setBuyerid(String buyerid) {
this.buyerid = buyerid;
}
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
@XmlTransient
public Set<Role> getRoleSet() {
return RoleSet;
}
public void setRoleSet(Set<Role> RoleSet) {
this.RoleSet = RoleSet;
}
@Override
public int hashCode() {
int hash = 0;
hash += (userId != null ? userId.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof User)) {
return false;
}
User other = (User) object;
if ((this.userId == null && other.userId != null) || (this.userId != null && !this.userId.equals(other.userId))) {
return false;
}
return true;
}
@Override
public String toString() {
return "[ userId=" + userId + " ]";
}
}
作用:
/ * *要更改此许可证标题,请在“项目属性”中选择“许可证标题”。 *要更改此模板文件,请选择“工具”|模板 *并在编辑器中打开模板。 * /
package com.petpe.ejbadmin.entity;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
@Entity
@Table(name = "user_roles")
@XmlRootElement
public class Role implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "ROLEID")
private Integer roleid;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 30)
@Column(name = "ROLENAME")
private String rolename;
@JoinColumn(name = "USER_ID", referencedColumnName = "USER_ID")
@ManyToOne( fetch = FetchType.LAZY)
private User user;
public Role() {
}
public Role(String rolename) {
this.rolename = rolename;
}
public Role(Integer roleid, String rolename) {
this.roleid = roleid;
this.rolename = rolename;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Integer getRoleid() {
return roleid;
}
public void setRoleid(Integer roleid) {
this.roleid = roleid;
}
public String getRolename() {
return rolename;
}
public void setRolename(String rolename) {
this.rolename = rolename;
}
@Override
public int hashCode() {
int hash = 0;
hash += (roleid != null ? roleid.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Role)) {
return false;
}
Role other = (Role) object;
if ((this.roleid == null && other.roleid != null) || (this.roleid != null && !this.roleid.equals(other.roleid))) {
return false;
}
return true;
}
@Override
public String toString() {
return "[ roleid=" + roleid + " ]";
}
}
错误:
1.Cannot insert the value NULL into column 'USER_ID', table 'pedb.pedb.user_roles'; column does not allow nulls. INSERT fails.
2.could not insert: [com.petpe.ejbadmin.entity.Role]
3.org.hibernate.exception.ConstraintViolationException: could not insert: [com.petpe.ejbadmin.entity.Role]
4.javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not insert: [com.petpe.ejbadmin.entity.Role]
设置和保留代码:
public boolean AddorModifyUser(UserAdminDTO udato) {
User usr=this.populateUserobj(udato);
pet_em.persist(usr);
return true;
}
public User populateUserobj(UserAdminDTO udto)
{
User usr=new User();
usr.setBuyerid(udto.getbuyerid());
usr.setCreatedDate(new Date());
usr.setEmail(udto.getEmail());
usr.setErpid(udto.geterpid());
usr.setPassword(udto.getPassword());
usr.setPhonenumber(udto.getPhoneNumber());
usr.setUserfirstname(udto.getFirstName());
usr.setUserlastname(udto.getLastName());
usr.setUsername(udto.getUserName());
for(Role r: udto.getRole())
{
Set<Role> RoleSet=new HashSet<>();
RoleSet.add(r);
usr.setRoleSet(RoleSet);
}
return usr;
}
表格布局:
{{3}}
答案 0 :(得分:3)
因此,您对角色表中的NOT NULL
有USER_ID
约束。当持久化(即使具有级联关系)时,外键(或实体关系)将不会自动设置自身。
最有可能在创建Role
时,您未设置User
,因此在保持时,提供程序不知道USER_ID
是什么,因此尝试将其设置为null在db中,这违反了db模式。
您需要做的就是为每个User
Role
Set<Role> RoleSet=new HashSet<>();
for(Role r: udto.getRole())
{
r.setUser(usr); // <------
RoleSet.add(r);
}
usr.setRoleSet(RoleSet);
另请注意,Set
是在循环外创建的
你还没有走出困境。以上将解决错误,但你还有另一个问题。它很微妙,肯定会导致很难找到错误。
您正在生成Role
个ID。
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer roleid;
因此,在创建角色时不要设置它
Set<Role> roles = new HashSet<>();
Role userRole = new Role("USER");
userRole.setUser(user);
roles.add(userRole);
Role coolRole = new Role("COOL_GUY");
coolRole.setUser(user);
roles.add(coolRole);
user.setRoleSet(roles);
看起来不是问题,但错误在hashCode
@Override
public int hashCode() {
int hash = 0;
hash += (roleid != null ? roleid.hashCode() : 0);
return hash;
}
hashCode
仅包含roleId
。但是你没有设置roleId
,因为它应该被生成。因此,当您将角色添加到Set
时,它们都具有相同的hashCode
,因此您最终只会将Role
添加到Set
,作为Set
中的对象1}}不能有相同的hashCode
。
要解决此问题,您只需更改hashCode
以包含rolename
。
在测试时,我只让Netbeans生成hashCode
和equals
。这是结果。
@Override
public int hashCode() {
int hash = 7;
hash = 31 * hash + Objects.hashCode(this.roleid);
hash = 31 * hash + Objects.hashCode(this.rolename);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Role other = (Role) obj;
if (!Objects.equals(this.roleid, other.roleid)) {
return false;
}
if (!Objects.equals(this.rolename, other.rolename)) {
return false;
}
return true;
}
这应该可以解决这个问题。