I'm new to hibernate and am having problems with a many-to-many relation. I have the following tables; surveys, linksurveyssections, surveysections, surveyquestions and linksurveyquestions. A survey can have multiple sections and a section can have multiple questions. Also a section can belong to multiple surveys and a question can belong to multiple sections. So there is a many to many relation between survey and surveysection. There is also a many-to-many relation between surveyquestion and survey section.
I'm using a link table between survey and section and between section and question. There may be a better way to do this? I'm all ears but even if there is a better way hibernate should be able to handle this as I have it defined.
If your willing to help and you need more info I'll be glad to supply it.
Hibernate seems to do the right thing for the link between survey and sections. Here is the sql from hibernate; as a sub-question is there a way to know the value assigned?
Code:
select linksurvey0_.surveyId_fk as surveyId2_1_
, linksurvey0_.linkId as linkId1_
, linksurvey0_.linkId as linkId9_0_
, linksurvey0_.surveyId_fk as surveyId2_9_0_
, linksurvey0_.surveySectionId_fk as surveySe3_9_0_
from linkSurveysSections linksurvey0_
where linksurvey0_.surveyId_fk=?
However the query for the link between survey sections and survey questions doesn't seem correct.
Code:
select linksurvey0_.surveyquestionId_fk as surveyqu3_1_
, linksurvey0_.linkId as linkId1_
, linksurvey0_.linkId as linkId10_0_
, linksurvey0_.surveysectionId_fk as surveyse2_10_0_
, linksurvey0_.surveyquestionId_fk as surveyqu3_10_0_
from linkSurveyquestions linksurvey0_
where linksurvey0_.surveyquestionId_fk=?
I think the where clause is wrong and should be
'where linksurvey0_.surveysectionId_fk=?'
I may have a problem in one of my xml files but I can't find it.
Thanks for your time.
Here are my xml files
Survey.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.dis.dqcs.data">
<class name="Survey" table="surveys">
<id name="surveyId" column="surveyId" type="long">
<generator class="native"/>
</id>
<property name="shortDescription" column="shortDescription" type="string" length="45"/>
<property name="longDescription" column="longDescription" type="string" length="45"/>
<set name="linkSurveysections" cascade="save-update" lazy="true">
<key column="surveyId_fk"/>
<one-to-many class="LinkSurveySurveysection"/>
</set>
<many-to-one name="surveytemplate" column="surveytemplateId_fk"
class="SurveyTemplate" cascade="save-update" />
</class>
</hibernate-mapping>
LinkSurveySurveysection.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.dis.dqcs.data">
<class name="LinkSurveySurveysection"
table="linkSurveysSections">
<id name="linkId" column="linkId" type="long">
<generator class="native"/>
</id>
<many-to-one name="survey" column="surveyId_fk"
class="Survey" cascade="save-update" />
<many-to-one name="surveysection" column="surveySectionId_fk"
class="SurveySection" cascade="save-update" />
</class>
</hibernate-mapping>
SurveySection.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.dis.dqcs.data">
<class name="SurveySection" table="surveySections">
<id name="surveysectionId" column="surveySectionId" type="long">
<generator class="native"/>
</id>
<property name="shortDescription" column="shortDescription" type="string"/>
<property name="longDescription" column="longDescription" type="string"/>
<set name="linkSurveySurveysection" cascade="save-update"
lazy="true">
<key column="surveysectionId_fk"/>
<one-to-many class="LinkSurveySurveysection"/>
</set>
<set name="linkSurveysectionSurveyquestion" cascade="save-update"
lazy="true">
<key column="surveyquestionId_fk"/>
<one-to-many class="LinkSurveysectionSurveyquestion"/>
</set>
</class>
</hibernate-mapping>
LinkSurveysectionSurveyquestion.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.dis.dqcs.data">
<class name="LinkSurveysectionSurveyquestion"
table="linkSurveyquestions">
<id name="linkId" column="linkId" type="long">
<generator class="native"/>
</id>
<many-to-one name="surveysection" column="surveysectionId_fk"
class="SurveySection" cascade="save-update" />
<many-to-one name="surveyquestion" column="surveyquestionId_fk"
class="Surveyquestion" cascade="save-update" />
</class>
</hibernate-mapping>
Surveyquestion.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.dis.dqcs.data">
<class name="Surveyquestion" table="surveyquestions">
<id name="questionId" column="questionId" type="long">
<generator class="native"/>
</id>
<property name="shortDescription" column="shortDescription" type="string"/>
<property name="longDescription" column="longDescription" type="string"/>
<set name="linkSurveysectionSurveyquestion" cascade="save-update"
lazy="true">
<key column="surveyquestionId_fk"/>
<one-to-many class="LinkSurveysectionSurveyquestion"/>
</set>
</class>
</hibernate-mapping>