-->
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.  [ 4 posts ] 
Author Message
 Post subject: Problem with mapping (many-to-many)
PostPosted: Wed Jul 30, 2008 2:48 am 
Newbie

Joined: Tue Jul 29, 2008 2:56 pm
Posts: 3
Hello,

I want to map the following situation. I have a table called EDUCATION and a table called SCHOOLS. Between those tables I have an associative table called EDUCATION_SCHOOLS. The (usefull) fields:

EDUCATION:
id (long) - PK
name (varchar)
versionNr (long)

SCHOOLS:
id (long) - PK
name (varchar)
versionNr (long)

EDUCATION_SCHOOLS:
id (long) - PK
education_id (long) (FK to EDUCATION.id)
school_id (long) (FK to SCHOOLS.id)
name (varchar)
versionNr (long)

Their is a Unique Constraint between EDUCATION_SCHOOLS.education_id and EDUCATION_SCHOOLS.school_id.

What I want to be able to do:
EDUCATION: select, update, insert
SCHOOLS: select, update, insert
EDUCATION_SCHOOLS: select, update (only the non-FK fields), insert

I never want to delete anything in those tables. (and it's never ever going to be an option either)

Hibernate version:
Hibernate-Version: 3.0.5

Mapping documents:
Code:
Education:
<hibernate-mapping>
   <class name="##.Education" table="EDUCATION">
      <id name="id" column="ID" type="java.lang.Long">
         <generator class="sequence">
            <param name="sequence">EDUCATION_SEQ</param>
         </generator>
      </id>
      
      <version name="versionNr" column="VERSIONNR" type="long"/>
      
      <property name="name" column="NAME" type="string" />

        <set name="SCHOOLS" table="EDUCATION_SCHOOLS">
            <key column="EDUCATION_ID" />
            <many-to-many class="##.Schools" column="SCHOOL_ID" lazy="false" />
        </set>
</hibernate-mapping>

Schools:
<hibernate-mapping>
   <class name="##.Schools" table="SCHOOLS">
      <id name="id" column="ID" type="java.lang.Long">
         <generator class="sequence">
            <param name="sequence">SCHOOLS_SEQ</param>
         </generator>
      </id>
      
      <version name="versionNr" column="VERSIONNR" type="long"/>
      
      <property name="name" column="NAAM_NAME" type="string" />

        <set name="educations" table="EDUCATION_SCHOOLS" inverse="true" cascade="none">
            <key column="SCHOOL_ID" />
            <many-to-many class="##.Schools" column="SCHOOL_ID" lazy="proxy"/>
        </set>
</hibernate-mapping>

Education_schools:
<hibernate-mapping>
<class name="##.EducationSchools" table="EDUCATION_SCHOOLS">
       <id name="id" column="ID" type="java.lang.Long" unsaved-value="0">
         <generator class="sequence">
            <param name="sequence">SEQ_EDUCATION_SCHOOLS</param>
         </generator>
      </id>
      
      <version name="versionNr" column="VERSIONNR" type="long" />
      
      <many-to-one name="education" class="##.Education" cascade="none" lazy="proxy"
                 column="EDUCATION_ID" not-null="true"/>
        <many-to-one name="schools" class="##.Schools" cascade="none" lazy="proxy"
                    column="SCHOOL_ID" not-null="true"/>   
</hibernate-mapping>   


Name and version of the database you are using:
Oracle XE 10g


I am able to:
EDUCATION: select, insert, update
SCHOOLS: select, insert, update
EDUCATION_SCHOOLS: select

Problems:
EDUCATION_SCHOOLS: when I try to insert, I sometimes get unique constraint violations. (when I should get them, thus I'm trying to insert something that already exists .. but how do I stop Hibernate from Inserting?)

EDUCATION_SCHOOLS: when I try to update, sometimes it works, but often I get:


Code:
23:03:55,484 [http-8081-1] ERROR be.vlaanderen.lne.vea.epb.ui.struts.EpbExceptionHandler - org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Object of class [##.EducationSchools] with identifier [null]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [##.EducationSchools#<null>]
ex.getMessage() Object of class [##.EducationSchools] with identifier [null]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [##.EducationSchools#<null>]
org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Object of class [##.EducationSchools] with identifier [null]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [##.EducationSchools#<null>]
Caused by: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect):


As you can see from the stacktrace I use Spring for the transactionManager: org.springframework.orm.hibernate3.HibernateTransactionManager in which I use e sessionFactory: org.springframework.orm.hibernate3.LocalSessionFactoryBean

In my DAO, I try to save with the regular this.getHibernateTemplate().saveOrUpdate that has always worked for me.

Another problem I have:
when i update "name" in EDUCATION, the records with that ID are delete from EDUCATION_SCHOOLS ...


As I am experiencing 3 different problems, I'm pretty sure something is wrong in the mapping files .. however I fail to find out what .. Any input would be greatly appreciated.

(I translated some class/table-names, that's what the ## cause)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 01, 2008 8:16 am 
Newbie

Joined: Tue Jul 29, 2008 2:56 pm
Posts: 3
Anyone has any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 01, 2008 5:22 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
EDUCATION_SCHOOLS: when I try to insert, I sometimes get unique constraint violations. (when I should get them, thus I'm trying to insert something that already exists .. but how do I stop Hibernate from Inserting?)


Hibernate doesn't check constraints. You'll have to write your own code to check if a duplicate exists before you try to insert a new entity.

Quote:
when i update "name" in EDUCATION, the records with that ID are delete from EDUCATION_SCHOOLS ...


Are you sure that the Education.getSchools() method is implemented correctly? Eg. it must return the same Set that was passed to Education.setSchools(). It may also help to add inverse="true" to the mapping just as you have done for the Schools.education property.

Quote:
EDUCATION_SCHOOLS: when I try to update, sometimes it works, but often I get:


I am not sure why the stack trace displays Object .... with identifier [null]... I had expected the ID to be shown instead of null if it is an update of an existing entity. Anyway, if your code works some times but not always it is possible that there is actually another transaction that has updated the same record. It's hard to be more precise without seeing any code.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 04, 2008 8:40 am 
Newbie

Joined: Tue Jul 29, 2008 2:56 pm
Posts: 3
nordborg: thank you for your suggestion.

Concerning the unique constraint violation: I SELECT the record I'm trying to INSERT first in my "save-method". If the records already exists, I don't call saveOrUpdate(). Problem solved.

Concerning the deleted record when updating: I added inverse="TRUE" to education and schools and the delete doesn't happen anymore. I tought I could only inverse one side of the many-to-many. Problem also solved.

I also solved the HibernateOptimisticLockingFailureException. When I try to UPDATE I first select the record with the same EDUCATION_ID and SCHOOL_ID and I add the changed fields to the result of that SELECT. Imho I'm selecting once to much .. but it works, and it works fast.

So, I'm happy man :)

Thnx for your input!


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