-->
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: PB session.save(object)
PostPosted: Fri Aug 01, 2008 9:36 am 
Newbie

Joined: Fri Aug 01, 2008 9:02 am
Posts: 1
Bonjour,

Lorsque je save mon objet, j'obtiens l'erreur suivante :

violation de contrainte d'intégrité (BASE.FK_CLE) - clé parent introuvable

voici mes objets JAVA
Code:
public Class A {
private Long id;
private String a_attribut1;
}

public Class B {
private Long id;
private String b_attribut1;
private A b_attribut2;
}


mes mappings hibernates
Code:
<hibernate-mapping >
   <class name="A" table="TAB_A" >
      <id name="id" type="java.lang.Long" column="TAB_A_ID">
         <generator class="sequence">
            <param name="sequence">A_SEQ</param>
         </generator>
      </id>
      <property name="a_attribut1" type="java.lang.String" column="Attribut1" />
   </class>
</hibernate-mapping >

<hibernate-mapping >
   <class name="B" table="TAB_B" >
      <id name="id" type="java.lang.Long" column="TAB_B_ID">
         <generator class="sequence">
            <param name="sequence">B_SEQ</param>
         </generator>
      </id>
      
      <many-to-one name="b_attribut2" class="A" cascade="save-update">
         <column name="TAB_A_F_ID" />
      </many-to-one>
      
      <property name="b_attribut1" type="java.lang.String" column="attribut1" />
    </class>
</hibernate-mapping>


Code sur lequel j'ai une erreur
Code:
session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();

B b = new B();
A a = new A();
b.setB_attribut1("test");
a.setA_attribut1("test2");
b.setB_attribut1(a);

session.save(b);
tx.commit();
session.close();


En fait mon hibernate essaie de sauvegarder de récupérer la clé id de A alors qu'il n'existe pas encore.

comment faire pour que hibernate enregistre mon objet avant mon objet b.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2008 8:14 am 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
Documentation Hibernate, Chapitre 10.11 Transitive Persistence:

Quote:
Recommendations:

* It doesn't usually make sense to enable cascade on a <many-to-one> or <many-to-many> association. Cascade is often useful for <one-to-one> and <one-to-many> associations.
* If the child object's lifespan is bounded by the lifespan of the of the parent object make it a lifecycle object by specifying cascade="all,delete-orphan".
* Otherwise, you might not need cascade at all. But if you think that you will often be working with the parent and children together in the same transaction, and you want to save yourself some typing, consider using cascade="persist,merge,save-update".


En conclusion, dans ton cas, il semble qu'il faille configurer la cascade selon "persist,merge,save-update" (ou un "all, delete-orphan" si il s'agit d'une composition).

_________________
If you found my post helpful, please rate it! Thanks


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.