-->
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: 1 zu N beziehung in Query abbilden
PostPosted: Mon Mar 10, 2008 12:18 pm 
Newbie

Joined: Mon Mar 10, 2008 11:55 am
Posts: 8
Hallo miteinander,

Hoffe mir kann geholfen werden.

Ich habe eine Tabelle "pref_language" in der die möglichen Sprachen (momentan (ger und eng) abgebildet sind. Ferner habe ich eine Tabelle "pref_Translations" in der für jedes Objekt 2 Einträge für Deutsch und Englisch vorhanden sind. Das ganze sieht in etwa so aus:

pref_language
Code:
pl_id     pl_name      pl_shortage
1          deutsch       ger
2          englisch      eng


pref_translation
Code:
pt_id    pl_id      pt_translation      pt_transl_group
12       1           Tisch                4
13       2           Table                4



Wie muss ich jetzt in der Methode "findByTranslGroup" (siehe unten) meine Abfrage formulieren, das ich mit Angabe der Shorty in pref_language und der transl_group den richtigen Wert bekomme also z.B.
Code:
findByTranslGroup("ger",4);


In der jetzigen Abfrage bekomme ich 2 Zeilen zurück.

Letztendlich will ich das eindeutige Ergebnis als Entity von "pref_translation".


Hibernate version: MyEclipse 6.0 bzw. Hibernate 3.x

Mapping documents:
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">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="de.bemar.hibernate.PrefLanguage" table="pref_language" catalog="test">
        <id name="plId" type="java.lang.Integer">
            <column name="pl_id" />
            <generator class="assigned" />
        </id>
        <property name="plName" type="java.lang.String">
            <column name="pl_name" length="45" not-null="true" />
        </property>
        <property name="plShortage" type="java.lang.String">
            <column name="pl_shortage" length="3" not-null="true" />
        </property>
        <set name="prefTranslations" inverse="true">
            <key>
                <column name="pl_id" not-null="true">
                    <comment>Spach ID</comment>
                </column>
            </key>
            <one-to-many class="de.bemar.hibernate.PrefTranslation" />
        </set>
    </class>
</hibernate-mapping>


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">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="de.bemar.hibernate.PrefTranslation" table="pref_translation" catalog="test">
        <id name="ptId" type="java.lang.Integer">
            <column name="pt_id" />
            <generator class="assigned" />
        </id>
        <many-to-one name="prefLanguage" class="de.bemar.hibernate.PrefLanguage" fetch="select">
            <column name="pl_id" not-null="true">
                <comment>Sprach ID</comment>
            </column>
        </many-to-one>
        <property name="ptTranslation" type="java.lang.String">
            <column name="pt_translation" not-null="true">
                <comment>Uebersetzungstext</comment>
            </column>
        </property>
        <property name="ptTranslGroup" type="java.lang.Integer">
            <column name="pt_transl_group" not-null="true">
                <comment>Ãœber diesen Key wird die Translation geholt</comment>
            </column>
        </property>
        <set name="prefGroups" inverse="true">
            <key>
                <column name="pg_transl_id" not-null="true">
                    <comment>Uebersetzungsgruppe</comment>
                </column>
            </key>
            <one-to-many class="de.bemar.hibernate.PrefGroup" />
        </set>
    </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
public List findByTranslGroup(int translGroup, String shorty) {
      log.debug("getting PrefTranslation instance with translGroup: " + translGroup + " and language "+shorty);
      try {
         
         String queryString = "from PrefTranslation as pt, PrefLanguage as pl " +
               "where pt.ptTranslGroup="+translGroup+" " +
               "and pl.plShortage= '"+shorty+"' ";
         Query queryObject = getSession().createQuery(queryString);         
         return queryObject.list();
      } catch (RuntimeException re) {
         log.error("get failed", re);
         throw re;
      }
   }


Name and version of the database you are using:
MySQL 5.x


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.