简体   繁体   中英

JPA: stateless session beans and managed entities

Lets assume I have an JPA Entity called Foo with a String myStr property;

When I now define a stateless session bean like this:

@Stateless
@LocalBean
public class FooBean {

    @PersistenceContext(unitName="foo-pu")
    private EntityManager em;

    public Foo getFoo(int id) {
         return em.find(Foo.class,id);
    }

    public void changeMyStr(Foo entity) {
         entity.setMyStr("fooStr");
    }

}

Will the changes made in changeMyStr() become persisted if I pass a previously acquired Foo object through getFoo() ?

As long as your calls to getFoo(...) and changeMyStr(...) are made inside the same transaction, changes to the Foo instance should become persisted, if that transaction was commited. Entities typically become detached once you leave the transaction, so if you invoke getFoo(...) in one transaction and changeMyStr(...) in another, changes made inside changeMyStr(...) (and any other changes made outside the original transaction) will not be persisted.

If you use an extended persistence context , entities will not become detached when leaving the original transaction, but that is a rather unusual case. Read this article or this one for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM