• Post Reply Bookmark Topic Watch Topic
  • New Topic

Persist is not working if after it I detach the entity  RSS feed

 
David Acevedo
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a problem with my code, I am trying to insert some information in the database and after that detach my object.

Basically what I am doing is:



When I use that code, nothing is inserted in the database, if I remove the "em.detach(operacion)" line it is going to work.
How can I insert the information in the database and after that detach my object??
What am I doing wrong?

Thanks for in advance for your help
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try calling flush() before detaching?
 
David Acevedo
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Try calling flush() before detaching?


Thanks, It works now.
I have a doubt, I am doing that query 2 times every 5 seconds, in this conditions do you think "flush" can affect my application performance?

 
Timo Patuelli
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course it will affect the performance. If you have 1000 entities, you will access DB 1000 times within a single transaction.

Why do you do you detach explicitly?

After transaction is committed, entities are detached automatically. If you have 1000 entities, then there will be a single flush for all entities.

In cases more complicated that in your example there can be several flushes. But still not a flush per entity.

Try not to flush explicitly.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!