简体   繁体   中英

JPA entity containing a class from an external jar

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM