-->
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.  [ 4 posts ] 
Author Message
 Post subject: Saving a object and his child collection objects
PostPosted: Tue Oct 17, 2006 4:01 pm 
Newbie

Joined: Tue Oct 17, 2006 2:44 pm
Posts: 2
Location: Rio de Janeiro - Brazil
Hi !!
I have 3 Class. Equipe, Profissional and a Association Class ProfissionalEquipe, which have a composite-id.
I want to Add ProfissionalEquipe Objects to the Equipe Object Class, and when save the Equipe object, the class save to the ProfissionalEquipe objects collection. I don't want to manage ProfissionalEquipe class it'self.

But this exception is trow:

NHibernate.SQL: 2006-10-17 16:22:48,785 [AdpaterExeMgrThread1] DEBUG NHibernate.SQL - UPDATE PROFISSIONAL_EQUIPE SET FUNC_CD_SEQUENCIAL = @p0, PREQ_IN_LIDER = @p1 WHERE EQUI_CD_SEQUENCIAL = @p2 AND USER_ID = @p3; @p0 = '172', @p1 = 'S', @p2 = '391', @p3 = 'BLC3'
NHibernate.Impl.BatcherImpl: 2006-10-17 16:22:48,848 [AdpaterExeMgrThread1] DEBUG NHibernate.Impl.BatcherImpl - Closed IDbCommand, open IDbCommands :0
NHibernate.Impl.SessionImpl: 2006-10-17 16:22:48,863 [AdpaterExeMgrThread1] ERROR NHibernate.Impl.SessionImpl - could not synchronize database state with session
NHibernate.StaleStateException: Unexpected row count: 0; expected: 1

Note the hibernate is generating a update clause.

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" assembly="Organograma.Business.BusinessEntity" namespace="Organograma.Business.BusinessEntity">
<class name="Equipe" table="EQUIPE" lazy="false">

<id name="Codigo" column="EQUI_CD_SEQUENCIAL" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>

<property name="Nome" type="String(150)" column="EQUI_NM_EQUIPE" />
<property name="Sigla" type="String(15)" column="EQUI_SG_SIGLA" />
<property name="Atribuicoes" type="String(2000)" column="EQUI_TX_ATRIBUICAO" />

<many-to-one name="TipoEquipe" class="TipoEquipe" column="TIEQ_CD_SEQUENCIAL" cascade="none"/>
<many-to-one name="EquipeSuperior" class="Equipe" column="EQUI_CD_SEQUENCIAL_SUP" cascade="none"/>

<bag name="ProfissionaisEquipe" lazy="false" inverse="false" cascade="all-delete-orphan">
<key column="EQUI_CD_SEQUENCIAL" />
<one-to-many class="ProfissionalEquipe" />
</bag>

</class>
</hibernate-mapping>



<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" assembly="Organograma.Business.BusinessEntity" namespace="Organograma.Business.BusinessEntity">
<class name="ProfissionalEquipe" table="PROFISSIONAL_EQUIPE">
<composite-id>
<key-many-to-one class="Equipe" name="Equipe" column="EQUI_CD_SEQUENCIAL" />
<key-many-to-one class="Profissional" name="Profissional" column="USER_ID" />
</composite-id>
<many-to-one name="Funcao" class="Funcao" column="FUNC_CD_SEQUENCIAL" cascade="none"/>
<property name="Lider" type="Char" column="PREQ_IN_LIDER" />
</class>
</hibernate-mapping>



<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" assembly="Organograma.Business.BusinessEntity" namespace="Organograma.Business.BusinessEntity">
<class name="Profissional" table="PROFISSIONAL" lazy="false">

<id name="Chave" column="USER_ID" type="String(8)" unsaved-value="0">
<generator class="foreign">
<param name="property">Funcionario</param>
</generator>
</id>

<one-to-one name="Funcionario" class="Funcionario" constrained="true" />
<many-to-one name="TipoAcesso" class="TipoAcesso" column="TIAC_CD_SEQUENCIAL" cascade="none" />

<property name="Email" type="String(200)" column="PROF_NM_EMAIL" />
<property name="Ramais" type="String(50)" column="PROF_NR_RAMAL" />
<property name="Foto" type="BinaryBlob" column="PROF_IM_FOTO" />

<bag name="ProfissionaisEquipe" lazy="false" inverse="true" cascade="all-delete-orphan">
<key column="USER_ID" />
<one-to-many class="ProfissionalEquipe" />
</bag>

</class>

</hibernate-mapping>

_________________
Leonardo Nunes
System Analyst


Top
 Profile  
 
 Post subject: Don't know if it's still relevant...
PostPosted: Wed Nov 29, 2006 6:35 am 
Beginner
Beginner

Joined: Thu Nov 23, 2006 5:09 am
Posts: 21
I'm having a similar problem.

Im having a model with items/packages/boxes

Now I'm going to add a newly created package to a box.

the source is something like

Code:
Box box = BoxDAO,findById(id);

Package package = new package();
session.save(package);

box.addPackage(package);


(Where in
Code:
addPackage
I handle the bidirectional relation (List of packages in Box Object, Box reference in Package Objects).)

I guess the problem results in the sequence of actions.

Hibernate wants to update the records of parent and child to accomodate for the object references, but at this point there is no child, thus resulting in a exception.


BUT: I havn't solved it myself yet...

So any hints welcome!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 29, 2006 7:14 am 
Beginner
Beginner

Joined: Tue Nov 28, 2006 4:26 pm
Posts: 32
Location: Montreal, Quebec, Canada
Here's an example that may help. I have 3 tables: Application, User, and ApplicationUser.

ApplicationUser has 2 foreign keys: ApplicationId and UserId.

In my Application.hbm.xml and Application.cs I add nothing.

In my User.hbm.xml I have the following definition.

Code:
<bag name="Applications" table="ApplicationUser" cascade="none" lazy="false" inverse="false">
    <key column="UserId" />
    <many-to-many column="ApplicationId" class="Application" />
</bag>


In the User.cs I have the followng code.

Code:
private IList applications = new ArrayList();
public IList Applications
{
    get { return applications; }
    set { applications = value; }
}


All of this essentially causes the reference to any Application instances to be updated in the table ApplicationUser. It will not however create an Application. You will have to create the Application instance before attempting to store the User instance with the relationship. You must have an instance of Application already existing with a valid Id before this relationship can be stored.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 29, 2006 9:18 am 
Beginner
Beginner

Joined: Thu Nov 23, 2006 5:09 am
Posts: 21
Strange enough:

I solved it in my case by simply changing the sequence of actions:

Code:
Box box = BoxDAO,findById(id);

Package package = new package();
box.addPackage(package);

session.save(package);


I'm not jet this familiar with Hibernate, that I can tell why this behavior occurs.

Usualy I would say the JAVA Code should not interfere this much with the execution of DB Statements.
Even stranger in this case: The DB equivalent to
Code:
box.addPackage(package);
would be the UPDATE clause for the reference note in earlier posts. and at this point (pre );
Code:
session.save(package);
in JAVA) there would deffinetly be no child-entity.

?????


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