使用@ManyToOne和@OneToMany导致org.hibernate.exception.GenericJDBCException

时间:2014-04-18 15:50:48

标签: java hibernate hibernate-annotations

我正在学习带有注释的Hibernate,并且在尝试时遇到了一个奇怪的异常 要使用@OneToMany@ManyToOne,代码如下:

Employee.java

@Entity
@Table(name = "EMPLOYEE")
public class Employee {

    @Id
    @GeneratedValue
    @Column(name="employee_id")
    private Long employeeId;

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

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

    @Column(name="birth_date")
    private Date birthDate;

    @Column(name="cell_phone")
    private String cellphone;

    @ManyToOne
    @JoinColumn(name="department_id", insertable=false, updatable=false , nullable=false)
    private Department department;

    public Employee() {

    }

    public Employee(String firstname, String lastname, String phone) {
        this.firstname = firstname;
        this.lastname = lastname;
        this.birthDate = new Date(System.currentTimeMillis());
        this.cellphone = phone;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    public Long getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(Long employeeId) {
        this.employeeId = employeeId;
    }

    public String getFirstname() {
        return firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public Date getBirthDate() {
        return birthDate;
    }

    public String getCellphone() {
        return cellphone;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }

    public void setCellphone(String cellphone) {
        this.cellphone = cellphone;
    }

}

Department.java

@Entity
@Table(name = "DEPARTMENT")
public class Department {

    @Id
    @Column(name="DEPARTMENT_ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long departmentId;

    @Column(name="DEPT_NAME")
    private String departmentName;

    @OneToMany(cascade={CascadeType.ALL})
    @JoinColumn(name="department_id")
    @IndexColumn(name="idx")
    private List<Employee> employees;

    public Long getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(Long departmentId) {
        this.departmentId = departmentId;
    }

    public String getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }

    public List<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(List<Employee> employees) {
        this.employees = employees;
    }
}

hibernate.cfg.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost/DepartmentEmployee</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.pool_size">1</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">create</property>
    <mapping class="com.Department"></mapping>
    <mapping class="com.Employee"></mapping>
    </session-factory>
</hibernate-configuration>

当我尝试记录一些时,我得到了这个:

Exception in thread "main" org.hibernate.exception.GenericJDBCException: Field 'department_id' doesn't have a default value
    at org.hibernate.exception.internal.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:148)
    at org.hibernate.exception.internal.SQLStateConverter.convert(SQLStateConverter.java:136)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
    at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
    at com.sun.proxy.$Proxy12.executeUpdate(Unknown Source)
    at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
    at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:55)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2757)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3268)
    at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:78)
    at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:273)

department_id出了点问题,但我没有看到问题。

知道是什么原因引起的吗?

非常感谢

1 个答案:

答案 0 :(得分:1)

该异常告诉您department_id没有默认值。因此,如果您想要可以为空的值,请将可为空的值更改为Employee部门字段。

@ManyToOne
@JoinColumn(name="department_id", insertable=false, updatable=false , nullable=true)
private Department department;

此外..

Department表没有包含Employee表的外键的列。 @JoinColumn表示此实体是关系的所有者,因此请从Department类中的employees字段中删除该批注。

然后更改@OneToMany添加mappedBy属性

的员工
@OneToMany(mappedBy="department", cascade={CascadeType.ALL})
private List<Employee> employees;