Hi Bruce,
Since you're outside of a container, by default you're using an extended
scope persistence context, not transaction scope. In an extended scope the
entities returned by find are managed, and will be persisted when you commit
the transaction.
Here are the relevant snippets from the spec :
Section 3.1.1 :
The find and getReference methods are not required to be invoked within a
transaction context. If
an entity manager with transaction-scoped persistence context is in use, the
resulting entities will be
detached; if an entity manager with an extended persistence context is used,
they will be managed. See
section 3.3 for entity manager use outside a transaction.
Section 5.7:
When an application-managed entity manager is used, the application
interacts directly with the persistence
provider's entity manager factory to manage the entity manager lifecycle and
to obtain and destroy
persistence contexts.
All such application-managed persistence contexts are extended in scope, and
may span multiple transactions.
So the call to clear is in fact required, for extended scope persistence
contexts.
Hope this helps,
-Mike
On 10/8/07, Bruce Beaumont <[EMAIL PROTECTED]> wrote:
>
> Hi
>
> I am using OpenJPA outside a container.
>
> I have found that if I do a select, update a field of the returned object
> and
> then do a transaction the state of the object is persisted to the
> database.
> My reading of chapter 7.3.1 (3rd chapter) indicates that this is behavior
> is
> incorrect as each read outside a transaction should return a detached
> object
> and that any updates to a detached object should not be persisted.
>
> Here is a small test application
>
> EntityManagerFactory emf =
> Persistence.createEntityManagerFactory("BRUCE-DEV");
> EntityManager em = emf.createEntityManager();
>
> (1) SimpleBean sb = em.find(SimpleBean.class, 2);
>
> log.debug("Bean 1 - " + sb);
> (2) sb.setDescription("Run 6");
>
> (4)// em.clear();
> EntityTransaction et = em.getTransaction();
> et.begin();
> (3) et.commit();
>
>
> According to my reading of the manual the bean (1) is outside a
> transaction
> and thus is detached (Which is what I would like) however the table is
> updates with the value set in (2) when the commit of the transaction
> happens
> in (3).
>
> If you put in the clear (4) you do get the correct operation but to my
> understanding this should not be required.
>
>
>
> My config file is as followes
> <persistence-unit name="BRUCE-DEV" transaction-type="RESOURCE_LOCAL">
>
> <provider>org.apache.openjpa.persistence.PersistenceProviderImpl
> </provider>
>
> <class>dao.SimpleBean</class>
>
> <properties>
> <property name="openjpa.ConnectionURL"
>
> value="jdbc:informix-sqli://10.221.9.8:52001/bruce:informixserver=moatslxd;INFORMIXCONRETRY=3;INFORMIXCONTIME=20"/>
> <property name="openjpa.ConnectionDriverName"
> value="com.informix.jdbc.IfxDriver"/>
> <property name="openjpa.ConnectionUserName" value="jdbc"/>
> <property name="openjpa.ConnectionPassword" value="[EMAIL
> PROTECTED]@db0nly"/>
> <property name="openjpa.Log" value="SQL=TRACE"/>
> </properties>
> </persistence-unit>
>
>
> By bean is defined as followes
> @Entity(name="simple")
> @Table(name="simple")
> public class SimpleBean {
>
> @Id
> @Column(name="s_id", nullable=false)
> private long id = 0;
>
> @Basic
> @Column(name="s_key", length=10, nullable=true)
> private String key = null;
>
> @Basic
> @Column(name="s_desc", length=30, nullable=true)
> private String description = null;
>
> @Basic
> @Column(name="s_decimal", scale=14, precision=2, nullable=true)
> public BigDecimal dec = null;
>
> @Basic
> @Column(name="s_date", nullable=true)
> public Calendar date = null;
>
> @Basic
> @Column(name="s_dt", nullable=true)
> public Calendar dt = null;
> }
>
>
>
> Thanks for any guidance
>
> Bruce
>