-->
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.  [ 9 posts ] 
Author Message
 Post subject: mapping One-to-One fileds
PostPosted: Tue May 22, 2007 8:53 pm 
Newbie

Joined: Sat May 19, 2007 12:02 pm
Posts: 19
I am new to hibernate and to ORM in general.
How do I map a filed in a class to another field in different class ?

If I have a property Employee.title that Should have a the value of Position.name and not the class position, how do I get around this ?

in other words, there's a table called Employee. title is one of the fileds in that table. And I have another table called Position that contains a field called positionName. How do I get Employee.title = Position.positionName ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 23, 2007 9:51 pm 
Newbie

Joined: Sat May 19, 2007 12:02 pm
Posts: 19
OK all I need is a tip or a link to a tutorial!


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 24, 2007 12:08 am 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
I guess you want Employee.title to be a foreign key to Position.name?

Here's one way how to do it.
http://www.hibernate.org/hib_docs/v3/re ... -manytoone

_________________
Don't forget to rate the reply if it helps..:)

Budyanto


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 24, 2007 1:22 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi


Employee

EmpId
EmpName
EmpTitle

Position
PositionId
PositionName


This is not exactly what you want. but your table structure might like this. Here EmpTitle foreign key is PositionId. With that link you can find position name. Don't use description type fields to foreignkey constrains.


hibernate mapping files may look like this.


Employee.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="test.Employee"
        table="Employee"
    >

       <id
            name="EmpId"
            column="EmpId"
            type="java.lang.Long"
        >
            <generator class="assigned">
             </generator>
        </id>

             <many-to-one
                 name="tool"
                 class="test.Position"
                 cascade="none"
                 update="true"
                 insert="true"
            lazy="false"
            column="EmpTitle"
        />

   <property
            name="EmpName"
            type="java.lang.String"
            update="true"
            insert="true"
            column="EmpName"
        />
       
    </class>

</hibernate-mapping>


Position.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="test.Position"
        table="Position"
    >

       <id
            name="PositionId"
            column="PositionId"
            type="java.lang.Long"
        >
            <generator class="assigned">
             </generator>
        </id>
             
   <property
            name="PositionName"
            type="java.lang.String"
            update="true"
            insert="true"
            column="PositionName"
        />
       
    </class>

</hibernate-mapping>



Amila

(Don't forget to rate if helps)


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 24, 2007 7:06 am 
Newbie

Joined: Sat May 19, 2007 12:02 pm
Posts: 19
What is tool?
Code:
<many-to-one
                 name="[color=red]tool[/color]"

The corresponding java classes are:
Code:
class Employee
{
private int EmpId;
private String EmpName ;
private String EmpTitle ;
//getters and setters here ..
}

and
Code:
class Position {
private int PositionId;
private String PositionName;
//getters and setters here ..
}

Now how would hibernate tell that Employee1 is mapped to Position2 ?
Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 24, 2007 7:43 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi



Employee.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="test.Employee"
        table="Employee"
    >

       <id
            name="EmpId"
            column="EmpId"
            type="java.lang.Long"
        >
            <generator class="assigned">
             </generator>
        </id>

             <many-to-one
                 name="pos"
                 class="test.Position"
                 cascade="none"
                 update="true"
                 insert="true"
            lazy="false"
            column="EmpTitle"
        />

   <property
            name="EmpName"
            type="java.lang.String"
            update="true"
            insert="true"
            column="EmpName"
        />
       
    </class>

</hibernate-mapping>




Code:
class Employee
{
private int EmpId;
private String EmpName ;
private Position pos;
private int positionID;

public String getEmpTitle(){

    return pos.getPositionName();
}


//getters and setters here ..
}




You didn't rate my answer


Amila
(Don't forget to rate)


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 24, 2007 8:16 am 
Newbie

Joined: Sat May 19, 2007 12:02 pm
Posts: 19
Amila:
BUt this doesn't make the field Employee.title = Posistion.name. This is the mapping I was looking for. Is this possible ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 24, 2007 8:43 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

No. You must have a link between two tables. it is EmpTitle. it links to Position table. So when you are loading a Employee object in hibenate, it will load automatically Position object which related to that Employee. You can use same position to another employee. That is the benifit of relational db.


Amila

(Don't forget to rate)


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 24, 2007 11:42 am 
Newbie

Joined: Sat May 19, 2007 12:02 pm
Posts: 19
That was alot of help. I will rate your answer again.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.