Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading JPA Entity from external Jar file

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> {
like image 400
Hrishikesh Avatar asked Feb 29 '12 18:02

Hrishikesh


2 Answers

if you are using spring boot use the entity scan annotation in your main springboot class

@EntityScan(
        basePackageClasses = {externalpackage.classname.class}
)
like image 67
Mohammed Rafeeq Avatar answered Oct 10 '22 08:10

Mohammed Rafeeq


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"/>

like image 26
Nayan Wadekar Avatar answered Oct 10 '22 07:10

Nayan Wadekar