Hibernate的JPA类加载问题 [英] JPA class loading issue with Hibernate

查看:104
本文介绍了Hibernate的JPA类加载问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试从数据库中获取条目时,从EntityManager返回的类型不同于我的代码中指定的类型.它们都是com.mycompany.Foo,但是已经被两个不同的类加载器实例化了.我发现第二种类型的类加载器的存储库设置为WEB-INF/classes/,而jarPath设置为/WEB-INF/lib. 所有这些都使用JPA和Hibernate进行设置.

When I am attempting to fetch an entry from my database, the type returned from the EntityManager differs from the type specified in my code. They are both com.mycompany.Foo, but they have been instanciated with two different class loaders. I have found out that the class loader for the second type has repositories set to WEB-INF/classes/ with jarPath set to /WEB-INF/lib. This is all set up using JPA with Hibernate.

persistence.xml:

persistence.xml:

<persistence 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"
             version="2.0">
    <persistence-unit name="manager1" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>devResource</jta-data-source>
        <validation-mode>CALLBACK</validation-mode>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
            <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform" />
        </properties>
    </persistence-unit>
</persistence>

build.gradle具有依赖项:

build.gradle with dependencies:

group 'testApplication'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'war'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compile 'javax:javaee-api:7.0'
    compile 'org.hibernate:hibernate-entitymanager:4.3.5.Final'
    compile 'org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.0.Final'
    compile 'mysql:mysql-connector-java:5.1.36'
}

获取实体的类:

@Stateless
public class FooService {

    @PersistenceContext
    private EntityManager em;

    public Foo getFooEntity(final int id) {
        // Will throw a CastException since Foo is not Foo (loaded through different class loaders)
        return em.find(Foo.class, id);
    }
}

我正在使用IntelliJ(爆炸的Web应用程序)进行编译,这可能与我的部署设置有关吗?目前,Gradle正在通过war插件定义它们.

I am compiling this using IntelliJ (Exploded web application), could it be an issue with my deployment settings? They are currently being defined by Gradle through the war plugin.

推荐答案

问题是加载用于扫描带注释的@Entity类的库的库不在我的域的lib文件夹中.将项目的依赖项移到该文件夹​​中解决了我的问题.

The issue was that the libraries that loads the libraries which scans for annotated @Entity classes is not in the lib folder for my domain. Moving the dependencies for my project to this folder fixed my issue.

这篇关于Hibernate的JPA类加载问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆