Hi it looks like I've found a bug where if you have an entity with a 
Calendar field, and you set the value to null (from a non-null value) and 
you use entityManager.merge() to store the state back to the database, 
nothing happens.

Not this only happens if you call merge(), if you set it to null while the 
entity is retrieved and edited within a transaction it works as expected.

The following is a simple test case which illustrates the anomalie (and it 
working when merge is not used).

Anyone else see something like this?


The following assumes jdk1.6, openJpa 1.1-snapshot, junit 4, mysql 5, maven2.

create_tables.sql : the DDL used
Foo.java : the entity
FooTest.java : junit test for the entity

------------
FooTest.java
------------
package foo;

import static org.junit.Assert.*;

public class FooTest
{
        private EntityManagerFactory emf = 
Persistence.createEntityManagerFactory("persistenceUnit");

        @Test
        public void fooTest()
        {
                // --- create with non-null date ---
                Foo foo = new Foo();
                foo.setDate(Calendar.getInstance());

                EntityManager em = emf.createEntityManager();

                try
                {
                        EntityTransaction tx = em.getTransaction();
                        tx.begin();

                        em.persist(foo);

                        tx.commit();
                }
                finally
                {
                        em.close();
                }
                
                assertNotNull(foo.getDate());
                
                // --- find expecting a non-null date ---
                
                em = emf.createEntityManager();
                
                try
                {
                        EntityTransaction tx = em.getTransaction();
                        tx.begin();

                        foo=em.find(Foo.class, foo.getId());

                        tx.commit();
                }
                finally
                {
                        em.close();
                }
                
                assertNotNull(foo.getDate());
                
                // --- updating to a null date ---
                
                em = emf.createEntityManager();
                
                try
                {
                        EntityTransaction tx = em.getTransaction();
                        tx.begin();
                        
//                      foo=em.find(Foo.class, foo.getId());
                        foo.setDate(null);
                        em.merge(foo);
                        
                        tx.commit();
                }
                finally
                {
                        em.close();
                }
                
                assertNull(foo.getDate());
                
                // --- find expecting a null date ---
                
                em = emf.createEntityManager();
                
                try
                {
                        EntityTransaction tx = em.getTransaction();
                        tx.begin();

                        foo=em.find(Foo.class, foo.getId());

                        tx.commit();
                }
                finally
                {
                        em.close();
                }
                
                assertNull(foo.getDate());
        }
        

}

--------
Foo.java
--------
package foo;

import java.util.Calendar;

@Entity
public class Foo
{
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private int id = 0;

        @Temporal(TemporalType.TIMESTAMP)
        private Calendar date = null;

        public int getId()
        {
                return id;
        }

        public void setId(int id)
        {
                this.id = id;
        }

        public Calendar getDate()
    {
        return date;
    }

        public void setDate(Calendar date)
    {
        this.date = date;
    }

        
}

-----------------
create_tables.sql
-----------------
create table Foo (id int primary key auto_increment, date datetime);

---------------
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>

-------
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]

I can speak Canadian, American, Australian, and little English.

Reply via email to