openjpa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sara H Waggoner <swag...@us.ibm.com>
Subject Can't null out entity attributes when openjpa.DetachState set to loaded
Date Mon, 14 Apr 2008 13:52:35 GMT
Hi,

I have some code that creates an entity, sets all attributes to non-null 
values, persists the entity, then sets all attributes to  null and merges 
the entity.  When I run this code, the persist works fine but the merge 
does not - the attributes that were nulled out in the merged entity still 
exist with their original values in the database record.

Here is the code:


persistence.xml:

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">

  <persistence-unit name="mss">

 
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
    <class>net.iss.mss.jparef.services.entitynulls.Item</class>
    <class>net.iss.mss.jparef.services.entitynulls.Photo</class>

    <properties>
      <property name="openjpa.DetachState" value="loaded"/>
      <property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO, 
SQL=TRACE"/>
      <property name="openjpa.DataCache" value="true"/>
      <property name="openjpa.RemoteCommitProvider" value="sjvm"/>
    </properties>

  </persistence-unit>
</persistence>


Item.java:

package net.iss.mss.jparef.services.entitynulls;

import java.io.*;
import javax.persistence.*;


@Entity
@Table(name = "item")
public class Item implements Serializable
{
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "item_id")
  private int id;

  @Column(name="title")
  private String title;
 
  @OneToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, 
CascadeType.MERGE, CascadeType.REFRESH, CascadeType.REMOVE})
  @JoinColumn(name="photo_id", referencedColumnName="photo_id")
  private Photo photo;


  public int getId ()
  {
    return id;
  }

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

  public String getTitle ()
  {
    return title;
  }

  public void setTitle (String title)
  {
    this.title = title;
  }

  public Photo getPhoto ()
  {
    return photo;
  }

  public void setPhoto (Photo photo)
  {
    this.photo = photo;
  }
}


Photo.java

package net.iss.mss.jparef.services.entitynulls;

import java.io.*;
import javax.persistence.*;


@SuppressWarnings({"JpaDataSourceORMInspection"})
@Entity
@Table(name = "photo")
public class Photo implements Serializable
{
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "photo_id")
  private int id;

  @Column(name = "bytes")
  @Lob
  private Byte[] bytes;

  @OneToOne(mappedBy = "photo")
  private Item item;

  public int getId ()
  {
    return id;
  }

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

  public Byte[] getBytes ()
  {
    return bytes;
  }

  public void setBytes (Byte[] bytes)
  {
    this.bytes = bytes;
  }

  public Item getItem ()
  {
    return item;
  }

  public void setItem (Item item)
  {
    this.item = item;
  }
}


TestNulls.java

import java.sql.Connection;
import javax.persistence.*;
import junit.framework.*;
import org.apache.openjpa.persistence.*;
import net.iss.mss.jparef.services.entitynulls.*;
import net.iss.mss.jpa.*;


public class TestNulls extends TestCase
{
 
  private JpaContext context = new JpaContext 
("spring-artMuseumServices.xml");

  public void test () throws Exception
  {
 
    EntityManager em = null;

    try
    {
      // Create new item, set properties and object reference (photo), and 
persist

      em = context.getEmf().createEntityManager ();

      Item item = new Item();
      item.setTitle("Title 1");

      Byte[] bytes = {1, 2, 3};
      Photo photo = new Photo();
      photo.setBytes(bytes);
      item.setPhoto(photo);
      photo.setItem(item);

      em.getTransaction().begin();
      em.persist(item);
      em.getTransaction().commit();

      em.close ();


      // Null out item properties and object reference, then merge

      em = context.getEmf().createEntityManager ();

      item.setTitle(null);
      item.setPhoto(null);
      photo.setItem(null);

      em.getTransaction().begin();
      em.merge(item);
      em.getTransaction().commit();

      em.close ();
    }
    catch (Exception e)
    {
      throw e;
    }
    finally
    {
      try {
        em.close ();
        context.close ();
      }
      catch (Exception e)
      {
         //Ignore 
      }
    }
  }
}


Thanks,
Sara
Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message