Java Hibernate with Entities:线程“main”org.Hibernate.Exception.genericjdbception:字段“department\u id”中的异常没有默认值
考虑以下类别: Employee.java Department.java 以及:Java Hibernate with Entities:线程“main”org.Hibernate.Exception.genericjdbception:字段“department\u id”中的异常没有默认值,java,hibernate,exception,hibernate-mapping,Java,Hibernate,Exception,Hibernate Mapping,考虑以下类别: Employee.java Department.java 以及: INFO: HHH000262: Table not found: DEPARTMENT Apr 12, 2014 12:08:59 PM org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata INFO: HHH000262: Table not found: EMPLOYEE Apr 12, 2014 12:08:59 PM org.hiber
INFO: HHH000262: Table not found: DEPARTMENT
Apr 12, 2014 12:08:59 PM org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: EMPLOYEE
Apr 12, 2014 12:08:59 PM org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
如何修复这些异常?一切似乎都很正常
顺便说一句,我使用的是Hibernate4.0
谢谢我已经有一段时间没有使用Hibernate了。数据库中是否存在这些表?您可以尝试运行应用程序,将hbm2ddl.auto更改为在hibernate.cfg.xml中创建吗?@TomasZ.:数据库中不存在这些表,我正在使用hibernate创建它们。我尝试将auto更改为create,但得到的表“myhibernate.department”不存在。请尝试在开始时注释掉createDB。在运行代码之前,您的架构应该存在。@TomasZ:“我得到了与以前相同的结果……感谢您在打印hibernate没有找到表之后创建了这些表。”。问题可能是hibernate没有为部门生成ID。此处的一些信息尝试将dialogue属性更改为hibernate.dialogue。如果不起作用,请尝试使用链接中的@GenericGenerator。
@Entity
@Table(name = "DEPARTMENT")
public class Department {
@Id
@Column(name="DEPARTMENT_ID")
@GeneratedValue(strategy=GenerationType.IDENTITY)
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;
}
}
public class Main {
private static final String FORNAME_URL = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost";
private static final String USERNAME = "root";
private static final String PASSWORD = "root";
private static final String DB_NAME = "MyHibernate";
private static final String HIBERNATE_CFG = "hibernate.cfg.xml";
/**
*
* @param args
* @throws ClassNotFoundException
* @throws SQLException
*/
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// create a DB
createDB();
// first the HIBERNATE settings
Configuration cfg = new Configuration().addResource(HIBERNATE_CFG).configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().
applySettings(cfg.getProperties()).buildServiceRegistry();
SessionFactory sessionFactory = cfg.buildSessionFactory(serviceRegistry);
Session session = sessionFactory.openSession();
session.beginTransaction();
Department department = new Department();
department.setDepartmentName("Sales");
Employee emp1 = new Employee("Nina", "Mayers", "111");
Employee emp2 = new Employee("Tony", "Almeida", "222");
department.setEmployees(new ArrayList<Employee>());
department.getEmployees().add(emp1);
department.getEmployees().add(emp2);
session.save(department);
session.getTransaction().commit();
session.close();
}
/**
* Drop & Create a Database
* Note that the DB name is above
* @throws SQLException
* @throws ClassNotFoundException
*/
public static void createDB() throws SQLException, ClassNotFoundException
{
try
{
Class.forName(FORNAME_URL);
Connection connection = DriverManager.getConnection(URL , USERNAME , PASSWORD);
Statement stmnt = connection.createStatement();
// Drop & recreate DB
stmnt.executeUpdate("DROP DATABASE IF EXISTS " + DB_NAME);
System.out.println("Database dropped successfully !");
stmnt.executeUpdate("CREATE DATABASE IF NOT EXISTS " + DB_NAME);
System.out.println("The Database was created successfully !");
}
catch (SQLException s)
{
System.out.println("Problem creating DB");
}
}
}
<?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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/MyHibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.pool_size">5</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</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
INFO: HHH000262: Table not found: DEPARTMENT
Apr 12, 2014 12:08:59 PM org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: EMPLOYEE
Apr 12, 2014 12:08:59 PM org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata