-->
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.  [ 1 post ] 
Author Message
 Post subject: Old issue asked again: 'Map one object from two tables'
PostPosted: Sat Jan 29, 2005 9:53 pm 
Beginner
Beginner

Joined: Sat Jan 29, 2005 8:49 pm
Posts: 20
i came up to the same internationalisation with hibernate problem as others did.

2 tables:

Code:
category
----------
id ... pk, seq
code ... string
parent .. category

label
------
id .... pk, seq
cat_id ... int
locale ... string
text .... string

why would i want it like that? so i can have generated pk for each label and for each category.

now i want to have a class category that has the following properties

Code:
class category
-----------------
id .... label.id
code
parent
locale .... current locale
text ..... text for the current locale
translations ... list of category objects with same code

then i can say something like that

session.find("from category c where c.locale = 'en' and c.parent.id = 13");

i really spent a lot of time now with this problem (read hibernate in action and so on) and it helped me to understand hibernate a bit more.

i also found a thread that seems to address exactly the same main issue
http://forum.hibernate.org/viewtopic.ph ... ght=locale

there greg_barton said on 22.03.2004 that this will be possible in v2.2
that is why i'm asking: is there already a way to do this in v2.1.7?

i also read that in v3 it is possible to define sql in the mapping files for about anything. would that help me with this?

what i did now is:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
                                   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="foo.bar" default-access="field">
   <class name="CategoryDetails" table="label">
      <id name="id" column="ID" type="int">
         <generator class="native"></generator>
      </id>
      <property name="locale" column="LANGUAGE" type="string"
         unique="false" not-null="true">
      </property>
      <property name="categoryId" column="CAT_ID" type="int"
         unique="false" not-null="true">
      </property>
      <property name="name" column="NAME" type="string" unique="false"
         not-null="true">
      </property>

      <joined-subclass name="Category" table="category" lazy="false">
         <key column="ID" foreign-key="CategoryId"></key>
         <many-to-one name="parent" class="Category" column="PARENT">
         </many-to-one>
         <set name="children" inverse="true" table="Categoryes"
            lazy="true">
            <key column="PARENT" foreign-key="ID"></key>
            <one-to-many class="Category" />
         </set>
         <property name="code" column="CODE" type="string"
            not-null="false" unique="false">
         </property>
      </joined-subclass>
   </class>
</hibernate-mapping>


and when i load some data with
Code:
session.find("from Category c where c.name like 'foo%'")


the generated sql is something like that (which is not exactly what i want).

Code:
select catego0_.ID as ID, catego0_.PARENT as PARENT1_, catego0_.CODE as CODE1_, catego0__1_.LANGUAGE as LANGUAGE0_, catego0__1_.CAT_ID as CAT_ID0_, catego0__1_.NAME as NAME0_ from categories catego0_ inner join label catego0__1_ on [b]catego0_.ID=catego0__1_.ID[/b] where (catego0__1_.NAME like 'foo%' )


now i'm again so close yet so far to a solution. i understand that the joined-subclass mappes the id of the subclass to the id of the class. yet i would like to define the foreign key myself.

(if there is some mismatch in the code forgive me it was hand edited before posting)

well! any comments are very much appreciated.[/code]


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

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.