一尘不染

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

hibernate

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

我的考试道课:

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

我在哪里做错了?

此项目在具有persistence.xml版本1.0(jpa)且没有此行的Glassfish 3.1上工作:

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

提前致谢


阅读 267

收藏
2020-06-20

共1个答案

一尘不染

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

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

我将Hibernate版本更改为4.3.5,问题已解决 。Hibernate 4.3.6和4.3.7有相同的问题。这是由不同的类加载器引起的。Ejb
Classloader和Web应用程序Classloader是不同的。

2020-06-20