Spring-Hibernate:插入错误,非空约束(一对多关系)

时间:2014-08-14 16:50:35

标签: java mysql hibernate spring-mvc liquibase

我已经有一段时间了,我无法找到我出错的地方。我认为这是一些轻微和/或简单的错误,第二双眼睛很快就会发现它。

我正在构建一个由员工和工作组成的MySQL数据库。一个员工可以拥有多个职位,但一个职位只能有一个职员。

我使用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>

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

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

Employee.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...
}

Job.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...

}

用于保存员工的方法(使用自动装配的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搞砸了什么?

谢谢!

1 个答案:

答案 0 :(得分:1)

想出来。

我的保存功能应该已经为每个作业分配了我正在创建的员工:

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);
}

我还需要在job表中的employeeID列中添加一个外键约束:

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

感谢大家的建议。