Java 如何使用hibernate和JBOSS工具在数据库中添加新记录?
我正在使用Eclipse和JBOSS工具从MySQL生成hibernate类。 工资表有复合键,一个是FK(用户ID),一个是自动递增。所以JBOSS工具生成了Salary和User类,它也生成了SalaryId类。 我已经在User类的GetPalaries方法中添加了cascade,但是当我试图保存新用户时,总是会出现异常: org.hibernate.exception.ConstraintViolationException:无法添加或更新子行:外键约束失败 有人能帮我解决这个问题吗?谢谢大家~ 以下是我的表格结构:Java 如何使用hibernate和JBOSS工具在数据库中添加新记录?,java,mysql,hibernate,pojo,jboss-tools,Java,Mysql,Hibernate,Pojo,Jboss Tools,我正在使用Eclipse和JBOSS工具从MySQL生成hibernate类。 工资表有复合键,一个是FK(用户ID),一个是自动递增。所以JBOSS工具生成了Salary和User类,它也生成了SalaryId类。 我已经在User类的GetPalaries方法中添加了cascade,但是当我试图保存新用户时,总是会出现异常: org.hibernate.exception.ConstraintViolationException:无法添加或更新子行:外键约束失败 有人能帮我解决这个问题吗?谢
CREATE TABLE IF NOT EXISTS `salary` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userId` int(11) NOT NULL,
`amount` int(11) NOT NULL,
`comingDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`,`userId`),
KEY `userId` (`userId`)
)
下面是我生成的类:
[用户类别]
@Entity
@Table(name = "user", catalog = "lab")
public class User implements java.io.Serializable {
private Integer id;
private String name;
private String password;
private String email;
private Set<Salary> salaries = new HashSet<Salary>(0);
public User() {
}
public User(String name, String password, String email, Set<Salary> salaries) {
this.name = name;
this.password = password;
this.email = email;
this.salaries = salaries;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "name", nullable = false, length = 100)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "password", nullable = false, length = 100)
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(name = "email", nullable = false, length = 50)
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "user", cascade = CascadeType.ALL)
public Set<Salary> getSalaries() {
return this.salaries;
}
public void setSalaries(Set<Salary> salaries) {
this.salaries = salaries;
}
}
以下是JBOSS工具自动生成的:
[工薪阶层]
@Embeddable
public class SalaryId implements java.io.Serializable {
private int id;
private int userId;
public SalaryId() {
}
public SalaryId(int id, int userId) {
this.id = id;
this.userId = userId;
}
@Column(name = "id", nullable = false)
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "userId", nullable = false)
public int getUserId() {
return this.userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof SalaryId))
return false;
SalaryId castOther = (SalaryId) other;
return (this.getId() == castOther.getId())
&& (this.getUserId() == castOther.getUserId());
}
public int hashCode() {
int result = 17;
result = 37 * result + this.getId();
result = 37 * result + this.getUserId();
return result;
}
}
[主要课程]
transaction = session.beginTransaction();
SalaryId salaryId = new SalaryId();
Set<Salary> salaries = new HashSet<Salary>();
User user = new User();
user.setName("JW");
user.setPassword("123456");
user.setEmail("jjj@jj.cc");
salaries.add(new Salary(salaryId, user, 10000));
user.setSalaries(salaries);
session.save(user);
transaction.commit();
transaction=session.beginTransaction();
SalaryId SalaryId=新的SalaryId();
Set salaries=新HashSet();
用户=新用户();
user.setName(“JW”);
user.setPassword(“123456”);
user.setEmail(“jjj@jj.cc");
添加(新薪资(salaryId,用户,10000));
用户设置日历(工资);
session.save(用户);
commit();
试着这样做:
transaction = session.beginTransaction();
SalaryId salaryId = new SalaryId();
Set<Salary> salaries = new HashSet<Salary>();
User user = new User();
user.setName("JW");
user.setPassword("123456");
user.setEmail("jjj@jj.cc");
session.save(user);
salaries.add(new Salary(salaryId, user, 10000));
//user.setSalaries(salaries);
transaction.commit();
transaction=session.beginTransaction();
SalaryId SalaryId=新的SalaryId();
Set salaries=新HashSet();
用户=新用户();
user.setName(“JW”);
user.setPassword(“123456”);
user.setEmail(“jjj@jj.cc");
session.save(用户);
添加(新薪资(salaryId,用户,10000));
//用户设置日历(工资);
commit();
这将只保存用户
记录,而不是它的工资,因为如果不将这些对象添加到集合中,hibernate不知道这些对象是否存在。
transaction = session.beginTransaction();
SalaryId salaryId = new SalaryId();
Set<Salary> salaries = new HashSet<Salary>();
User user = new User();
user.setName("JW");
user.setPassword("123456");
user.setEmail("jjj@jj.cc");
session.save(user);
salaries.add(new Salary(salaryId, user, 10000));
//user.setSalaries(salaries);
transaction.commit();