-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: keeping a single row in a table
PostPosted: Sun Oct 02, 2011 8:42 am 
Newbie

Joined: Sun Oct 02, 2011 8:23 am
Posts: 2
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.


Top
 Profile  
 
 Post subject: Re: keeping a single row in a table
PostPosted: Sun Oct 02, 2011 2:51 pm 
Newbie

Joined: Sun Sep 25, 2011 4:05 pm
Posts: 12
Quote:
I tried using one-to-one mapping in the salary.hbm file but that doesn't help


You have a unidirectional one-to-one association, no join table, see 8.2.2 in the manual:
http://docs.jboss.org/hibernate/core/3. ... tional-121

You should use the second of the two examples in that section. The one problem I noticed between yours and the example is the id generator is "assigned" in yours but should use "foreign" for the salary entity.

Quote:
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?


See 1.1.6 in the manual:
http://docs.jboss.org/hibernate/core/3. ... pp-helpers


Top
 Profile  
 
 Post subject: Re: keeping a single row in a table
PostPosted: Mon Oct 03, 2011 7:27 am 
Newbie

Joined: Sun Oct 02, 2011 8:23 am
Posts: 2
thanks a lot for the reply..
i tried modifying the salary.hbm file and also the table as:
salary.hbm:
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="Salary" table="salary">

<id name="salId" column="SALID">
        <generator class="foreign">
            <param name="property">employee</param>
        </generator>
</id>

<property
name = "netSal"
column = "netsal">
</property>

<one-to-one  name="employee" class = "Employee"  constrained = "true"
>
</one-to-one>


</class>
</hibernate-mapping>


the salary table is as:
Code:
create table salary(salid int(5) primary key,netsal double)


Now the problem is in deleting. If i delete an employee then i get foreign key constraint even if add cascade = "all" in employee as follows:

employee.hbm:
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>

<one-to-one
name="salary"
class="Salary"
cascade = "all">
</one-to-one>



</class>

</hibernate-mapping>


where the corresponding pojo is:
employee.java
Code:

public class Employee
{
int EmpId;
String EmpName;
Salary salary;
//getters and setters
}

So, do i have to delete the salary row before deleting the corresponding Employee or is there any other way?
Thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.