-->
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: persisting an entity with a collection of components
PostPosted: Wed Sep 19, 2007 6:38 am 
Beginner
Beginner

Joined: Wed Aug 29, 2007 8:23 am
Posts: 31
hello guys.

I have the following on my hbm file:

<hibernate-mapping
xmlns="urn:nhibernate-mapping-2.2"
schema="dbo">
<class name="SRA.MercadosOO.Movimento,SRA.MercadosOO"
table="Movimento" lazy="false">
<id name="Id" column="IdMovimento" unsaved-value="0">
<generator class="identity" />
</id>
<version name="Version" column="Version"
type="int"/>
<property name="DataEntrada" column="DataEntrada" />
<property name="DataSaida" column="DataSaida" />
<property name="Peso" column="Peso" />
<property name="IdLote" column="IdLote" />
<property name="TotalDias" column="TotalDiasAnterior" />
<property name="Estado" column="Estado" />
<!--<many-to-one name="Taxa"
class="SRA.MercadosOO.Taxa, SRA.MercadosOO"
column="IdTaxaInicial"
cascade="none" />
-->
<property name="TipoTaxaInicial" column="TipoTaxaInicial" />
<bag name="Celulas" lazy="false" table="MovimentoCelula">
<key column="IdMovimento" />
<composite-element class="SRA.MercadosOO.CelulaMercado,SRA.MercadosOO">
<property name="IdCamara" column="IdCamara" />
<property name="Celula" column="Celula" />
<property name="Fila" column="Fila" />
<property name="Piso" column="Piso" />
</composite-element>
</bag>

</class>
</hibernate-mapping>

my problem is that when i have an instance of the Movimento object and i try to persist it inside a transaction, i'm seeing that the cascading is not being done inside that main transaction. since i'm running tests, this means that at the end of my test method, i have no elements on my movimento table and the movimentocelula table keeps that existed on the bag...

this seems to be confirmed by the logging that i'm putting here:

2007-09-19 11:37:40,514 [1] DEBUG NHibernate.Engine.Cascades - unsaved-value: 0
2007-09-19 11:37:40,535 [1] DEBUG NHibernate.Impl.SessionImpl - SaveOrUpdate() unsaved instance
2007-09-19 11:37:40,542 [1] DEBUG NHibernate.Impl.SessionImpl - saving [SRA.MercadosOO.Movimento#<null>]
2007-09-19 11:37:40,566 [1] DEBUG NHibernate.Impl.SessionImpl - executing insertions
2007-09-19 11:37:40,571 [1] DEBUG NHibernate.Engine.Cascades - unsaved-value: 0
2007-09-19 11:37:40,572 [1] DEBUG NHibernate.Engine.Versioning - Seeding: 1
2007-09-19 11:37:40,587 [1] DEBUG NHibernate.Impl.WrapVisitor - Wrapped collection in role: SRA.MercadosOO.Movimento.Celulas
2007-09-19 11:37:40,599 [1] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister - Inserting entity: SRA.MercadosOO.Movimento (native id)
2007-09-19 11:37:40,599 [1] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister - Version: 1
2007-09-19 11:37:40,668 [1] DEBUG NHibernate.Impl.BatcherImpl - Opened new IDbCommand, open IDbCommands: 1
2007-09-19 11:37:40,668 [1] DEBUG NHibernate.Impl.BatcherImpl - Building an IDbCommand object for the SqlString: INSERT INTO dbo.Movimento (Version, DataEntrada, DataSaida, Peso, IdLote, TotalDiasAnterior, Estado, TipoTaxaInicial) VALUES (?, ?, ?, ?, ?, ?, ?, ?); select SCOPE_IDENTITY()
2007-09-19 11:37:40,669 [1] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister - Dehydrating entity: [SRA.MercadosOO.Movimento#<null>]
2007-09-19 11:37:40,856 [1] DEBUG NHibernate.Impl.BatcherImpl - Opened IDataReader, open IDataReaders: 1
2007-09-19 11:37:40,908 [1] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister - Natively generated identity: 99
2007-09-19 11:37:40,909 [1] DEBUG NHibernate.Driver.NHybridDataReader - running NHybridDataReader.Dispose()
2007-09-19 11:37:40,910 [1] DEBUG NHibernate.Impl.BatcherImpl - Closed IDataReader, open IDataReaders :0
2007-09-19 11:37:40,912 [1] DEBUG NHibernate.Impl.BatcherImpl - Closed IDbCommand, open IDbCommands: 0
2007-09-19 11:37:40,956 [1] DEBUG NHibernate.Transaction.AdoTransaction - rollback
2007-09-19 11:37:40,961 [1] DEBUG NHibernate.Transaction.AdoTransaction - running AdoTransaction.Dispose()
2007-09-19 11:37:40,965 [1] DEBUG NHibernate.Impl.ConnectionManager - aggressively releasing database connection
2007-09-19 11:37:40,966 [1] DEBUG NHibernate.Connection.ConnectionProvider - Closing connection
2007-09-19 11:37:40,979 [1] DEBUG NHibernate.Impl.SessionImpl - transaction completion
2007-09-19 11:37:40,985 [1] DEBUG NHibernate.Impl.SessionImpl - flushing session
2007-09-19 11:37:40,990 [1] DEBUG NHibernate.Impl.SessionImpl - Flushing entities and processing referenced collections
2007-09-19 11:37:41,003 [1] DEBUG NHibernate.Impl.AbstractVisitor - Processing collection for role SRA.MercadosOO.Movimento.Celulas
2007-09-19 11:37:41,005 [1] DEBUG NHibernate.Impl.SessionImpl - Collection found: [SRA.MercadosOO.Movimento.Celulas#99], was: [<unreferenced>]
2007-09-19 11:37:41,009 [1] DEBUG NHibernate.Impl.SessionImpl - Processing unreferenced collections
2007-09-19 11:37:41,009 [1] DEBUG NHibernate.Impl.SessionImpl - scheduling collection removes/(re)creates/updates
2007-09-19 11:37:41,011 [1] DEBUG NHibernate.Impl.SessionImpl - Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
2007-09-19 11:37:41,011 [1] DEBUG NHibernate.Impl.SessionImpl - Flushed: 1 (re)creations, 0 updates, 0 removals to 1 collections
2007-09-19 11:37:41,012 [1] DEBUG NHibernate.Impl.Printer - listing entities:
2007-09-19 11:37:41,024 [1] DEBUG NHibernate.Impl.Printer - SRA.MercadosOO.Movimento{Celulas=[CelulaMercado{Piso=1, IdCamara=1, Fila=1, Celula=1}], Estado=1, TipoTaxaInicial=0, DataSaida=null, Version=1, DataEntrada=19-09-2007, Id=99, IdLote=1, TotalDias=10, Peso=10}
2007-09-19 11:37:41,025 [1] DEBUG NHibernate.Impl.SessionImpl - executing flush
2007-09-19 11:37:41,025 [1] DEBUG NHibernate.Impl.ConnectionManager - registering flush begin
2007-09-19 11:37:41,030 [1] DEBUG NHibernate.Persister.Collection.ICollectionPersister - Inserting collection: [SRA.MercadosOO.Movimento.Celulas#99]
2007-09-19 11:37:41,033 [1] DEBUG NHibernate.Impl.BatcherImpl - Opened new IDbCommand, open IDbCommands: 1
2007-09-19 11:37:41,034 [1] DEBUG NHibernate.Impl.BatcherImpl - Building an IDbCommand object for the SqlString: INSERT INTO dbo.MovimentoCelula (IdMovimento, IdCamara, Celula, Fila, Piso) VALUES (?, ?, ?, ?, ?)
2007-09-19 11:37:41,037 [1] DEBUG NHibernate.Connection.DriverConnectionProvider - Obtaining IDbConnection from Driver
2007-09-19 11:37:41,057 [1] DEBUG NHibernate.Persister.Collection.ICollectionPersister - done inserting collection: 1 rows inserted
2007-09-19 11:37:41,064 [1] DEBUG NHibernate.Impl.BatcherImpl - Closed IDbCommand, open IDbCommands: 0
2007-09-19 11:37:41,064 [1] DEBUG NHibernate.Impl.ConnectionManager - skipping aggressive-release due to flush cycle
2007-09-19 11:37:41,064 [1] DEBUG NHibernate.Impl.ConnectionManager - registering flush end
2007-09-19 11:37:41,065 [1] DEBUG NHibernate.Impl.ConnectionManager - aggressively releasing database connection
2007-09-19 11:37:41,065 [1] DEBUG NHibernate.Connection.ConnectionProvider - Closing connection
2007-09-19 11:37:41,067 [1] DEBUG NHibernate.Impl.SessionImpl - post flush
2007-09-19 11:37:41,069 [1] DEBUG NHibernate.Impl.SessionImpl - closing session
2007-09-19 11:37:41,069 [1] DEBUG NHibernate.Impl.BatcherImpl - running BatcherImpl.Dispose(true)
2007-09-19 11:37:41,071 [1] DEBUG NHibernate.Transaction.AdoTransaction - running AdoTransaction.Dispose()

can smoeone tell me what i'm missing?

thanks

[update: in my test class, i'm starting a transaction from my setupmethod and closing it from my teardown method]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 19, 2007 6:45 am 
Newbie

Joined: Mon Aug 13, 2007 9:27 am
Posts: 12
edit like this:

Code:
<bag name="Celulas" lazy="false" table="MovimentoCelula" cascade="all">
<key column="IdMovimento" />
<composite-element class="SRA.MercadosOO.CelulaMercado,SRA.MercadosOO">
<property name="IdCamara" column="IdCamara" />
<property name="Celula" column="Celula" />
<property name="Fila" column="Fila" />
<property name="Piso" column="Piso" />
</composite-element>
</bag>


just added a cascade attribute to bag element.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 19, 2007 7:02 am 
Beginner
Beginner

Joined: Wed Aug 29, 2007 8:23 am
Posts: 31
hello.

i did try that, but still no luck. it really looks like during the "1st pass" nhibernate will only persist the values of the movimento element and won't do anything to component collection maintained on the bag. then, when it flushes the session, it somwhoe sees that there's one element on the collection and it just persists it by creating a new connection and executing the command (at least, this is what i think it's doing).

can someone help me understand the log of operations executed by nhibernate (maybe i'm seeing it incorrectly). btw, i'm not sure if waht i need is supported for collection of components or if it'll only work for collection of elements:

2007-09-19 11:58:03,848 [1] DEBUG NHibernate.Engine.Cascades - unsaved-value: 0
2007-09-19 11:58:03,868 [1] DEBUG NHibernate.Impl.SessionImpl - SaveOrUpdate() unsaved instance
2007-09-19 11:58:03,875 [1] DEBUG NHibernate.Impl.SessionImpl - saving [SRA.MercadosOO.Movimento#<null>]
2007-09-19 11:58:03,881 [1] DEBUG NHibernate.Engine.Cascades - processing cascades for: SRA.MercadosOO.Movimento
2007-09-19 11:58:03,884 [1] DEBUG NHibernate.Engine.Cascades - done processing cascades for: SRA.MercadosOO.Movimento
2007-09-19 11:58:03,885 [1] DEBUG NHibernate.Impl.SessionImpl - executing insertions
2007-09-19 11:58:03,889 [1] DEBUG NHibernate.Engine.Cascades - unsaved-value: 0
2007-09-19 11:58:03,890 [1] DEBUG NHibernate.Engine.Versioning - Seeding: 1
2007-09-19 11:58:03,912 [1] DEBUG NHibernate.Impl.WrapVisitor - Wrapped collection in role: SRA.MercadosOO.Movimento.Celulas
2007-09-19 11:58:03,922 [1] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister - Inserting entity: SRA.MercadosOO.Movimento (native id)
2007-09-19 11:58:03,922 [1] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister - Version: 1
2007-09-19 11:58:03,960 [1] DEBUG NHibernate.Impl.BatcherImpl - Opened new IDbCommand, open IDbCommands: 1
2007-09-19 11:58:03,960 [1] DEBUG NHibernate.Impl.BatcherImpl - Building an IDbCommand object for the SqlString: INSERT INTO dbo.Movimento (Version, DataEntrada, DataSaida, Peso, IdLote, TotalDiasAnterior, Estado, TipoTaxaInicial) VALUES (?, ?, ?, ?, ?, ?, ?, ?); select SCOPE_IDENTITY()
2007-09-19 11:58:03,961 [1] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister - Dehydrating entity: [SRA.MercadosOO.Movimento#<null>]
2007-09-19 11:58:04,078 [1] DEBUG NHibernate.Impl.BatcherImpl - Opened IDataReader, open IDataReaders: 1
2007-09-19 11:58:04,160 [1] DEBUG NHibernate.Persister.Entity.AbstractEntityPersister - Natively generated identity: 105
2007-09-19 11:58:04,161 [1] DEBUG NHibernate.Driver.NHybridDataReader - running NHybridDataReader.Dispose()
2007-09-19 11:58:04,162 [1] DEBUG NHibernate.Impl.BatcherImpl - Closed IDataReader, open IDataReaders :0
2007-09-19 11:58:04,164 [1] DEBUG NHibernate.Impl.BatcherImpl - Closed IDbCommand, open IDbCommands: 0
2007-09-19 11:58:04,170 [1] DEBUG NHibernate.Engine.Cascades - processing cascades for: SRA.MercadosOO.Movimento
2007-09-19 11:58:04,172 [1] DEBUG NHibernate.Engine.Cascades - cascading to collection: SRA.MercadosOO.Movimento.Celulas
2007-09-19 11:58:04,176 [1] DEBUG NHibernate.Engine.Cascades - done processing cascades for: SRA.MercadosOO.Movimento
2007-09-19 11:58:04,201 [1] DEBUG NHibernate.Transaction.AdoTransaction - rollback
2007-09-19 11:58:04,207 [1] DEBUG NHibernate.Transaction.AdoTransaction - running AdoTransaction.Dispose()
2007-09-19 11:58:04,211 [1] DEBUG NHibernate.Impl.ConnectionManager - aggressively releasing database connection
2007-09-19 11:58:04,212 [1] DEBUG NHibernate.Connection.ConnectionProvider - Closing connection
2007-09-19 11:58:04,224 [1] DEBUG NHibernate.Impl.SessionImpl - transaction completion
2007-09-19 11:58:04,230 [1] DEBUG NHibernate.Impl.SessionImpl - flushing session
2007-09-19 11:58:04,232 [1] DEBUG NHibernate.Engine.Cascades - processing cascades for: SRA.MercadosOO.Movimento
2007-09-19 11:58:04,232 [1] DEBUG NHibernate.Engine.Cascades - cascading to collection: SRA.MercadosOO.Movimento.Celulas
2007-09-19 11:58:04,232 [1] DEBUG NHibernate.Engine.Cascades - done processing cascades for: SRA.MercadosOO.Movimento
2007-09-19 11:58:04,235 [1] DEBUG NHibernate.Impl.SessionImpl - Flushing entities and processing referenced collections
2007-09-19 11:58:04,248 [1] DEBUG NHibernate.Impl.AbstractVisitor - Processing collection for role SRA.MercadosOO.Movimento.Celulas
2007-09-19 11:58:04,251 [1] DEBUG NHibernate.Impl.SessionImpl - Collection found: [SRA.MercadosOO.Movimento.Celulas#105], was: [<unreferenced>]
2007-09-19 11:58:04,255 [1] DEBUG NHibernate.Impl.SessionImpl - Processing unreferenced collections
2007-09-19 11:58:04,255 [1] DEBUG NHibernate.Impl.SessionImpl - scheduling collection removes/(re)creates/updates
2007-09-19 11:58:04,255 [1] DEBUG NHibernate.Impl.SessionImpl - Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
2007-09-19 11:58:04,255 [1] DEBUG NHibernate.Impl.SessionImpl - Flushed: 1 (re)creations, 0 updates, 0 removals to 1 collections
2007-09-19 11:58:04,257 [1] DEBUG NHibernate.Impl.Printer - listing entities:
2007-09-19 11:58:04,266 [1] DEBUG NHibernate.Impl.Printer - SRA.MercadosOO.Movimento{Celulas=[CelulaMercado{Piso=1, IdCamara=1, Fila=1, Celula=1}], Estado=1, TipoTaxaInicial=0, DataSaida=null, Version=1, DataEntrada=19-09-2007, Id=105, IdLote=1, TotalDias=10, Peso=10}
2007-09-19 11:58:04,266 [1] DEBUG NHibernate.Impl.SessionImpl - executing flush
2007-09-19 11:58:04,266 [1] DEBUG NHibernate.Impl.ConnectionManager - registering flush begin
2007-09-19 11:58:04,271 [1] DEBUG NHibernate.Persister.Collection.ICollectionPersister - Inserting collection: [SRA.MercadosOO.Movimento.Celulas#105]
2007-09-19 11:58:04,274 [1] DEBUG NHibernate.Impl.BatcherImpl - Opened new IDbCommand, open IDbCommands: 1
2007-09-19 11:58:04,275 [1] DEBUG NHibernate.Impl.BatcherImpl - Building an IDbCommand object for the SqlString: INSERT INTO dbo.MovimentoCelula (IdMovimento, IdCamara, Celula, Fila, Piso) VALUES (?, ?, ?, ?, ?)
2007-09-19 11:58:04,278 [1] DEBUG NHibernate.Connection.DriverConnectionProvider - Obtaining IDbConnection from Driver
2007-09-19 11:58:04,293 [1] DEBUG NHibernate.Persister.Collection.ICollectionPersister - done inserting collection: 1 rows inserted
2007-09-19 11:58:04,299 [1] DEBUG NHibernate.Impl.BatcherImpl - Closed IDbCommand, open IDbCommands: 0
2007-09-19 11:58:04,300 [1] DEBUG NHibernate.Impl.ConnectionManager - skipping aggressive-release due to flush cycle
2007-09-19 11:58:04,300 [1] DEBUG NHibernate.Impl.ConnectionManager - registering flush end
2007-09-19 11:58:04,300 [1] DEBUG NHibernate.Impl.ConnectionManager - aggressively releasing database connection
2007-09-19 11:58:04,300 [1] DEBUG NHibernate.Connection.ConnectionProvider - Closing connection
2007-09-19 11:58:04,302 [1] DEBUG NHibernate.Impl.SessionImpl - post flush
2007-09-19 11:58:04,304 [1] DEBUG NHibernate.Impl.SessionImpl - closing session
2007-09-19 11:58:04,304 [1] DEBUG NHibernate.Impl.BatcherImpl - running BatcherImpl.Dispose(true)
2007-09-19 11:58:04,306 [1] DEBUG NHibernate.Transaction.AdoTransaction - running AdoTransaction.Dispose()


Top
 Profile  
 
 Post subject: Are there any know bugs with collection of components?
PostPosted: Thu Sep 20, 2007 4:57 am 
Beginner
Beginner

Joined: Wed Aug 29, 2007 8:23 am
Posts: 31
Hello again.

I've run another tests and it looks like something isn't working well when you have a collection of components. Let me explain this better: i've noticed that when you have an object with something like:

<bag name="_celulas" access="field" table="MovimentoCelula" cascade="all">
<key column="IdMovimento" />
<composite-element class="SRA.MercadosOO.CelulaMercado,SRA.MercadosOO">
<property name="IdCamara" column="IdCamara" />
<property name="Celula" column="Celula" />
<property name="Fila" column="Fila" />
<property name="Piso" column="Piso" />
</composite-element>
</bag>

then the collection associated with the obejct will only be sent to the db when you perform a session.flush. I think this is wrong since i believe that calling save over the main object should also generate the code to persist the collection. again, the docs don't really say much about this cases (when you have a collection of composite-element) and they only say that using something like this is a "exotic scenario". I'm not really sure if this is such an exotic scenario since i think it's really normal to have an entity with a collection of components and i don'tbelieve that promoting the components to entities is the way to go here!

so, can anyone give me any advise on what i should do next?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 20, 2007 11:43 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
As far as I know, the only way to guarantee things get written to the database is:
  1. Call ISession.Flush()
  2. Call ITransaction.Commit()

When ISession.FlushMode is set to FlushMode.Auto, "The ISession is sometimes flushed before query execution in order to ensure that queries never return stale state. This is the default flush mode." according to the API doc.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 20, 2007 3:50 pm 
Beginner
Beginner

Joined: Wed Aug 29, 2007 8:23 am
Posts: 31
hello.

yes, you're correct. i'm still not getting some things though...


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.