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 IllegalArgumentException when openjpa.DetachState set to "all" or " fetch-groups(DetachedStateField=true)"
Date Mon, 14 Apr 2008 14:18:43 GMT
Hi,

I have some code that creates and persists an entity.  When I run the 
code, it inserts records into the database as expected, but throws the 
following error when the entity manager closes.  This exception is only 
thrown if I have 
openjpa.DetachState set to "all" or 
"fetch-groups(DetachedStateField=true)".  When I have openjpa.DetachState 
set to "loaded, it runs to completion with no exceptions.

java.lang.IllegalArgumentException
        at 
sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:63)
        at java.lang.reflect.Field.set(Field.java:656)
        at org.apache.openjpa.enhance.Reflection.set(Reflection.java:447)
        at 
org.apache.openjpa.enhance.ReflectingPersistenceCapable.setValue(ReflectingPersistenceCapable.java:350)
        at 
org.apache.openjpa.enhance.ReflectingPersistenceCapable.pcReplaceField(ReflectingPersistenceCapable.java:155)
        at 
org.apache.openjpa.kernel.StateManagerImpl.replaceField(StateManagerImpl.java:2990)
        at 
org.apache.openjpa.kernel.DetachManager$InstanceDetachFieldManager.storeObjectField(DetachManager.java:673)
        at 
org.apache.openjpa.kernel.StateManagerImpl.providedObjectField(StateManagerImpl.java:1989)
        at 
org.apache.openjpa.enhance.ReflectingPersistenceCapable.pcProvideField(ReflectingPersistenceCapable.java:114)
        at 
org.apache.openjpa.kernel.StateManagerImpl.provideField(StateManagerImpl.java:2978)
        at 
org.apache.openjpa.kernel.DetachManager$InstanceDetachFieldManager.detachField(DetachManager.java:618)
        at 
org.apache.openjpa.kernel.DetachManager$InstanceDetachFieldManager.detachFields(DetachManager.java:602)
        at 
org.apache.openjpa.kernel.DetachManager.detachInternal(DetachManager.java:448)
        at 
org.apache.openjpa.kernel.DetachManager.access$300(DetachManager.java:57)
        at 
org.apache.openjpa.kernel.DetachManager$InstanceDetachFieldManager.detachField(DetachManager.java:744)
        at 
org.apache.openjpa.kernel.DetachManager$InstanceDetachFieldManager.storeObjectField(DetachManager.java:672)
        at 
org.apache.openjpa.kernel.StateManagerImpl.providedObjectField(StateManagerImpl.java:1989)
        at 
org.apache.openjpa.enhance.ReflectingPersistenceCapable.pcProvideField(ReflectingPersistenceCapable.java:114)
        at 
org.apache.openjpa.kernel.StateManagerImpl.provideField(StateManagerImpl.java:2978)
        at 
org.apache.openjpa.kernel.DetachManager$InstanceDetachFieldManager.detachField(DetachManager.java:618)
        at 
org.apache.openjpa.kernel.DetachManager$InstanceDetachFieldManager.detachFields(DetachManager.java:602)
        at 
org.apache.openjpa.kernel.DetachManager.detachInternal(DetachManager.java:448)
        at 
org.apache.openjpa.kernel.DetachManager.detachAll(DetachManager.java:310)
        at 
org.apache.openjpa.kernel.BrokerImpl.detachAllInternal(BrokerImpl.java:3147)
        at org.apache.openjpa.kernel.BrokerImpl.free(BrokerImpl.java:4035)
        at 
org.apache.openjpa.kernel.BrokerImpl.close(BrokerImpl.java:4022)
        at 
org.apache.openjpa.kernel.DelegatingBroker.close(DelegatingBroker.java:1282)
        at 
org.apache.openjpa.persistence.EntityManagerImpl.close(EntityManagerImpl.java:1002)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at 
org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:346)
        at $Proxy23.close(Unknown Source)
        at TestNulls.test(TestNulls.java:39)
        at TestNulls.testAll(TestNulls.java:77)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at 
com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:40)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at 
com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)


Here is the code:

DDL:

Create table item (
        item_id Int NOT NULL AUTO_INCREMENT,
        title Varchar(50),
        photo_id Int,
 Primary Key (item_id)) ENGINE = MyISAM;

Create table photo (
        photo_id Int NOT NULL AUTO_INCREMENT,
        bytes Blob NOT NULL,
 Primary Key (photo_id)) ENGINE = MyISAM;

Alter table item add Foreign Key (photo_id) references photo (photo_id) on 
delete  restrict on update  restrict;
Alter table item_detail add Foreign Key (item_id) references item 
(item_id) on delete  restrict on update  restrict;


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="fetch-groups(DetachedStateField=true)"/>
      <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