hi, i have two tables.. Employee and salary. I intend that each customer must have only one entry in the salary table.
the table structure is :
customer:
EMPID primary key,
EMPNAME
salary :
SALID primary key,
netSal
cust_id(foreign key references customer(cust_id))
Now the problem is , one customer can have mulitple rows in the salary table. I tried using one-to-one mapping in the salary.hbm file but that doesn't help. Also i think if i use in the salary table the primary key as the foreign key of customer table then the problem might be solved. but i don't know how to write the corresponding mapping files.
Here are the two hbm files:
salary.hbm.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="employee.Salary" table="salary">
<id
name = "salId"
column = "SALID">
<generator class="increment"></generator>
</id>
<property
name = "netSal"
column = "netsal">
</property>
<!-- <many-to-one class="Employee" name="employee"
unique = "true" >
<column name = "EMPID"/>
</many-to-one> -->
<one-to-one name="employee" class = "Employee"
>
</one-to-one>
</class>
</hibernate-mapping>
Employee.hbm.xml file:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name = "Employee" table = "employee">
<id
name = "EmpId"
type = "int"
column = "EMPID">
<generator class = "assigned"></generator>
</id>
<property
name="EmpName"
column = "EMPNAME">
</property>
</class>
</hibernate-mapping>
The corresponding pojo files are:
salary.java:
Code:
public class Salary{
Double netSal=0.0;
int salId;
Employee employee;
.... //getters and setters
Employee.java
Code:
public class Employee{
int Empid;
String Empname;
//getters and setters
Also this is a web app and i have an index page where i am creating a SessionFactory but as the page can be referenced many times in a session, so each time a new SessionFactory is created. So where should the SessionFactory be initialised?
Thanks.