错误org.hibernate.InstantiationException:无法实例化实体

时间:2014-10-21 00:37:41

标签: hibernate jsf jpa glassfish

我有以下设置:

  • GlassFish server 4.1
  • Hibernate 4.3.6.Final
  • JPA 2.0

应用程序通常部署在服务器上,表格已正确创建和填充。当我打开用JavaServer Faces编写的Web应用程序时,会发生错误。

当我尝试获取实体时,我得到了异常

  

javax.servlet.ServletException:org.hibernate.InstantiationException:无法实例化实体:: entity.Test

我在以下代码行em.find(Test.class, 1L);中遇到了异常。实际上下一行也不行。这是原始代码,但我创建了一个简单的测试用例来显示问题。

package service;

import java.io.Serializable;
import java.util.List;
import java.util.logging.Logger;

import javax.faces.bean.SessionScoped;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import entity.Test;
import entity.WeatherInfo;

@Named
@SessionScoped
public class WeatherInfoEntryBean implements Serializable{

    @PersistenceContext
    EntityManager em;

    /**
     * @return the blogEntry
     */
    public List<WeatherInfo> getEntities() {
        em.find(Test.class, 1L);
        List<WeatherInfo> weatherInfos = em.createQuery("SELECT w FROM WeatherInfo w").getResultList();
        return weatherInfos;
    }
}

测试实体非常简单

package entity;

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

@Entity
@Table(name = "test")
public class Test implements Serializable {
    private Long id;
    private String url;

    public Test() {
        super();
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {

        return id;
    }

    @Column(name = "url")
    public String getUrl() {

        return url;
    }

    public void setId(Long id) {

        this.id = id;
    }

    public void setUrl(String url) {

        this.url = url;
    }
}
根据JPA文档,

persistence.xml文件看起来很正常:

<persistence version="2.0"
    xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="weatherTaskPersistenceUnit" transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

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

        <class>entity.WeatherInfo</class>
        <class>entity.CityInfo</class>
        <class>entity.Api</class>
        <class>entity.Test</class>

        <exclude-unlisted-classes>true</exclude-unlisted-classes>

        <properties>
            <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"/>
            <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
            <property name="javax.persistence.schema-generation.create-source" value="metadata"/>
            <property name="javax.persistence.sql-load-script-source" value="META-INF/data.sql"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>
        </properties>
    </persistence-unit>
</persistence>

这个设置应该有效吗?我究竟做错了什么?这里有没有遗失的信息?

1 个答案:

答案 0 :(得分:0)

我只是猜测问题是Test类的构造函数中的super()调用。

此调用没有任何意义,因为您只是展开Object