• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hibernate session: to close or not to close

 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i've been struggling with this for hours

as it is, the test runs ok:


and runs ok in either cases: closing or not session at the end of persistence method:

now lets comment these two lines:

well, i get the error: this probably deals with database identity versus object identity

ok

my reasoning is the one i tried to say in comments: problem is tied with the rithm of open-closing of session, except when an object is loaded from db!

solutions:
- uncomment again and use it instead o previous code, using it as a barrier
- not closing session in persistence classes
- closing session in persistence classes and
- using s.clear()
or
- HibernateUtil.closeSession() followed by HibernateUtil.getSession()

speaking franlky: i dont get it
[ March 27, 2005: Message edited by: miguel lisboa ]
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not quite clear on exactly what happens since the comments mention a couple errors that occur, yet you've said "the test runs ok." Is NonUniqueObjectException the only exception that is ever thrown in this test, and only when you comment out the two lines mentioned?

When that exception is thrown (during saveOrUpdate() call, I assume), the transaction isn't rolled back and the session isn't closed. You need to use finally blocks as with the example you posted in your other message.I'm not very familiar with HibernateUtil; does getSession() open a session if there's none opened? How does this differ from openSession()?

now lets comment these two lines:

I believe the problem here is that both ent and convencao are given the same contrato to manage using adiciona(contrato). Assuming this adds contrato to a collection of associated entities, check your cascade attributes on the associations. Does Entidade or Convencao own Contrato?

Others with more Hibernate experience may be able to help you with more information. Can you post the relavent parts of your mappings?
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi again, David!
let me tell you this first: i got quite absorved with this matter, so i'll catch later on our other posts, ok?
of course most of it i dont need, but that's what i'm using

Entidade has: Set cotratos=new HashSet(); and Convencao also does the same

and Convencao.hbm:

all these problems arise when i start closing sessions in Dao; the remaining was debug... becaue before closing sessions all tests run ok!
i'm puzzled with it

David: thanks a lot for your interest

[ March 27, 2005: Message edited by: miguel lisboa ]
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
LOL, we all get caught up in various things (sometimes even work!) -- no worries.

Okay, I see that HibernateUtil is using thread-bound sessions and transactions, so it works pretty much as I described my usage via Spring.

From your mappings, it looks like Contrato represents a many-to-many association between Entidade and Convencao, is that correct? I think in this case you shouldn't use all-delete-orphan; which side owns the Contrato? If you delete the Convencao, I'm not sure it will remove the Contrato from the Entidade that owns it if it's loaded, but I'm not sure.

Do you have the Hibernate in Action book? I'll see if I can see something related in there. I would recommend at this point to simplify your code to try to narrow the problem down. Yes, it happens when you close the session, but how much code can you remove and still cause the error when closing the session?
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no, not many-to-many, i think; imagine i'v a clinic and you have an insurance company; we both have to write down how much you'll pay for every type ofsurgery i'll realize: here you have Entidade (me), Contrato(what is written) and Convencao (you).

So both you and me need a collection of contracts; in my app there are several me's and several you's, with diferent prices for the same medical intervention

you'r right when you raise the problem of deletion

if i want to delete Convencao what happens to Contratos.ok good question; i'll write a persistence test to see.

so far i've only this test:
and at the time it tranquilized me

gonna write that test
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by miguel lisboa:
imagine i'v a clinic and you have an insurance company; we both have to write down how much you'll pay for every type of surgery i'll realize: here you have Entidade (me), Contrato (what is written) and Convencao (you).

Yes, this is a standard many-to-many relationship. An Entidade can have Contratos with many Convencaos, and any Convencao can have Contratos with many Entidades.

But looking at the Contrato itself, each one relates to exactly one Entidade and one Convencao. This is why it doesn't appear to be many-to-many. Because you need to assign attributes to each association (Contrato), it becomes two one-to-many relationships.

As for deletion, given that each Contrato has references to its "owners," you should be able to chase down the references and remove the Contrato from both sides of the relationship. But as I said, Hibernate may already do this for you, so your idea to write a test is a good one.
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
good news!
test passes:

do you trust it?

i simply made, in Convencao.hbm.xml cascade default (deleted it)


edit:
posted before i saw your post
[ March 27, 2005: Message edited by: miguel lisboa ]
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by miguel lisboa:
i simply made, in Convencao.hbm.xml cascade default (deleted it)

You left Entidade.hbm.xml as delete-orphan, correct? This way, the Entidade owns the Contrato while Convencao merely references a Set of them. That seems reasonable to me.
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I would recommend at this point to simplify your code to try to narrow the problem down. Yes, it happens when you close the session, but how much code can you remove and still cause the error when closing the session?


i guess i've the minimum possible ... but
i'll sleep on it
(and i deserve it )
 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
since the very moment i set cascade as default in Convenvao.hbm .... no more errors!

anyway i think error was due to the existence of a diferent session, which would forbid me of inserting a new object in the same line with the very same id
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic