gpt4 book ai didi

java - Spring-Hibernate : Insertion error, not-null 约束(一对多关系)

转载 作者:行者123 更新时间:2023-11-29 00:10:33 24 4
gpt4 key购买 nike

我已经做了一段时间了,但我找不到哪里出错了。我假设这是一些小的和/或简单的错误,第二双眼睛可能会很快发现它。

我正在构建一个由 Employees 和 Jobs 组成的 MySQL 数据库。一个 Employee 可以有多个 Job,但一个 Job 只能有一个 Employee。

我使用 Liquibase 来构建我的数据库。这是我的变更集:

<changeSet id="0001" author="mparker" context="base">
<comment>Creating Base Table</comment>
<createTable tableName="employees" >
<column name="employeeID" autoIncrement="true" type="int">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="firstname" type="varchar(50)">
<constraints nullable="false"/>
</column>
<column name="lastname" type="varchar(50)">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>
<changeSet id="0002" author="mparker">
<createTable tableName="jobs" >
<column name="jobID" autoIncrement="true" type="int">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="employer" type="varchar(50)">
<constraints nullable="false"/>
</column>
<column name="employeeID" type="int">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>

如您所见,我正在尝试创建两个表,其中每个条目都有自己的唯一标识符,当新条目添加到表中时该标识符会自动递增。这些列永远不应该为空,因为值应该只是前一个值 + 1。

以下是 Employee 和 Job 类的相关部分:

雇员.java:

@Entity
@Table(name = "employees")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "employeeID")
private Long id;

@Column(name = "firstname")
private String firstName;

@Column(name = "lastname")
private String lastName;
@OneToMany(mappedBy = "employee", cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
private List<Job> jobList = new ArrayList<Job>();

// getters and setters...
}

作业.java:

@Entity
@Table(name = "jobs")
public class Job {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "jobID")
private Long id;

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinColumn(name = "employeeID")
private Employee employee;

@Column(name = "employer")
private String employer;

// getters and setters...

}

以及用于保存员工的方法(使用 Autowiring 的 EntityManager):

public void saveEmployee(String firstname, String lastname, List<Job> jobs) {
Employee employee1 = new Employee();
employee1.setJobList(jobs);
employee1.setFirstName(firstname);
employee1.setLastName(lastname);
entityManager.persist(employee1);
}

下面是执行这个方法时抛出的异常:

[ERROR] 2014-08-14 11:33:51,561 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions - Column 'employeeID' cannot be null
[ERROR] 2014-08-14 11:33:51,603 com.sourceallies.webapp.exceptions.CustomHandlerExceptionResolver resolveException - could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

我不知道是什么原因造成的。不应该在每次输入时生成 employeeID 吗?还是我把 Hibernate 搞砸了?

谢谢!

最佳答案

想通了。

我的保存功能应该已将我正在创建的员工分配给每项工作:

public void saveEmployee(String firstname, String lastname, List<Job> jobs) {
Employee employee1 = new Employee();
employee1.setFirstName(firstname);
employee1.setLastName(lastname);
for (Job job : jobs) {
job.setEmployee(employee1);
}
employee1.setJobList(jobs);
entityManager.persist(employee1);
}

我还需要向我的工作表中的 employeeID 列添加一个外键约束:

<changeSet id="0003" author="mparker">
<addForeignKeyConstraint
baseTableName="jobs"
baseColumnNames="employeeID"
constraintName="FK_jobs_employeeID_employees_employeeID"
referencedTableName="employees"
referencedColumnNames="employeeID"/>
</changeSet>

谢谢大家的建议。

关于java - Spring-Hibernate : Insertion error, not-null 约束(一对多关系),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25313419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com