-->
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: Getting values of Bidrectional Association
PostPosted: Fri Jan 25, 2008 9:40 am 
Newbie

Joined: Fri Jan 25, 2008 9:07 am
Posts: 5
Hello all,

i'm quite a newbe on hibernate an have a problem on bidrectional associations.

I have three DB - Tables with following association.

Visite(1) <--> (m)Visite2Probe(m) <--> (1)Probe

Assume i want following information of these three tables/objects.
Here the common SQL stmt of what i want as result:

Code:
Select proben.kurztext as probenname, visiten.kurztext as visitename
from proben
inner join visiten2proben on proben.internalid = visiten2proben.fkProbenID
inner join visiten on visiten.internalid = visiten2proben.fkVisitenID
where visiten.internalid = 1



Ok - here is what i already have.
Mappings:

Mapping of table Proben
Code:
<hibernate-mapping>
<class name="dBStudien2Obj.Proben" table="proben" catalog="studien">
        <id name="internalid" type="java.lang.Integer">
            <column name="internalid" />
            <generator class="identity" />
        </id>
        <property name="kurztext" type="string">
            <column name="kurztext" length="10" />
        </property>

        <set name="visiten2probens" inverse="true">
            <key>
                <column name="fkProbenID" />
            </key>
            <one-to-many class="dBStudien2Obj.Visiten2proben" />
        </set>
</class>
</hibernate-mapping>


Mapping of Table Visiten
Code:
<hibernate-mapping>
<class name="dBStudien2Obj.Visiten" table="visiten" catalog="studien">
        <id name="internalid" type="java.lang.Integer">
            <column name="internalid" />
            <generator class="increment" />
        </id>
           <property name="kurztext" type="string">
          <column name="kurztext" length="10" />
        </property>
<set name="visiten2probens" inverse="true">
            <key>
                <column name="fkVisitenID" />
            </key>
            <one-to-many class="dBStudien2Obj.Visiten2proben" />
        </set>
</class>
</hibernate-mapping>


Finally the join - table visiten2proben
Code:
<hibernate-mapping>
    <class name="dBStudien2Obj.Visiten2proben" table="visiten2proben" catalog="studien">
        <id name="internalid" type="int">
            <column name="internalid" />
            <generator class="assigned" />
        </id>
        <many-to-one name="proben" class="dBStudien2Obj.Proben" fetch="select">
            <column name="fkProbenID" />
        </many-to-one>
        <many-to-one name="visiten" class="dBStudien2Obj.Visiten" fetch="select">
            <column name="fkVisitenID" />
        </many-to-one>
    </class>
</hibernate-mapping>


Further more i already have the classes to the table generated with hibernate tools.
And then i created a class StudienManager in which i try to provide methods for getting access to my data.

One method in class StudienManager would be.
Code:
public ??? getProbesANDVisitnameToVisite(pkVisiteID)
{
   Session session = HibernateSessionFactory.currentSession();
   session.beginTransaction();
//What do i have to code here to get the right data like the above sql - stmt
//?????????????

session.commit();
}


So far.
I have some questions.
1. Is the mapping of the tables ok - correct and well designed.
2. And what is the best way to get the right result corresponding the sql - stmt at the top of this post. --> How has the Method getProbesANDVisitnameToVisite(...) to look like???
And what kind of Object should i return on that method? (Probe or Visite)

What is the best way to go for that?

Unfortunately i found no tutorials which would do the select.

I hope you can help me...

Regards


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 26, 2008 1:46 pm 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
Hi Progster,

I think what you are trying to map is a classical bidirectional many-to-many-association between Visits and Probes. At the database level this is solved by the join table Visite2Probe - at the object level this latter only is reflected in the mapping of Visits and Probes.

Note, that Visits should have a mapping like this:
Code:
<set name="proben"
        table="Visit2Proben" cascade="save-update"
        <key column = "VISIT_ID" />
        <many-to-many class="Probe" column="PROBEN_ID" />
</set>

and Proben should have the reverse:
Code:
<set name="visits"
        table="Visit2Proben"
        inverse="true"
        cascade="save-update"
        <key column = "PROBEN_ID" />
        <many-to-many class="Visit" column="VISIT_ID" />
</set>


In your application code you just have methods like
Code:
visit.getProben() or probe.getVisits().
.

Hope this helps,

Carlo


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 26, 2008 5:07 pm 
Newbie

Joined: Fri Jan 25, 2008 9:07 am
Posts: 5
Ah, thanks for your reply.

As i see - in hibernate it is not needed to create a mapping file of the Join - Table visiten2proben.
Is that right? Or are there any situation where i will need a mapping file of a join table.
I think about it because the hibernate - tool of eclipse generated these files from my existing database.

Thanks a lot...


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 27, 2008 7:58 am 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
progster wrote:
Ah, thanks for your reply.

As i see - in hibernate it is not needed to create a mapping file of the Join - Table visiten2proben. Is that right?

Quite right!
Quote:
Or are there any situation where i will need a mapping file of a join table.

I cannot imagine of this - maybe some others do?

Quote:
Thanks a lot...

You could give me some credits -:)

Carlo


Top
 Profile  
 
 Post subject: Delete the association
PostPosted: Wed Jan 30, 2008 7:38 am 
Newbie

Joined: Fri Jan 25, 2008 9:07 am
Posts: 5
Hello again.

I have a problem on deleting association to that
E.g. I want do delete a visite all associations in studien2proben to the visite 1.
After that i want to delete the parent (Visite).

My question is, how can i delete the association with hibernate if there is no mapping - file studien2proben.hbm.xml.
Is this a case where i will need the join - table mapped in hibernate?!?
Or is there another way to delete the associations and the parent visiten.

Thanks a lot...


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.