从外部Jar文件加载JPA实体 [英] Loading JPA Entity from external Jar file

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

问题描述

我有一个项目设置,在这里我模块化了一个项目,并将一个模块打包成一个jar文件,当我们创建一个war并部署它时,它将包含在主项目中。
我面临的问题是,我有一个实体出现在模块中,该模块在启动期间构建unitName的JPA Container EntityManagerFactory时不加载。



我拥有的基本问题是不在Persistence.xml中查找EntityManager,然后加载指定的属性,然后扫描所有包以查找@Entity注释?

关于如何运作以及如何解决这个问题的任何见解都会很棒。



我找到了这个链接,它提到了创建单独的persistenceUnits,但是在这里我不需要单独的持久性单元。我只需要模块捎带父项目并加载实体和其他@Resource,@Component类,由于上下文:组件扫描和注释配置。

http:/ /javathoughts.capesugarbird.com/2009/02/jpa-and-multiple-persistence-units.html



这是我的代码/配置

 < bean id =entityManagerFactoryclass =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean> 
< property name =dataSourceref =dataSource/>
< property name =persistenceUnitNamevalue =LineManagement/>
< property name =jpaVendorAdapter>
< bean class =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter>
< property name =generateDdlvalue =false/>
< property name =showSqlvalue =false/>
< property name =databasePlatformref =cpsHibernateDialectClassName/>
< / bean>
< / property>
< property name =beanNamevalue =entityManager>< / property>
< / bean>

启动实体管理器的EnitityManagerFactory的定义。

 < persistence-unit name =LineManagementtransaction-type =RESOURCE_LOCAL> 
<属性>
< property name =hibernate.id.new_generator_mappingsvalue =true/>
< property name =hibernate.current_session_context_classvalue =thread/>
< property name =hibernate.default_batch_fetch_sizevalue =200/>

....



持久性。 xml定义了二级缓存和其他休眠属性。



然后,该模块拥有一个实体。

  import javax .persistence.Entity; 


$实际
@Table(name = IntegrationEvent.TABLE_NAME,uniqueConstraints = @UniqueConstraint(columnNames =INTGRTN_EVNT_QUEUE_SK))
@GenericGenerator(name =UUID_GEN ,strategy =org.hibernate.id.UUIDHexGenerator,parameters = {@Parameter(name =separator,value = - )})
public class IntegrationEvent implements Serializable {

....
}



注意:实体与父代不同,因为它本身是一个独立的模块。



在主项目中加载的实体。

  package com.parent.line.entity; 

import javax.persistence.Entity;

@Entity
@Table(name =ACCOUNT)
@Cacheable(true)
public class Account
实现LMLookupTypeEntityByDivision,Serializable,Comparable<帐户> {


解决方案

要扫描驻留在jar中的实体,您必须将其包含在persistence.xml中。
< jar-file> packedEntity.jar< / jar-file>



想要从包中加载单元,然后可以尝试直接从jar中注入它。
@PersistenceContext(unitName =../ packedEntity.jar#main)



Haven' t试过了,但是你可以为实体
< property name =hibernate.archive.autodetectionvalue =class,hbm/>

i have a project setup where i have modularized a project, and packaged a module into a jar file which gets included in the main project when we create a war and deploy it. The problem that i am facing is that, i have an Entity present in the module which does not load when the JPA Container EntityManagerFactory for the unitName is built during startup.

The basic question that i have is doesnt the EntityManager lookup at the persistence.xml and then load the properties that are specified, and then scan all the packages for @Entity annotation?

Any insight on how this works and how can i resolve this would be great.

I found this link and it mentions about creating seperate persistenceUnits, but here i dont need a seperate persistence unit. i just need the module to piggy back on the parent project and load the entity and any other @Resource, @Component classes, which it does due to the context:component scan and the annotation config.

http://javathoughts.capesugarbird.com/2009/02/jpa-and-multiple-persistence-units.html

Here is my code/configuration

<bean id="entityManagerFactory"  class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceUnitName" value="LineManagement" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="false" />
            <property name="showSql" value="false" />
            <property name="databasePlatform" ref="cpsHibernateDialectClassName" />
        </bean>
    </property>
    <property name="beanName" value="entityManager"></property>
</bean>

definition of the EnitityManagerFactory to startup the Entity Manager.

<persistence-unit name="LineManagement" transaction-type="RESOURCE_LOCAL">
    <properties>
        <property name="hibernate.id.new_generator_mappings" value="true" />
        <property name="hibernate.current_session_context_class" value="thread" />
        <property name="hibernate.default_batch_fetch_size" value="200" />

....

Persistence.xml which defines the second level cache and other hibernate properties.

Then, the module which has an entity.

import javax.persistence.Entity;


@Entity
@Table(name = IntegrationEvent.TABLE_NAME, uniqueConstraints =    @UniqueConstraint(columnNames = "INTGRTN_EVNT_QUEUE_SK"))
@GenericGenerator(name = "UUID_GEN", strategy = "org.hibernate.id.UUIDHexGenerator",     parameters = { @Parameter(name = "separator", value = "-") })
public class IntegrationEvent implements Serializable {

.... }

Note: the entity is in a different package than the parent, since it is a seperate module on its own.

The entity which loads fine in the main project.

package com.parent.line.entity;

import javax.persistence.Entity;

@Entity
@Table(name = "ACCOUNT")
@Cacheable(true)
public class Account
  implements LMLookupTypeEntityByDivision, Serializable, Comparable<Account> {

解决方案

To scan entities residing in jar, you have to include it in persistence.xml. <jar-file>packedEntity.jar</jar-file>.

If you want to load unit from the package, then you can try directly injecting it from jar. @PersistenceContext(unitName = "../packedEntity.jar#main")

Haven't tried but you can enable hibernate auto detection for entities <property name="hibernate.archive.autodetection" value="class, hbm"/>

这篇关于从外部Jar文件加载JPA实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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