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