Consider the classes :
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.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;
}
}
Main program
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");
}
}
}
And the configuration file :
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="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>
When I run the Main class I get :
Exception in thread "main" org.hibernate.exception.GenericJDBCException: Field 'department_id' doesn't have a default value
and :
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
How can I fix those exceptions ? everything seems normal ...
BTW I'm using Hibernate 4.0
Thanks