gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:52:30 25 4
gpt4 key购买 nike

我正在使用注释学习 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;
}

}

部门.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 有问题,但我没有发现问题所在。

知道是什么原因造成的吗?

非常感谢

最佳答案

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

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

此外..

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

然后在添加 mappedBy 属性的员工上更改您的 @OneToMany

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

关于java - 使用 @ManyToOne 和 @OneToMany 会导致 org.hibernate.exception.GenericJDBCException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23157585/

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