-->
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: Problem with one to one mapping
PostPosted: Sat Aug 27, 2005 11:32 am 
Beginner
Beginner

Joined: Sat Dec 20, 2003 5:09 pm
Posts: 38
I'm sure this is simple, but I keep going round and round on this. I have a very simple, legacy relationship

Code:
clients
-------
id
dms_type

dms_types
------------
id
name




When I query for a clients object, I don't want the dms_type ID. I want the the client object to contain the DMS name. I've tried many different mapping configurations. THe one that get's me the closest is this one:

Code:
    <class name = "Clients" table="clients">
        <id name="id" type="int" column="id" unsaved-value="0">
            <generator class="native"/>
        </id>

        <property name="dms" column="dms_type" type="integer"/>

        <many-to-one name="dmsTypes" class="DmsTypes" column="crm_type" unique="true" insert="false" update="false"/>
</class?


And

Code:
    <class name = "DmsTypes" table="dms_types">
        <id name="id" type="int" column="id" unsaved-value="0">
            <generator class="native"/>
        </id>

        <property name="name" column="name" type="string"/>

        <one-to-one name="client" class="Clients" property-ref="dms"/>
    </class> 


When I run my tests, I see hibernate is trying to select all clients that have a clients.dms_type of some value. What I really want to happen is to have hibernate select all the dmsTypes (there will be only one) that have a crm_type.id of some value. Can someone point out what I'm doing wrong?

Hibernate version: version 3.0.5, 25 May 2005


Name and version of the database you are using: postgreSQL 7.4

The generated SQL (show_sql=true): select clients0_.id as id0_, clients0_.name as name70_0_, clients0_.active as active70_0_, clients0_.internal as internal70_0_, clients0_.addr1 as addr5_70_0_, clients0_.addr2 as addr6_70_0_, clients0_.city as city70_0_, clients0_.state as state70_0_, clients0_.zip as zip70_0_, clients0_.phone as phone70_0_, clients0_.timezone as timezone70_0_, clients0_.dms_type as dms12_70_0_ from clients clients0_ where clients0_.dms_type=?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 27, 2005 11:45 am 
Beginner
Beginner

Joined: Sat Dec 20, 2003 5:09 pm
Posts: 38
Well, I think I have it. I took the reverse mapping:

[code] <one-to-one name="client" class="Clients" property-ref="dms"/> [/code

Out of the DmsTypes definition. Now when I run tests I see that Hiberate is returning the string along with the other values.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 27, 2005 11:53 am 
Senior
Senior

Joined: Wed Jul 13, 2005 4:31 pm
Posts: 142
Location: Seattle, WA
Quote:
When I query for a clients object, I don't want the dms_type ID. I want the the client object to contain the DMS name.


To achieve the above, best solution is to use formula

Code:
<class name = "Clients" table="clients">
        <id name="id" type="int" column="id" unsaved-value="0">
            <generator class="native"/>
        </id>

        <property name="dms" formula="(select t.name from dms_types t where t.id=dms_type)" type="string"/>

</class>


if you want to use many-to-one, it can be done as...
(map dms_type column using many-to-one)
Code:
<class name = "Clients" table="clients">
        <id name="id" type="int" column="id" unsaved-value="0">
            <generator class="native"/>
        </id>
        <many-to-one name="dmsTypes" class="DmsTypes" column="dms_type" unique="true" insert="false" update="false"/>
</class>

with
Code:
   <class name = "DmsTypes" table="dms_types">
        <id name="id" type="int" column="id" unsaved-value="0">
            <generator class="native"/>
        </id>
        <property name="name" column="name" type="string"/>
    </class> 

(There is no need to reverse map the many-to-one relationship.)

Note: I havent' tested these. So, there may need to be some alterations.


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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.