JPA entity containing a class from an external jar

1.8k Views Asked by At

I'm building a Roo webapp using JPA and Hibernate. I have a library project that is referenced from the webapp. The Roo webapp has the following entity:

@RooJpaEntity
@RooJavaBean
public class UpdatePolicyHolder {

    @ElementCollection(targetClass=Policy.class)
    private List<Policy> policies;
    private String deviceId;
    private long timestamp;

}

The Policy class is defined in an external library, included as a jar file. When starting the application, Hibernate complains that it cannot determine the type of Policy:

Could not determine type for: com.company.policy.Policy, at table:...

I can't seem to find anything online about issues around included external classes. I haven't made any changes to the standard, Roo-generated persistence.xml file. I did modify the applicationContext.xml file to make sure the package for both the entity and the Policy class are covered by the component scan element.

The webapp project is packaged as a war and is currently run via a mvn jetty:run command. The jar containing Policy is included via a Maven dependency.

1

There are 1 best solutions below

2
On

The jar containing Policy must contain the META-INF/persistence.xml to let 3rd party apps to scan the entities inside that jar.

Then set Spring EntityManagerFactory for scanning persistent entities:

<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    id="entityManagerFactory">
  <property name="persistenceUnitName" 
      value="UNIT_NAME_OF_POLICY_PERSISTENCE_XML"/>
  <property name="dataSource" ref="..."/>
  <property name="packagesToScan" value="com.company.policy" />
...
</bean>

Regards