-->
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: Set avec <key ... not-null="true"/>
PostPosted: Thu Jun 09, 2005 11:22 am 
Regular
Regular

Joined: Tue May 03, 2005 8:19 am
Posts: 53
Location: Paris
J'ai un problème lors d'une sauvegarde d'un set avec l'attribut not-null plutot qu'un relation bidirectionnelle.
voir Very important note de la doc
http://www.hibernate.org/hib_docs/v3/re ... -onetomany

je me suis inspiré du testcase onetomany pour l'illustrer, je pense à un bugs, mais je ne l'ai pas trouver dans JIRA.

OneToManyTest utilise un mapping avec une relation bidirectionnelle fonctionne, tandis que OneToManyTestKO qui utilise l'attribut not-null ne fonctionne pas.

HS : on ne peut pas mettre d'attachement de fichier dans le forum, Pourquoi ?

Dans le répertoire test\org\hibernate\test\onetomany\ de la distrib :

Hibernate version:
3.0.5

Mapping documents:
fichier Parent.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">
<!--

  This mapping demonstrates how to use an "inverse" join
  to map an association table as an association with
  one-to-many multiplicity at the level of the object
  model.
     
-->
<hibernate-mapping package="org.hibernate.test.onetomany">
    <class name="Parent">
        <id name="id" column="parent_id">
            <generator class="increment" />
        </id>
        <property name="name" />
        <set name="children" table="ParentChild" cascade="all" inverse="true">
            <key column="parent_id"/>
            <one-to-many class="Child" />
        </set>
    </class>
    <class name="Child">
        <id name="id" column="child_id">
            <generator class="increment" />
        </id>
        <property name="name" />
        <many-to-one name="parent" class="Parent" column="parent_id"/>
    </class>
</hibernate-mapping>


fichier ParentKO.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="org.hibernate.test.onetomany">
    <class name="Parent">
        <id name="id" column="parent_id">
            <generator class="increment" />
        </id>
        <property name="name" />
        <set name="children" table="ParentChild" cascade="all" >
            <key column="parent_id" not-null="true"/>
            <one-to-many class="Child" />
        </set>
    </class>
    <class name="Child">
        <id name="id" column="child_id">
            <generator class="increment" />
        </id>
        <property name="name" />
    </class>
</hibernate-mapping>


fichier de Test:
fichier OneToManyTest.java
Code:
package org.hibernate.test.onetomany.notnull;

import java.io.Serializable;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Expression;
import org.hibernate.test.TestCase;
import org.hibernate.test.onetomany.Child;
import org.hibernate.test.onetomany.Parent;

/**
* @author Lilians Auvigne
*/
public class OneToManyTest extends TestCase {

   public OneToManyTest(String str) {
      super(str);
   }

   public Serializable getTestParentId() {
      Session s = openSession();
      Transaction t = s.beginTransaction();
      Child c = new Child();
      c.setName("Child One");
      Parent p = new Parent();
      p.setName("Parent");
      p.getChildren().add(c);
      c.setParent(p);

      c = new Child();
      c.setName("Child Two");
      p.getChildren().add(c);
      c.setParent(p);

      c = new Child();
      c.setName("Child three");
      p.getChildren().add(c);
      c.setParent(p);

      Serializable parentId = s.save(p);

      t.commit();
      s.close();

      s = openSession();
      t = s.beginTransaction();
      Parent pSaved = (Parent) s.createCriteria(Parent.class).add(
            Expression.eq("id", parentId)).uniqueResult();
      assertEquals("pSaved.getChildren().size() ", pSaved.getChildren()
            .size(), 3);

      t.commit();
      s.close();
      
      return parentId;
   }

   public void testOneToManySave3AndRemove() {
      Serializable parentId = getTestParentId();
      
      Session s = openSession();
      Transaction t = s.beginTransaction();
      
      Parent pSaved = (Parent) s.createCriteria(Parent.class).add(
            Expression.eq("id", parentId)).uniqueResult();
      
      //removed firstChild
      Child c = (Child) pSaved.getChildren().iterator().next();
      System.err.println("Test for removed " + c.getName());
      boolean isRemoved = pSaved.getChildren().remove(c);
      c.setParent(null);

      assertTrue("isRemoved", isRemoved);
      assertEquals("pSaved.getChildren().size() after remove ", pSaved.getChildren()
            .size(), 2);

      s.save(pSaved);

      assertEquals("pSaved.getChildren().size() after save", pSaved.getChildren()
            .size(), 2);

      pSaved = (Parent) s.createCriteria(Parent.class).add(
            Expression.eq("id", parentId)).uniqueResult();

      assertEquals("pSaved.getChildren().size() after get", 2, pSaved.getChildren().size());
      
      t.commit();
      s.close();
      
      //read this object
      s = openSession();
      t = s.beginTransaction();
      
      Parent pReaded = (Parent) s.createCriteria(Parent.class).add(
            Expression.eq("id", parentId)).uniqueResult();

      assertEquals("pReaded.getChildren().size() after get ", pReaded.getChildren()
            .size(), 2);
      
      t.commit();
      s.close();
   }

   protected String[] getMappings() {
      return new String[] { "onetomany/notnull/Parent.hbm.xml" };
   }

   public static Test suite() {
      return new TestSuite(OneToManyTest.class);
   }

}


fichier OneToManyTestKO.java
Code:
package org.hibernate.test.onetomany.notnull;

import java.io.Serializable;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Expression;
import org.hibernate.test.TestCase;
import org.jgroups.SetStateEvent;

/**
* @author Lilians Auvigne
*/
public class OneToManyTestKO extends OneToManyTest {

   public OneToManyTestKO(String str) {
      super(str);
   }
   
   protected String[] getMappings() {
      return new String[] { "onetomany/notnull/ParentKO.hbm.xml" };
   }

   public static Test suite() {
      return new TestSuite(OneToManyTestKO.class);
   }

}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 13, 2005 8:11 am 
Regular
Regular

Joined: Tue May 03, 2005 8:19 am
Posts: 53
Location: Paris
Après lecture de l'explication sur l'attribut inverse des associations bidirectionnelle et une enième lecture de la FAQ.

le problème vient bien de ma configuration, et fonctionne avec l'attribut cascade="all-delete-orphan"

FAQ
Inside explanation of inverse=true


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.