-->
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.  [ 6 posts ] 
Author Message
 Post subject: Another simple problem of Parent/child relationship :)
PostPosted: Tue Nov 30, 2004 10:18 am 
Newbie

Joined: Tue Nov 30, 2004 10:07 am
Posts: 3
I m using a simple relationship of a parent and a child.
The child is "Biographie" and the parent is "Auteur".

When i want to update my parent, i got :
java.sql.SQLException: Cannot insert the value NULL into column 'Auteur_ID', table 'HetCJacques.dbo.Biographie'; column does not allow nulls. INSERT fails.

Auteur_Id is the foreign key of my parent on the table of the child
It is a bi directionnal relationship, an OF COURSE i have not forgotten to set inverse = true

When i set cascade="none" of course i got no problem but my child are not stored.

Hibernate version is 2.1.6

Here is my java code :

###############################################################################################

Auteur auteur = AuteurService.getAuteurById(formModifierAuteur.getAuteurId());

auteur.setAuteurNom(formModifierAuteur.getAuteurNom());
auteur.setAuteurPrenom(formModifierAuteur.getAuteurPrenom());

List listeBiographie = (List)httpSession.getAttribute("listeBiographie");
auteur.setBiographies(ConversionCollection.convertirListEnSet(listeBiographie));

AuteurService.updateAuteur(auteur);


Code of updateAuteur is :
-----------------------

public static void updateAuteur(Object object) throws Exception
{
log.debug("Ajout ou modification d'un objet.");

Session session = HibernateUtil.currentSession();

session.saveOrUpdate(object);
session.flush();
session.connection().commit();

log.debug("Ajout ou modification effectuée.");

HibernateUtil.closeSession();
}

###############################################################################################

Here is the parent mapping file :

###############################################################################################

<?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>
<!--
Created by the Middlegen Hibernate plugin

http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->

<class
name="fr.secrets.intranet.commercial.modeles.Auteur"
table="Auteur"
>

<id
name="auteurId"
type="java.lang.Integer"
column="Auteur_ID"
>
<generator class="native" />
</id>

<property
name="auteurNom"
type="java.lang.String"
column="Auteur_Nom"
length="1073741823"
/>
<property
name="auteurPrenom"
type="java.lang.String"
column="Auteur_Prenom"
length="1073741823"
/>

<!-- associations -->
<!-- bi-directional one-to-many association to Biographie -->
<set
name="biographies"
lazy="false"
inverse="true"
cascade="all"
>
<key>
<column name="Auteur_ID" />
</key>
<one-to-many
class="fr.secrets.intranet.commercial.modeles.Biographie"
/>
</set>
<!-- bi-directional many-to-many association to Article -->
<set
name="articles"
lazy="true"
table="RArticleAuteur"
>
<key>
<column name="Auteur_ID" />
</key>
<many-to-many
class="fr.secrets.intranet.commercial.modeles.Article"
>
<column name="Article_ID" />
</many-to-many>
</set>

</class>
</hibernate-mapping>


###############################################################################################

And here is the child :

###############################################################################################

<?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>
<!--
Created by the Middlegen Hibernate plugin

http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->

<class
name="fr.secrets.intranet.commercial.modeles.Biographie"
table="Biographie"
>

<id
name="biographieId"
type="java.lang.Integer"
column="Biographie_ID"
>
<generator class="native" />
</id>

<property
name="biographieBioFrancais"
type="java.lang.String"
column="Biographie_BioFrancais"
length="1073741823"
/>
<property
name="biographieBioAnglais"
type="java.lang.String"
column="Biographie_BioAnglais"
length="1073741823"
/>

<!-- associations -->
<!-- bi-directional many-to-one association to Auteur -->
<many-to-one
name="auteur"
class="fr.secrets.intranet.commercial.modeles.Auteur"
not-null="true"
>
<column name="Auteur_ID" />
</many-to-one>
<!-- bi-directional many-to-one association to Collection -->
<many-to-one
name="collection"
class="fr.secrets.intranet.commercial.modeles.Collection"
not-null="true"
>
<column name="Collection_ID" />
</many-to-one>

</class>
</hibernate-mapping>


###############################################################################################
Log trace is :

###############################################################################################

[2004-11-30 19:12:46,459 net.sf.hibernate.connection.DriverManagerConnectionProvider DEBUG] total checked-out connections: 0
[2004-11-30 19:12:46,459 net.sf.hibernate.connection.DriverManagerConnectionProvider DEBUG] using pooled JDBC connection, pool size: 0
[2004-11-30 19:12:46,629 net.sf.hibernate.util.JDBCExceptionReporter WARN] SQL Error: 515, SQLState: 23000
[2004-11-30 19:12:46,629 net.sf.hibernate.util.JDBCExceptionReporter ERROR] Cannot insert the value NULL into column 'Auteur_ID', table 'HetCJacques.dbo.Biographie'; column does not allow nulls. INSERT fails.
[2004-11-30 19:12:46,639 net.sf.hibernate.util.JDBCExceptionReporter WARN] SQL Error: 515, SQLState: 23000
[2004-11-30 19:12:46,639 net.sf.hibernate.util.JDBCExceptionReporter ERROR] Cannot insert the value NULL into column 'Auteur_ID', table 'HetCJacques.dbo.Biographie'; column does not allow nulls. INSERT fails.
[2004-11-30 19:12:46,639 net.sf.hibernate.util.JDBCExceptionReporter ERROR] could not insert: [fr.secrets.intranet.commercial.modeles.Biographie]
java.sql.SQLException: Cannot insert the value NULL into column 'Auteur_ID', table 'HetCJacques.dbo.Biographie'; column does not allow nulls. INSERT fails.
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:366)
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2048)
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:1648)
at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:489)
at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQL(JtdsStatement.java:348)
at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeUpdate(JtdsPreparedStatement.java:360)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:528)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:432)
at net.sf.hibernate.impl.ScheduledIdentityInsertion.execute(ScheduledIdentityInsertion.java:29)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:932)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:857)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:775)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:738)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1388)
at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)
at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:526)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:482)
at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1475)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1393)
at fr.secrets.intranet.commercial.hibernate.HibernateService.saveOrUpdateObject(HibernateService.java:71)
at fr.secrets.intranet.commercial.services.AuteurService.updateAuteur(AuteurService.java:33)
at fr.secrets.intranet.commercial.struts.actions.auteur.ActionModifierAuteur.execute(ActionModifierAuteur.java:51)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:459)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:268)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1191)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:455)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:793)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:702)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:571)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:644)
at java.lang.Thread.run(Thread.java:534)
[2004-11-30 19:12:46,659 org.apache.struts.action.RequestProcessor WARN] Unhandled Exception thrown: class net.sf.hibernate.JDBCException


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 30, 2004 4:24 pm 
Regular
Regular

Joined: Wed Nov 05, 2003 10:57 pm
Posts: 96
Solution in the documentation (16.2. Bidirectional one-to-many).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 30, 2004 7:41 pm 
Newbie

Joined: Thu Nov 25, 2004 10:08 pm
Posts: 5
Code:
...
session.flush();
session.connection().commit();


That doesn't look right to me.

Have you tried with normal transaction handling code, something like this?
Code:
Session l_hibSession = null;
Transaction l_hibTx = null;
try
{
   // Now update our instance to the database
   l_hibSession = getSession();
   l_hibTx = l_hibSession.beginTransaction();
   l_hibSession.saveOrUpdate(_instance);
   l_hibTx.commit();
}
catch (HibernateException e)
{
   if (l_hibTx!=null && !l_hibTx.wasRolledBack()) l_hibTx.rollback();
      throw e;
}
finally
{
   if (l_hibSession.isConnected()) l_hibSession.close();
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 30, 2004 10:24 pm 
Newbie

Joined: Tue Nov 30, 2004 10:07 am
Posts: 3
But in the documentation (16.2) they say that i need to make the link part of the Child mapping. But i have do this !

In my child mapping i have :

<!-- associations -->
<!-- bi-directional many-to-one association to Auteur -->
<many-to-one
name="auteur"
class="fr.secrets.intranet.commercial.modeles.Auteur"
not-null="true"
>
<column name="Auteur_ID" />
</many-to-one>

And i have also the inverse = "true" on the other side !


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 01, 2004 5:14 am 
Newbie

Joined: Tue Nov 30, 2004 3:45 pm
Posts: 2
It seems to be a problem due to the generator type choice. If I choose a "native" generator (with MySQL database), I have the same problem. If I choose a "uuid.hex" generator, it works. Maybe the cascade insert doesn't work if the id of the parent is stored in an "autoincrement" database column ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 01, 2004 4:03 pm 
Regular
Regular

Joined: Wed Nov 05, 2003 10:57 pm
Posts: 96
The following line of code is not sufficient to set the association:

auteur.setBiographies(ConversionCollection.convertirListEnSet(listeBiographie));

You need a code similar to the one in the addChild():

public void addChild(Child c) {
c.setParent(this);
children.add(c);
}


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