1
votes

am new to Hibernate learning entity mapping using hibernate framework via instead of annotation am using XML for mapping the entities

Here I have two classes Employee and Address [Address is the target class and Employee is the source class i.e is Employee table will have the foreign key column refers to Address table primary key]

class Employee{
String name;
int id;
Address addr;
//getter and setter methods
}

class Address{
String state;
String city
}

mapping.hbm.xml file:-

<hibernate-mapping>
<class name="Employee" table="emp">
<id name="id" column="id">
<generator class="assigned"/>
</id>
<property name="name" column="emp_name"/>
<one-to-one name="addr" class="Address" foreign-key="addr_id" cascade="all"/>
</class>

<!--mapping for Address Entity-->
<class name="Address" table="address>
<property name="city"/>
<property name"state"/>
</class>
</hibernate-mapping>

Note:- am using MySQL in table level I did not add a foreign key constraint to the employee table

And My Question while am try to save employee entity addr_id value is not stored in employee table how to resolve this issue.IN web most of the things are using annotations or shared a primary key.How to solve this issue kindly help me

1
How exactly are you trying to save?fg78nc
your address has not PK.davidxxx
Below is a link to xml based mapping spring and hibernate example mkyong.com/spring-security/…user8271644
Also check your logs or eclipse( or whatever you are using) console for full stack trace and post ituser8271644
@fg78nc am mentioning the pesudo code for storing the entity:- creating employee and address entity and setting the property of corresponding fields Session session=sessionFactory.openSession(); session.save(emp); session.getTransaction().commit();S Manikandan

1 Answers

0
votes

Change your mapping. You need to fully qualify class name and indicate join column, which is PK of Employee, column id.

Employee

<class name="Employee" table="emp">
<id name="id" column="id">
<generator class="assigned"/>

Address entity have to refer to the PK of Employee, which is id in your case :

 <one-to-one name="addr" class="my.package.Employee"
                   unique="true" not-null="true" cascade="all"/>