-->
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.  [ 8 posts ] 
Author Message
 Post subject: Table per sub-class mapping pb
PostPosted: Tue Sep 07, 2004 9:40 am 
Beginner
Beginner

Joined: Mon Sep 15, 2003 9:10 am
Posts: 43
I have 2 class : entreprise and affrete (affrete is a sub class of entreprise).

But when i insert a new affrete, it only does an update instead of an insert.

The primary key of the affrete is also a foreign key, cause i choose to use the table per sub-class mapping.

Hibernate version: 2.1.4

Mapping documents:

Code:
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class
        name="org.astre.sig.modele.Entreprise"
        table="entreprises"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <jcs-cache usage="read-write" />

        <id
            name="id"
            column="id"
            type="java.lang.Long"
            unsaved-value="0"
        >
            <generator class="native">
            </generator>
        </id>

        </joined-subclass>
        <joined-subclass
            name="org.astre.sig.modele.Affrete"
            table="affretes"
            dynamic-update="false"
            dynamic-insert="false"
        >
        <key
            column="id"
        />

        </joined-subclass>

    </class>

</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

Code:
affrete=new Affrete();
affrete.setId(new Long(request.getParameter("idAffrete")));
mgrEntreprise.saveAffrete(affrete);


Name and version of the database you are using: Mysql 4

Thanks,

Fabien.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 07, 2004 10:50 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
what does mgrEntreprise.saveAffrete(affrete); do + stacktrace please

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 07, 2004 11:07 am 
Beginner
Beginner

Joined: Mon Sep 15, 2003 9:10 am
Posts: 43
Hi,

First, thanks for your replies. here is more informations :

Answer to your question :
---------------------
mgrEntreprise.saveAffrete(affrete) is a service class that just call the dao class and the dao do getHibernateTemplate().saveOrUpdate(objet).

In my database i have :
---------------------------

A empty table : "affrete" with only an id property (primary and foreign key to entreprise table).

entreprise table with already information for example :
|id | raisonSociale|
|1 | my company|

In fact i want to create a record in affrete table with the same id already in the entreprise table.

Code is :

Code:
affrete=new Affrete();
affrete.setId(new Long(request.getParameter("idAffrete")));  //idaffrete=1
mgrEntreprise.saveAffrete(affrete);

Stack trace :
---------------

Code:
ERROR - FrameworkServlet.service(342) | Could not complete request
org.springframework.orm.hibernate.HibernateSystemException: a different object with the same identifier value was already associated with the session: 1, of class: org.astre.sig.modele.Affrete; nested exception is net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 1, of class: org.astre.sig.modele.Affrete
net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 1, of class: org.astre.sig.modele.Affrete
   at net.sf.hibernate.impl.SessionImpl.checkUniqueness(SessionImpl.java:1673)
   at net.sf.hibernate.impl.SessionImpl.doUpdateMutable(SessionImpl.java:1442)
   at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1469)
   at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1392)
   at org.springframework.orm.hibernate.HibernateTemplate$13.doInHibernate(HibernateTemplate.java:320)
   at org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:176)
   at org.springframework.orm.hibernate.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:317)
   at org.astre.sig.dao.hibernateImpl.AffreteDAOHibernate.save(AffreteDAOHibernate.java:23)
   at org.astre.sig.service.EntrepriseManagerImpl.saveAffrete(EntrepriseManagerImpl.java:159)


Thanks,

Fabien.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 07, 2004 11:15 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Code:
affrete.setId(new Long(request.getParameter("idAffrete")));


Code:
        <id
            name="id"
            column="id"
            type="java.lang.Long"
            unsaved-value="0"
        >
            <generator class="native">
            </generator>
        </id>


there is a problem....

you are using an id generator BUT you 're assigning manually the id...

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 07, 2004 11:33 am 
Beginner
Beginner

Joined: Mon Sep 15, 2003 9:10 am
Posts: 43
Your are right because i was thinking that was the way to create a row in a subclass when the row already exist in the main class ? remember my problem:

Entreprise (main class table) row exist with the identifer : 1
Affrete (sub class table) doesn't exist with the identifier : 1

how to create the row in the Affrete table ?

Thanks

Fabien.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 07, 2004 11:38 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
think object, you just cannot change a type of a java object.

That's not very difficult.
In your case, just delete the first object, copy its properties into a new typed object, that's all

you still have to define a strategy to manage the ids, assigned or generated?

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 07, 2004 12:17 pm 
Beginner
Beginner

Joined: Mon Sep 15, 2003 9:10 am
Posts: 43
ok i understand. but simply copy is more complicated in my case...

In fact, the entreprise object has relations 1-n to other 4 tables (contacts, ...) so if i changed the id ....

Humm, that would not be simple...

Thanks again for your help antony,

Fabien


Top
 Profile  
 
 Post subject: Re: Table per sub-class mapping pb
PostPosted: Fri Aug 07, 2009 10:11 pm 
Newbie

Joined: Fri Aug 07, 2009 10:08 pm
Posts: 1
I came across this forum by chance and discovered this is one of the best thread ever. Thanks u guys so much for the information you give. So cool.


comparatif simulation assurance vie multisupport - simulation assurance vie ! Les sites de simulation assurance viecomparatif simulation assurance vie multisupport


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