Hibernate 4.3.6 and Glassfish 4.0 JPA 2.1 object is not an instance of declaration class

I am using Hibernate 4.3.6 and Glassfish 4.0 for my ejb project.

My Dao test class:

@PersistenceContext
private EntityManager entityManager;

public void saveTest(){
    Foo testFoo = new Foo();
    testFoo.setSomething("test");
    entityManager.persist(testFoo);
    entityManager.flush();
}

      

and the POJO class Foo.class:

@Entity
@Table(name = "FOO")
public class Foo implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;

    private String something;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "T_ID", unique = true, nullable = false, precision = 15, scale = 0)
    public Long getId() {
        return id;
    }

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

    @Column(name = "T_SOMETHING", length = 50)
    public String getSomething() {
        return something;
    }

    public void setSomething(String something) {
        this.adi = something;
    }
}

      

persistence.xml:

 <?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
    http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
    version="2.1">
    <persistence-unit name="TestAppUnit" transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

        <jta-data-source>jdbc/TestApp</jta-data-source>

        <class>com.example.test.Foo</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.transaction.jta.platform"
                value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9iDialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
        </properties>
    </persistence-unit>
</persistence>

      

I can list the table data, get the data with a query, and I can delete the data from the table. But I cannot continue or merge.

An exception:

 IllegalArgumentException occurred calling getter of com.example.test.Foo.id
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:192)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:346)
    at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4746)
    at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4465)
    at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:243)
    at org.hibernate.event.internal.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:511)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:100)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
    at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:684)
    at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:676)
    at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:671)
....

Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:169)

      

Where am I doing wrong?

This project is working on Glassfish 3.1 with persistence.xml version 1.0 (jpa) and without this line:

<property name="hibernate.transaction.jta.platform"
                    value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform" />

      

thanks in advance

+3


source to share


1 answer


I found him. Perhaps others are facing this problem. I wanted to share a solution.

The problem is caused by Hibernate annotation and @PersistenceContext.



I am migrating to Hibernate version in 4.3.5 and the issue is resolved . Hibernation 4.3.6 and 4.3.7 have the same problem. This is caused by different class loaders. Ejb Classloader and Classloader web app are different.

+3


source







All Articles