Você está na página 1de 3

Username:

Password (Reset):

Login

Register

Members

Search...

Find

Home
Why Seam?

Community > Forums > Seam Users > entityManager persist with manual ...

Seam 2
Getting Started Downloads Documentation

Forum: Seam Users


entityManager persist with manual flush mode
Hello, I have a simple question:

Forum List

Topic List

13. Aug 2009, 17:45 America/New_York | Link Juan Chiaradia

Seam 3
Contribute Downloads Documentation Compatibility Tooling Solder Catch Module Config Module Cron Module Drools Module Faces Module Errai CDI International Module JCR Module JMS Module Mail Module Persistence Module Remoting Module Reports Module REST Module Security Module Servlet Module Social Module Spring Module Validation Module Wicket Module Module Handbook

Is there any way to do entityManager.persist(myEntity) and not automatically persist the entity to the database? I have this code, but isn't working as I spec to:
Conversation.instance().begin(true,false); Conversation.instance().changeFlushMode(FlushModeType.MANUAL); entityManager.persist(entity);

It automatically persist the entity and doesn't wait for entity.flush.


Tags: entityManager | hibernate

9 Replies:

Re: entityManager persist with manual flush mode


What I really need is something like this:
entityManager.getTransaction().begin(); entityManager.persist(object1); entityManager.persist(object2); entityManager.getTransaction().commit();

13. Aug 2009, 19:31 America/Ne J

Could it be done? My first approach was to make entityManager work in MANUAL flush mode, so I could do something like this:
Conversation.instance().begin(true,false); Conversation.instance().changeFlushMode(FlushModeType.MANUAL); ... entityManager.persist(object1); ... entityManager.persist(object2); ... entityManager.flush(); Conversation.instance().end;

Weld
Downloads Documentation Development

Community
Forums CDI Extensions Contribute JIRA Mailing Lists Events Links Seam In Production

With this approach, I spec that when I do entityManager.flush(); it automatically start a transaction, flush all changes to da commit the transaction if every thing was ok. But the problem it doesn't work like I suppose to do, entityManager.persist(ob persisted automatically to the database and don't wait for entity.flush(). Any help? Regards, Chiara

Documentation
FAQs Knowledge Base Design

Re: entityManager persist with manual flush mode


How are you getting the entityManager reference? @In EntityManager entityManager?

13. Aug 2009, 21:05 America/Ne Bra

Built with Seam


You can find the full source code for this website in the Seam package in the directory /examples/wiki. It is licensed under the LGPL.

There are other ways to get a reference to the entityManager that will cause behavior similar to what you talking about.
13. Aug 2009, 21:56 America/Ne J

Re: entityManager persist with manual flush mode


Hello, I am using:

Online: 11 Members of 12020

protected EntityManager entityManager = (EntityManager) Component.getInstance("entityManager");.

I cannot use @In because It need to be marked as @BypassInterceptors Here is Why I'm using this approach: http://seamframework.org/Community/JavaBeanInterceptorAndObjectInternalState

Re: entityManager persist with manual flush mode

14. Aug 2009, 00:20 America/New_York | Arbi Sooka

In main(), the insert commits, so I was able to reproduce your problem. Likely due to using @Id with @GeneratedValue in y class. The 2nd example below (update) only commits if I add the em.flush().

@Name("conversationAPI") @Scope(ScopeType.CONVERSATION) public class TestConversationAPI { @Logger Log log; @Transactional public void main() { EntityManager entityManager = (EntityManager) Component.getInstance("em"); Conversation.instance().begin(true,false); Conversation.instance().changeFlushMode(FlushModeType.MANUAL); Hotel hotel = new Hotel(); hotel.setName("fooname"); hotel.setAddress("fooaddress"); hotel.setCity("foocity"); hotel.setState("foostate"); hotel.setZip("foozip"); hotel.setCountry("USA"); entityManager.persist(hotel); //entityManager.flush(); Session session = (Session) entityManager.getDelegate(); log.info("session.getFlushMode() = "+session.getFlushMode()); } @Transactional public void main2() { EntityManager entityManager = (EntityManager) Component.getInstance("em"); Conversation.instance().begin(true,false); Conversation.instance().changeFlushMode(FlushModeType.MANUAL); Hotel hotel = entityManager.find(Hotel.class, new Long(1)); hotel.setAddress("1234 Arbi St."); //entityManager.flush(); Session session = (Session) entityManager.getDelegate(); log.info("session.getFlushMode() = "+session.getFlushMode()); }

I'm learning Objective-C, it's about time I learned an OO language other than Java!

Re: entityManager persist with manual flush mode


Arbi,

14. Aug 2009, 16:44 America/Ne J

This is exactly me problem, another thing to add to it is that if you are using EntityHome and you perform an update before entityManager.flush, it will flush the update to db.

@Name("conversationAPI") @Scope(ScopeType.CONVERSATION) public class TestConversationAPI { @Logger Log log; @Transactional public void main3() { EntityManager entityManager = (EntityManager) Component.getInstance("em"); HotelEntityHome hotelEntityHome = (HotelEntityHome ) Component.getInstance("hotelEntit Conversation.instance().begin(true,false); Conversation.instance().changeFlushMode(FlushModeType.MANUAL);

hotelEntityHome.setId((long) 1); hotelEntityHome.getInstances().setName("Name Changed"); hotelEntityHome.update(); Session session = (Session) entityManager.getDelegate(); log.info("session.getFlushMode() = "+session.getFlushMode()); } }

Re: entityManager persist with manual flush mode


Do you have any solution to this behavior? No sorry, I do not use the SAF. Regards, Chiara
I'm learning Objective-C, it's about time I learned an OO language other than Java!

14. Aug 2009, 17:23 America/New_York | Arbi Sooka

Re: entityManager persist with manual flush mode


Arbi,

14. Aug 2009, 18:34 America/Ne J

I'm not very interested in SAF, it was just another similar behavior that I found, but for the initial problem: auto flush of entityManager.persist(object) with flush mode MANUAL, do you have any solution? anyone else that could help us? Regards, Chiara

Re: entityManager persist with manual flush mode


Like I said before:

15. Aug 2009, 01:30 America/New_York | Arbi Sooka

are you using @Id with @GeneratedValue (IDENTITY, not sequences) in your entity class(es)? An auto-generated identity c cause a commit even if you don't call flush() manually. This is covered in SiA book.
I'm learning Objective-C, it's about time I learned an OO language other than Java! Rating:

Re: entityManager persist with manual flush mode


Yes! I was using:
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)

18. Aug 2009, 16:00 America/Ne J

I will change it and try again. Thanks a lot! PD: Sory for the late reply, I am from Argentina and yesterday was a holiday.

Copyright 2009, Red Hat Middleware, LLC. All rights reserved. JBoss and Seam are registered trademarks and servicemarks of Red Hat, Inc. [Privacy Policy]

Você também pode gostar