-->
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.  [ 5 posts ] 
Author Message
 Post subject: Mapping a Joined String
PostPosted: Fri Oct 26, 2007 11:08 am 
Newbie

Joined: Fri Oct 26, 2007 11:01 am
Posts: 3
Hi there,

I am sort of new to hibernate. I would like to know how to map a certain property. I have a String property in my Person class that is split into a different table.

Heres my tables:

tbl_Person
========
ID
NAME
ADDRESS_ID

tbl_Address
=========
ID
ADDRESS

This is my class:

Class Person
String NAME
String ADDRESS

How would you map the address relationship on the person class?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 28, 2007 2:21 pm 
Regular
Regular

Joined: Wed Aug 25, 2004 7:40 pm
Posts: 65
There are a few approaches in this regard.

If you want to set up the address as an entity where you need a table with a unique id as you have set up, you can use one-to-one mapping or many-to-one mapping. Please read the reference documentation for more detailed information.

An easy way may be to set up the address as a component which you don't need a table if you only need one address per person.

HTH


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 29, 2007 3:36 am 
Newbie

Joined: Fri Oct 26, 2007 11:01 am
Posts: 3
Hi,

Thanks for the reply. This is actually a legacy system that I cannot change the database or the java source. I will have to sort this out with mappings. I am a bit fuzy on how to use a one-to-one to map to a string and not a custom class.

I would do this:
Code:
<hibernate-mapping>
<class name="Person" table="tbl_Person">

        <id name="id"
            type="java.lang.Integer"
            column="id"
            unsaved-value="0">
           
            <generator class="identity" />
        </id>

        <property name="name"
                  type="java.lang.String"
                  column="name"
                  not-null="true"
                  length="30"/>

       <one-to-one
         name="ADDRESS"
         foreign-key="ADDRESS_ID"
         cascade="save-update"/>

   </class>
</hibernate-mapping>



This doesnt work. How would hibernate know what column I want to put in that ADDRESS field? How would hibernate know what ID column it should match? Am I missing something huge here?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 29, 2007 7:55 am 
Newbie

Joined: Fri Oct 26, 2007 11:01 am
Posts: 3
I tried everything I can think of, is there no way to map a String to a class that sits in another table?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 29, 2007 10:10 am 
Regular
Regular

Joined: Mon Jan 22, 2007 10:32 am
Posts: 101
You can create a property with formula column that can bring the value of address from underlying table. But this resolve only the reading part.

If you can change the java souce (of person.java) then another option could be to have a read-write mapping for address with another name and the setter / getter for address can use the setter / getter of this property under the covers.

Another approach could be to implement address as a userType.

HTH

_________________
Please rate this post if you find it helpful


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.