-->
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.  [ 5 posts ] 
Author Message
 Post subject: Unable to delete a child
PostPosted: Wed Aug 18, 2004 4:29 am 
Beginner
Beginner

Joined: Mon Sep 15, 2003 9:10 am
Posts: 43
Hibernate version: 2.4

Mapping documents:

<?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>

<property
name="raisonSociale"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="raisonSociale"
/>

<property
name="adresse1"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="adresse1"
/>

<property
name="adresse2"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="adresse2"
/>

<property
name="codePostal"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="codePostal"
/>

<property
name="ville"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="ville"
/>

<many-to-one
name="entrepriseParent"
class="org.astre.sig.modele.Entreprise"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="id_parent"
/>

<many-to-one
name="regionAstrienne"
class="org.astre.sig.modele.RegionAstrienne"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="id_regionastrienne"
/>

<many-to-one
name="pays"
class="org.astre.sig.modele.Pays"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="id_pays"
/>

<many-to-one
name="region"
class="org.astre.sig.modele.Region"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="id_region"
/>

<many-to-one
name="departement"
class="org.astre.sig.modele.Departement"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="id_departement"
/>

<set
name="utilisateurs"
lazy="true"
inverse="false"
cascade="all-delete-orphan"
sort="unsorted"
>

<key
column="id_entreprise"
>
</key>

<one-to-many
class="org.astre.sig.modele.Utilisateur"
/>
</set>

<property
name="certification"
type="boolean"
update="true"
insert="true"
access="property"
column="certification"
/>

<property
name="codeApe"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="codeApe"
/>

<property
name="datePassageAstre"
type="java.util.Date"
update="true"
insert="true"
access="property"
column="datePassageAstre"
/>

<property
name="dateSortie"
type="java.util.Date"
update="true"
insert="true"
access="property"
column="dateSortie"
/>

<property
name="email"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="email"
/>

<property
name="fax"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="fax"
/>

<property
name="gestionMatiereDangereuse"
type="boolean"
update="true"
insert="true"
access="property"
column="gestionMatiereDangereuse"
/>

<property
name="referenceInstranet"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="referenceInstranet"
/>

<property
name="numeroAstre"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="numeroAstre"
/>

<property
name="numeroTVA"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="numeroTVA"
/>

<many-to-one
name="typeStatutAstre"
class="org.astre.sig.modele.TypeStatutAstre"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="id_typeStatutAstre"
/>

<many-to-one
name="typeStatutJuridique"
class="org.astre.sig.modele.TypeStatutJuridique"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="id_typeStatutjuridique"
/>

<many-to-one
name="typeStructure"
class="org.astre.sig.modele.TypeStructure"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="id_typeStructure"
/>

<property
name="telephone"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="telephone"
/>

<set
name="contacts"
lazy="true"
inverse="true"
cascade="all-delete-orphan"
sort="unsorted"
>

<key
column="id_entreprise"
>
</key>

<one-to-many
class="org.astre.sig.modele.Contact"
/>
</set>

<set
name="offres"
lazy="true"
inverse="false"
cascade="all-delete-orphan"
sort="unsorted"
>

<key
column="id_entreprise"
>
</key>

<one-to-many
class="org.astre.sig.modele.OffreFret"
/>
</set>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Entreprise.xml
containing the additional properties and place it in your merge dir.
-->

</class>

</hibernate-mapping>

<?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.Contact"
table="contacts"
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>

<property
name="email"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="email"
/>

<property
name="fax"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="fax"
/>

<property
name="nom"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="nom"
/>

<property
name="prenom"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="prenom"
/>

<property
name="telephone"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="telephone"
/>

<property
name="portable"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="portable"
/>

<many-to-one
name="entreprise"
class="org.astre.sig.modele.Entreprise"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="id_entreprise"
/>

<property
name="referenceInstranet"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="referenceInstranet"
/>

<bag
name="fonctions"
table="fonctionscontacts"
lazy="true"
inverse="false"
cascade="save-update"
>

<key
column="id_contact"
>
</key>

<many-to-many
class="org.astre.sig.modele.Fonction"
column="id_fonction"
outer-join="auto"
/>

</bag>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Contact.xml
containing the additional properties and place it in your merge dir.
-->

</class>

</hibernate-mapping>

My Object:

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

entreprise.addContact(contact);
mgr.saveEntreprise(entreprise); // [daoEntreprise.save(entreprise);]

assertEquals(1, entreprise.getContacts().size());
assertNotNull(contact.getEntreprise());

if (log.isDebugEnabled()) {
log.debug("Suppression contact...");
}

entreprise.getContacts().remove(contact);
mgr.saveEntreprise(entreprise);
log.info(entreprise);

Name and version of the database you are using:

MySQL 4.04

The entreprise object is ok, the contact is in database and link with the entreprise but when i remove my contact it is not deleted ...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 5:58 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
removeContact manage the link between your 2 tables, it has to set null not delete the child...

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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 6:18 am 
Beginner
Beginner

Joined: Mon Sep 15, 2003 9:10 am
Posts: 43
I try this way but doesn't work :

entreprise.addContact(contact);
mgr.saveEntreprise(entreprise); // [daoEntreprise.save(entreprise);]

assertEquals(1, entreprise.getContacts().size());
assertNotNull(contact.getEntreprise());

for (Iterator it = entreprise.getContacts().iterator(); it.hasNext();) {
Contact c = (Contact) it.next();
c=null;
}
mgr.saveEntreprise(entreprise);


Top
 Profile  
 
 Post subject: Spring way
PostPosted: Wed Aug 18, 2004 6:43 am 
Beginner
Beginner

Joined: Mon Sep 15, 2003 9:10 am
Posts: 43
In fact i was in a spring unit test and i was needed to put in the setup of my unit case in order to initialize the lazy collection :

sessionFactory = (SessionFactory) ctx.getBean("maSessionFactory");
Session session = SessionFactoryUtils.getSession(sessionFactory, true);
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));

If i close the connexion and reopen it between my save part ant the delete part it is ok :

entreprise.addContact(contact);
mgr.saveEntreprise(entreprise); // [daoEntreprise.save(entreprise);]

SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
Session s = holder.getSession();
s.flush();
TransactionSynchronizationManager.unbindResource(sessionFactory);
SessionFactoryUtils.closeSessionIfNecessary(s, sessionFactory);

sessionFactory = (SessionFactory) ctx.getBean("maSessionFactory");
Session session = SessionFactoryUtils.getSession(sessionFactory, true);
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));

Entreprise e=mgr.getEntreprise(entreprise.getId().toString());
Contact c=mgr.getContact(contact.getId().toString());

e.getContacts().remove(c);
mgr.saveEntreprise(e);

Do you understand why ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 18, 2004 7:29 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
the topic is not really easy to read
simplify mapping file with the associations having problem
simplify java code and replace all dao call with final hibernate api call...
please do your best to give a simplified topic

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


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