I am new to Hibernate learning entity mapping using hibernate framework via instead of annotation i am using XML for mapping the entities using foreign key.
Here is my database :
employee.java :
package domaine;
import lombok.*;
@Getter @Setter @AllArgsConstructor @NoArgsConstructor
public class Employee {
private int employeeId;
private Account account;
private String first_name;
private String last_name;
private String email;
}
Account.java :
package domaine;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter @Setter @NoArgsConstructor @AllArgsConstructor
public class Account {
private int accountId;
private Employee employee;
private String acc_number;
}
employee.hbm.xml :
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 18 avr. 2017 12:10:58 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="domaine.Employee" table="EMPLOYEE">
<id name="employeeId" type="int">
<column name="ID" />
<generator class="increment" />
</id>
<property name="first_name" type="java.lang.String">
<column name="FIRST_NAME" />
</property>
<property name="last_name" type="java.lang.String">
<column name="LAST_NAME" />
</property>
<property name="email" type="java.lang.String">
<column name="EMAIL" />
</property>
<one-to-one name="account" cascade="all"
class="domaine.Account"></one-to-one>
</class>
</hibernate-mapping>
account.hbm.xml :
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 18 avr. 2017 12:10:58 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="domaine.Account" table="ACCOUNT">
<id name="accountId" type="int">
<column name="IDACCOUNT" />
<generator class="increment" />
</id>
<property name="acc_number" type="java.lang.String">
<column name="ACC_NUMBER" />
</property>
<one-to-one name="employee"
class="domaine.Employee"></one-to-one>
</class>
</hibernate-mapping>
test program :
package dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import domaine.Account;
import domaine.Employee;
public class TestOneToOne {
public static void main(String[] args) {
//creating configuration object
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file
//creating session factory object
SessionFactory factory=cfg.buildSessionFactory();
//creating session object
Session session=factory.openSession();
//creating transaction object
Transaction t=session.beginTransaction();
Account account = new Account();
account.setAcc_number("123-345-65454");
// Add new Employee object
Employee emp = new Employee();
emp.setEmail("[email protected]");
emp.setFirst_name("demo");
emp.setLast_name("user");
account.setEmployee(emp);
emp.setAccount(account);
session.persist(emp);
t.commit();//transaction is commited
session.close();
}
}
The problem is as follow when running the above code: the row in account table is added successfully. The row in employee table is added successfully but with null value for the foreign key (idaccount).
Do you have any idea with the origin of this problem ? and how can i resolve it ?
Thank you
