-->
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.  [ 2 posts ] 
Author Message
 Post subject: Problem with many-to-many and insert query
PostPosted: Sun May 06, 2007 5:13 am 
Newbie

Joined: Sat May 05, 2007 4:19 pm
Posts: 5
Hi

I have some problems with my many-to-many association.
I'm using Hibebernate 3 and Spring 2.

I have a table person, a table language and a table persons_languages

In my java object, I have a class Person which has a set of Language and a class Language which has a set of Person.


here are the mapping files:

Language.hbm.xml
Code:
<hibernate-mapping>
    <class name="com.unisys.r4egov.eaw.spring.bo.Languages" table="languages" catalog="eaw1">
        <comment></comment>
        <id name="id" type="string">
            <column name="ID" length="3" />
            <generator class="assigned" />
        </id>
        <property name="name" type="string">
            <column name="NAME" length="20">
                <comment></comment>
            </column>
        </property>
<bag name="reqPersonsAs" table="persons_languages" outer-join="true"  cascade="all" lazy="false">
            <key>
                <column name="LANGUAGE_ID" length="3" not-null="true">
                    <comment></comment>
                </column>
            </key>
            <many-to-many entity-name="com.unisys.r4egov.eaw.spring.bo.ReqPersonsA">
                <column name="PERSON_ID" not-null="true">
                    <comment></comment>
                </column>
            </many-to-many>
        </bag>    </class>
</hibernate-mapping>


person.hbm.xml
Code:
<hibernate-mapping>
    <class name="com.unisys.r4egov.eaw.spring.bo.ReqPersonsA" table="req_persons_a" catalog="eaw1">
        <comment></comment>
        <id name="id" type="int" unsaved-value="0">
            <column name="ID" />
            <generator class="identity" />
        </id>
        <property name="name" type="string">
            <column name="NAME" length="200">
                <comment></comment>
            </column>
        </property><bag  name="languageses" outer-join="true" inverse="true" table="persons_languages" cascade="all"  lazy="false" >
            <key>
                <column name="PERSON_ID" not-null="true" >
                    <comment></comment>
                </column>
            </key>
            <many-to-many entity-name="com.unisys.r4egov.eaw.spring.bo.Languages" >
                <column name="LANGUAGE_ID" length="3" not-null="true" >
                    <comment></comment>
                </column>
            </many-to-many>
        </bag>
    </class>
</hibernate-mapping>


When I insert a Person which has the language 'fr', it is correctly inserted in the table persons_languages. But if i had a second person which has also the language 'fr', hibernate delete from the table persons_languages, the first person with the language 'fr' and insert the second person with the language 'fr'.

Here are the query sql generated by hibernate:

Code:
Hibernate: insert into eaw1.req_persons_a (NAME) values (?)
Hibernate: select languages_.ID, languages_.NAME as NAME12_ from eaw1.languages languages_ where languages_.ID=?
Hibernate: insert into eaw1.eu_arrest_warrant (CENTRAL_ID, SIGNATURE_ID, PERSON_ID, DECISION_ID, SENTENCE_ID, LANGUAGE_ID, ISSUING_JUDICIAL_AUTHORITY_ID, ABSENTIA_ID, DATE, LEGAL_GARANTEES_D, OTHER_CIRCUMSTANCES_F, TOTAL_OFFENCES_E, FULL_DESCRIPTION_OFFENCES_E) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

Hibernate: delete from persons_languages where LANGUAGE_ID=?

Hibernate: insert into persons_languages (LANGUAGE_ID, PERSON_ID) values (?, ?)


Why does hibernate delete all the entries with 'fr' before insert the new entry in persons_languages??


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 06, 2007 8:16 am 
Newbie

Joined: Sat May 05, 2007 4:19 pm
Posts: 5
Some more explanation:

Here is a simple representation of the problem: here

Actually, I save the entity EAW, and by cascade, hibernate insert the person, the language and so on...

the code to insert an EAW is the following:

Code:
getHibernateTemplate().save(eaw)


Here is the mapping file of the eaw:

Code:
<hibernate-mapping>
    <class  name="com.eaw.spring.bo.EuArrestWarrant" table="eu_arrest_warrant" catalog="eaw1">
        <comment></comment>
        <id name="id" type="int" unsaved-value="0">
            <column name="ID" />
            <generator class="identity"  />
        </id>
<many-to-one name="reqPersonsA" class="com.eaw.spring.bo.ReqPersonsA" fetch="join" cascade="save-update" lazy="false">
            <column name="PERSON_ID" >
                <comment></comment>
            </column>
        </many-to-one><many-to-one name="languages" class="com.eaw.spring.bo.Languages" fetch="join" cascade="save-update" lazy="false">
            <column name="LANGUAGE_ID" length="3">
                <comment></comment>
            </column>
        </many-to-one>    </class>
</hibernate-mapping>


and here is the code to insert :

Code:
EuArrestWarrant eawSpring = new EuArrestWarrant();
        com.eaw.axis.EuArrestWarrant eawAxis = paramEaw.getEuArrestWarrant();
       
        //date
        eawSpring.setDate(eawAxis.getDATE());
       
        //person
        ReqPersonsA person  = new ReqPersonsA();
        person.setName(eawAxis.getReq_person_a().getNAME());

        person.getEuArrestWarrants().add(eawSpring);
       
        LanguageItem[] langAxis = eawAxis.getReq_person_a().getLanguages().getLanguageItem();
       
        for(int i = 0; i < langAxis.length; i++){
            Languages langSpring = new Languages();
            langSpring.setId(langAxis[i].getLanguage());
       
            person.getLanguageses().add(langSpring);
            langSpring.getReqPersonsAs().add(person);
        }
 
        eawSpring.setReqPersonsA(person);
       
   
        eawService.create(eawSpring);
    }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.