Hibernate 4.3.6和Glassfish 4.0 JPA 2.1对象不是声明类的实例

时间:2014-10-30 15:56:45

标签: java hibernate jpa glassfish-4

我在我的ejb项目中使用Hibernate 4.3.6和Glassfish 4.0。

我的测试Dao课程:

@PersistenceContext
private EntityManager entityManager;

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

和POJO类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>

我可以列出表数据,使用查询获取数据,我可以从表中删除数据。但我不能坚持或合并。

例外是:

 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)

我在哪里做错了?

这个项目在Glassfish 3.1上使用persistence.xml版本1.0(jpa)而没有这行:

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

提前致谢

1 个答案:

答案 0 :(得分:3)

我找到了。也许其他人遇到这个问题。我想分享解决方案。

由Hibernate和“@PersistenceContext”注释引起的问题。

我将Hibernate版本更改为4.3.5并解决了问题。 Hibernate 4.3.6和4.3.7有同样的问题。它是由不同的类加载器引起的。 Ejb Classloader和web app Classloader是不同的。