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: key parent not exist
PostPosted: Thu Jul 27, 2006 8:10 am 
Newbie

Joined: Tue Jul 25, 2006 8:59 am
Posts: 18
Need help !

I have two table : B and A
the B have a relation with A on COLUM ID_BL

i save A and B object , i make a flush() on the session

i make a commit()
i have a violation of integrity due to the constraint : key parent A must exist.


this is the Mapping B is :

<!-- Associations -->
<many-to-one name="bonLivraison" class="org.ultimania.model.BonLivraison" column="ID_BL"/>


Must i create a relation one-to-many in A ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 27, 2006 8:19 am 
Beginner
Beginner

Joined: Tue May 23, 2006 4:10 pm
Posts: 38
Location: Charleston, SC
If I understand you correctly, the B object has an association to the A object. First, you don't need a bi-directional mapping. I assume your B object has a getter/setter for the A object. Set the association on the pojo, call save with the B object, and add a cascade setting on your association mapping.

Code:
<!-- Associations -->
<many-to-one name="bonLivraison" class="org.ultimania.model.BonLivraison" column="ID_BL" cascade="save-update"/>


Grant


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 27, 2006 8:50 am 
Newbie

Joined: Tue Jul 25, 2006 8:59 am
Posts: 18
that 's what i do , i try

<!-- Associations -->
<many-to-one name="bonLivraison" class="org.ultimania.model.BonLivraison" column="ID_BL" cascade="save-update"/>

and cascade="all" , whith the do i have the violation.

parent key not exist

i try to make a session.flush(); after the two save(A ) and save(B)

I have an error after the save(B) : same problem parent not exist


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 27, 2006 8:10 pm 
Beginner
Beginner

Joined: Tue May 23, 2006 4:10 pm
Posts: 38
Location: Charleston, SC
I suggest you post your entire mapping file and all your code. You are most likely doing something else wrong. We save complete object graphs using Hibernate's transitive persistence so I know it works.

Grrant


Top
 Profile  
 
 Post subject: the source code
PostPosted: Fri Jul 28, 2006 4:20 am 
Newbie

Joined: Tue Jul 25, 2006 8:59 am
Posts: 18
hello,

Sorry if the source are not well formated , i try to do my best.


That is the mapping of the father :

<hibernate-mapping>

<class name="org.ultimania.model.BonLivraison" table="BON_LIVRAISON" lazy="false" dynamic-update="true" dynamic-insert="true">

<id name="idBl" type="java.lang.Long" column="ID_BL">
<generator class="sequence">
<param name="sequence">SEQ_ID_BON_LIVRAISON</param>
</generator>
</id>

<timestamp column="DATE_MODIFICATION" name="dateModification" />

<property name="noBl" type="java.lang.Long" column="NO_BL" not-null="true" length="22" />

<property name="idEtablissement" type="java.lang.Long"
column="ID_ETABLISSEMENT" length="22" />

<property name="idFournisseur" type="java.lang.Long" column="ID_FOURNISSEUR" length="22" />

<property name="idLabo" type="java.lang.Long" column="ID_LABO"
length="22" />

<property name="idDepot" type="java.lang.Long" column="ID_DEPOT"
length="22" />

<property name="idDiffuseur" type="java.lang.Long"
column="ID_DIFFUSEUR" not-null="true" length="22" />

<property name="dateEnvoi" type="java.util.Date"
column="DATE_ENVOI" length="7" />

<property name="montantAchatHt" type="java.math.BigDecimal"
column="MONTANT_ACHAT_HT" length="15" />

<property name="montantVenteTtc" type="java.math.BigDecimal"
column="MONTANT_VENTE_TTC" length="15" />

<property name="montantAchatHtCdes" type="java.math.BigDecimal"
column="MONTANT_ACHAT_HT_CDES" length="15" />

<property name="montantVenteTtcCdes" type="java.math.BigDecimal"
column="MONTANT_VENTE_TTC_CDES" length="15" />

<property name="idTva" type="java.math.BigDecimal"
column="ID_TVA" length="22" />

<property name="sens" type="java.lang.Integer" column="SENS"
length="1" />

<property name="dateCreation" type="java.util.Date"
column="DATE_CREATION" length="7" />

<property name="dernierUtilisateur" type="java.lang.String"
column="DERNIER_UTILISATEUR" length="30" />

<property name="etat" type="java.lang.Boolean" column="ETAT"
not-null="true" length="1">

<meta attribute="use-in-equals">true</meta>
</property>

<!-- in comment , i don't make a bidirectional relation
<set name="commandes" inverse="true">
<key column="ID_BL" />
<one-to-many class="org.ultimania.model.Commande" />
</set>

-->

</class>
</hibernate-mapping>

that is the mapping of the child :

<hibernate-mapping>

<class
name="org.ultimania.model.Commande"
table="COMMANDE"
lazy="false"
>

<id
name="idCommande"
type="java.lang.Long"
column="ID_COMMANDE"
>


<generator class="sequence">
<param name="sequence">SEQ_ID_COMMANDE</param>
</generator>

</id>

<property
name="idEtablissement"
type="java.lang.Long"
column="ID_ETABLISSEMENT"
length="22"
/>
<property
name="idFournisseur"
type="java.lang.Long"
column="ID_FOURNISSEUR"
length="22"
/>
<property
name="idLabo"
type="java.lang.Long"
column="ID_LABO"
length="22"
/>
<property
name="idDepot"
type="java.lang.Long"
column="ID_DEPOT"
length="22"
/>
<property
name="idDiffuseur"
type="java.lang.Long"
column="ID_DIFFUSEUR"
length="22"
/>
<property
name="noPochette"
type="java.lang.Long"
column="NO_POCHETTE"
length="22"
/>
<property
name="idEtatCommande"
type="java.lang.Long"
column="ID_ETAT_COMMANDE"
length="22"
/>
<property
name="montantAchatHt"
type="java.math.BigDecimal"
column="MONTANT_ACHAT_HT"
length="15"
/>
<property
name="montantVenteTtc"
type="java.math.BigDecimal"
column="MONTANT_VENTE_TTC"
length="15"
/>
<property
name="idTypeCommande"
type="java.lang.Long"
column="ID_TYPE_COMMANDE"
length="22"
/>
<property
name="dateVente"
type="java.util.Date"
column="DATE_VENTE"
length="7"
/>
<property
name="dateCreation"
type="java.util.Date"
column="DATE_CREATION"
length="7"
/>
<property
name="dateModification"
type="java.util.Date"
column="DATE_MODIFICATION"
length="7"
/>
<property
name="dernierUtilisateur"
type="java.lang.String"
column="DERNIER_UTILISATEUR"
length="30"
/>
<property
name="etat"
type="java.lang.Boolean"
column="ETAT"
not-null="true"
length="1"
/>
<property
name="factureGeneree"
type="java.lang.Boolean"
column="FACTURE_GENEREE"
length="1"
/>

<many-to-one name="bonLivraison" class="org.ultimania.model.BonLivraison" column="ID_BL" cascade="save-update"/>

</class>
</hibernate-mapping>


that's what i do to save the father :

Transaction tx = null;

try {

tx = start.getVar().getSession().beginTransaction();
org.ultimania.model.BonLivraison bon = new org.ultimania.model.BonLivraison();
bon.setIdDepot(new Long(idDepot));
bon.setNoBl(new Long(noBL));
bon.setIdDiffuseur(new Long(idDiffuseur));
bon.setIdEtablissement(new Long(idEtablissement));
bon.setEtat(new Boolean(false));
bon.setIdFournisseur(new Long(idFournisseur));
bon.setIdLabo(new Long(idLabo));
bon.setDateEnvoi(dateEnvoi);
bon.setMontantAchatHt(new BigDecimal(montantAchatHT));
bon.setMontantVenteTtc(new BigDecimal(montantVenteTTC));
bon.setMontantAchatHtCdes(new BigDecimal(montantAchatHTCommandes));
bon.setMontantVenteTtcCdes(new BigDecimal(montantVenteTTCCommandes));
bon.setSens(new Integer(sensBL));

start.getVar().getSession().save(bon);
start.getVar().getSession().flush();


and after for the child :

org.ultimania.model.Commande comm = new org.ultimania.model.Commande();
comm.setIdDepot(bl.getIdDepot());
comm.setBonLivraison(bl);
comm.setIdDiffuseur( bl.getIdDiffuseur());
comm.setIdEtablissement(bl.getIdEtablissement());
comm.setEtat(new Boolean(false));
comm.setIdFournisseur(bl.getIdFournisseur());
comm.setIdLabo(bl.getIdLabo());
comm.setMontantAchatHt(new BigDecimal(montantAchatHT));
comm.setMontantVenteTtc(new BigDecimal(montantVenteTTC));
comm.setNoPochette(new Long(noPochette));
comm.setIdTypeCommande(new Long(typeCommande));

// here i save the child : no problem
start.getVar().getSession().save(comm);

// here i have the violation : parent key not exist
start.getVar().getSession().flush();

the constraint is

ALTER TABLE COMMANDE ADD CONSTRAINT FKABBFA7A42509EE
FOREIGN KEY (ID_BL)
REFERENCES PLSVISTA.BON_LIVRAISON (ID_BL) ;

Thank for your help


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.