Announcement Announcement Module
Collapse
No announcement yet.
JPA Entities from an external JAR: "references an unknown entity" error. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JPA Entities from an external JAR: "references an unknown entity" error.

    Hello,

    We are trying to use an external JAR as a maven dependency containing JPA entities but we have this error:

    Code:
    Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on ourPackage.OurClass.currency references an unknown entity: ourExternalPackage.Currency
    	at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:107)
    	at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1550)
    	at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1473)
    	at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1389)
    	at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1345)
    	at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1477)
    	at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:193)
    	at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:1096)
    	at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:685)
    	at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:73)
    	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:225)
    	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:308)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
    	... 47 more
    Can anybody help us with this problem?

    Some context below:

    We'd like to use a multi module project. Instead of that, half of the team has been working in the main module, a Roo web application, and the other half has created a helper module as a Roo web application, but at the end only a Maven JAR is created in order to include it as a Maven dependency in the main project.

    Afterwards, we will create the appropriate modules for an EAR project to be deployed in a WebSphere Application Server.

    However, the main Roo web project doesn't recognize the Entities within the external JAR, included as a Maven dependecy, as you can see in the error above.

    After a long Google search, I tried something like:

    http://stackoverflow.com/questions/1...ersistence-xml

    or

    http://stackoverflow.com/questions/2...se-environment

    But not yet the solutions mentioned here:

    http://forum.springsource.org/showth...lt-jar-file-gt that links to http://javathoughts.capesugarbird.co...nce-units.html

    However, it doesn't work. This is my persistence.xml.

    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    
    	<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
    		<provider>org.hibernate.ejb.HibernatePersistence</provider>
    		<class>ourExternalPackage.Currency</class>
    		<class>ourExternalPackage.AnotherClass</class>
    		<properties>
    			<property name="hibernate.dialect" value="org.hibernate.dialect.DB2390Dialect"/>
    			<!-- value="create" to build a new database on each run; value="update" 
    				to modify an existing database; value="create-drop" means the same as "create" 
    				but also drops tables when Hibernate closes; value="validate" makes no changes 
    				to the database -->
    			<!-- property name="hibernate.hbm2ddl.auto" value="validate" /-->
    			<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
    			<property name="hibernate.connection.charSet" value="UTF-8" />
    			<!-- Uncomment the following two properties for JBoss only -->
    			<!-- property name="hibernate.validator.apply_to_ddl" value="false" / -->
    			<!-- property name="hibernate.validator.autoregister_listeners" value="false" / -->
    			<!-- property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WebSphereExtendedJTATransactionLookup"/-->
    			<property name="hibernate.archive.autodetection" value="class, hbm"/>       
    		</properties>
    	</persistence-unit>
    </persistence>

    The helper project works correctly (build success in its test integration), so, there is no error in the annotations (@Entity is the one from JPA, not the Hibernate, and the Entity is not missing, actually it is in the JAR, as it seems to occur here http://forum.springsource.org/showth...rnate-mappings)

    The helper module is finished and the main module only lacks of the details regarding the helper module.

    Thank you very much in advance.

  • #2
    At the end it worked without any intervention from my part.

    Maybe it was a STS problem, because I saw the error the last Friday, but it disappear on Monday.

    To summarize, we have several Roo projects and we want to include into one, that we call the "main project", the rest as maven jar dependencies, we call them the "helper modules". We're trying to build a multi module project building each module as a separate Roo application.

    I don't recommend this approach, because it comes with a lot of manual configuration (error prone) and manual manipulation (in order to avoid conflicts, the best option is to eliminate the XML files from the "helper modules". Besides, the configuration of the application should be done in the application itself, not in the helper modules)

    The process is:
    - For a "helper module": create a complete Roo web project, change the POM packaging to JAR (temporary), remove all the configuration files (application context, persistence and so on) in this JAR.
    - For the "main project": import the "helper modules" as MAVEN dependencies. In the persistence.xml add the entities as individual <class> entries:

    Code:
    <persistence ...
    
    	<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
    		<provider>org.hibernate.ejb.HibernatePersistence</provider>
    		<class>com.malsolo.helper1.domain.Currency</class>
    		<class>com.malsolo.helper1.domain.Country</class>
    		<class>com.malsolo.helper2.domain.User</class>
    		...
    		<properties>
    		...
    		</properties>
    	</persistence-unit>
    </persistence>
    As far as I know there is no need of configuring autodetection if you're using Hibernate:
    Code:
    <!-- property name="hibernate.archive.autodetection" value="class, hbm"/ -->
    Now, we need to create the views for the helper entities within the main application.

    Since they're not in the top level package, maybe we'll just copy and paste the Controllers and the view from the helper projects as long as we can use the annotations that they contain. In other case, we will create a Spring MVC objects from scratch.

    Greetings.

    Comment

    Working...
    X