okay so the mail-list won't let me send the test case as an attachment so
here are the files individually :
-------------
Attached is a simple test that shows both @PreUpdate and @PostUpdate are
called even when nothing is done at all.
- It requires maven2.
- I used jdk1.6 (but not required, modify the pom.xml for 1.5)
- I used 1.10-SNAPSHOT of the OpenJPA
- I used Mysql (not required but you will need to modify create tables.sql
and persistence.xml)
To run it do the following:
- run the create_tables.sql file
- configure the persistence.xml to your system
- run do an mvn test or run the junit test in eclipse
what I get is the following :
@PrePersist
@PreUpdate
@PostUpdate
@PreUpdate
@PostUpdate
@PreUpdate
@PostUpdate
@PreUpdate
@PostUpdate
Note that it did a Pre/Post update for every entityManager.find() I did
even though I didn't modify anything.
------------
create_tables.sql
------------
create table Foo (id int primary key auto_increment, name varchar(128));
------------
Foo.java
------------
package foo;
import javax.persistence.Entity;
@Entity
public class Foo
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id = 0;
private String name=null;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
@PrePersist
public void prePersist()
{
System.err.println("@PrePersist");
}
@PreUpdate
public void preUpdate()
{
System.err.println("@PreUpdate");
}
@PostUpdate
public void postUpdate()
{
System.err.println("@PostUpdate");
}
}
------------
persistence.xml
------------
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="persistenceUnit"
transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>foo.Foo</class>
<properties>
<property name="openjpa.ConnectionURL"
value="jdbc:mysql://127.0.0.1:3306/test"
/>
<property name="openjpa.ConnectionDriverName"
value="com.mysql.jdbc.Driver" />
<property name="openjpa.ConnectionUserName"
value="tedman" />
<property name="openjpa.ConnectionPassword"
value="" />
<property name="openjpa.Log"
value="DefaultLevel=WARN, Tool=INFO" />
</properties>
</persistence-unit>
</persistence>
------------
FooTest.java
------------
package foo;
import javax.persistence.EntityManager;
public class FooTest
{
private EntityManagerFactory emf =
Persistence.createEntityManagerFactory("persistenceUnit");
@Test
public void fooTest()
{
// --- create ---
Foo foo = new Foo();
foo.setName("name:" + System.currentTimeMillis());
EntityManager em = emf.createEntityManager();
try
{
EntityTransaction tx = em.getTransaction();
tx.begin();
em.persist(foo);
tx.commit();
}
finally
{
em.close();
}
// --- find ---
find(foo.getId());
find(foo.getId());
find(foo.getId());
}
private void find(int id)
{
EntityManager em = emf.createEntityManager();
try
{
EntityTransaction tx = em.getTransaction();
tx.begin();
em.find(Foo.class, id);
tx.commit();
}
finally
{
em.close();
}
}
}
------------
pom.xml
------------
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<packaging>jar</packaging>
<version>0.0-SNAPSHOT</version>
<name>test</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>apache-snapshots</id>
<url>
http://people.apache.org/repo/m2-snapshot-repository
</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa</artifactId>
<version>1.1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
--
Ted Leung
[EMAIL PROTECTED]
It is easier to speak wisely than to act wisely.