Loading a jpa object from an external jar file

I have a project where I simulated a project and packaged the module into a jar file, which is included in the main project when we create a war and deploy it. The problem I am facing is that I have an Entity present in a module that does not load when the JPA Container EntityManagerFactory for unitName is created at startup time.

The main question I have is not looking for the EntityManager in the persistence.xml file, and then loading the specified properties and then scanning all the packages for @Entity annotation?

Any insight into how this works and how I can solve it would be great.

I found this link and mentions the creation of separate persistenceUnits constants, but here I do not need a separate persistence unit. I just need this module to go back to the parent project and load the object and any other @Resource, @Component classes that it executes because of context: checking the components and configuring the annotation.

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> 

Defining EnitityManagerFactory to launch 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 that has the 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 object is in a different package than the parent, because it is a separate module on its own.

The object that is being loaded 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> { 
+6
source share
3 answers

To scan objects in the bank, you must include it in the persistence.xml file. <jar-file>packedEntity.jar</jar-file> .

If you want to load a block from a package, you can try direct injection of it from a can. @PersistenceContext(unitName = "../packedEntity.jar#main")

I haven’t tried, but you can enable automatic sleep detection for objects <property name="hibernate.archive.autodetection" value="class, hbm"/>

+4
source

At work, we do not use JPA, but Hibernate, and our project is fully modularized. We process about 30 countries that do not use the same modules, and we can load all these module objects into a factory session by simply changing the maven dependencies (without changing the orm file)

The solution used is to use Java SPI. ServiceLoader will look for classes that implement the main EntityProvider interface. Each EntityProvider of each module will hard-code the entities it brings, and return a list of entities when calling ServiceLoader.load (EntityProvider.class). Thus, in the core of the platform, we just need to create a spring factory session in which we provide a list of factory bean entities that will invoke the SPI.

I don’t know if this will be easy with JPA, but I’m sure it is possible, but you may need to manually create EMF from the context of spring, as well as PersistenceUnit, I think ... That you may need a unit of persistence, which is a "union "persistence units of all your modules (at least for your objects) Take a look at the SPI as well as the javax.persistence.spi package.

Check also how to create EMF with Spring: AbstractEntityManagerFactoryBean

Edit: you can check this: using MergingPersistenceUnitManager to load an object

+1
source

if you use spring boot use object scan annotation in your main springboot class

 @EntityScan( basePackageClasses = {externalpackage.classname.class} ) 
0
source

Source: https://habr.com/ru/post/909666/


All Articles