0

I was deploying a war that contained the entities inside the WEB-INF/classes directory, but now I moved those entities to a separate proyect/module and now the entities reside inside WEB-INF/lib/*.jar

After the change wildfly failed to scan the entities producing errors while deploying.

I then tried adding

<class>class.inside.webinf.lib.Entity1</class>

tags inside my persistence.xml file and everything deployed correctly.

Full persistence.xml at file.war/WEB-INF/classes/META-INF/persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
   <persistence-unit name="pu" transaction-type="JTA">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>java:/jboss/datasources/sgsdscore</jta-data-source>

      <class>io.ingenia.sgsds.score.entity.Entity1</class>
      <class>io.ingenia.sgsds.score.entity.EntityN</class>

      <exclude-unlisted-classes>false</exclude-unlisted-classes> 
      <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
      <properties>
         <property name="hibernate.hbm2ddl.auto" value="update"/>
         <property name="hibernate.jdbc.batch_size" value="200"/> 
         <property name="hibernate.order_inserts" value="true" />
         <property name="hibernate.order_updates" value="true" />

         <property name="hibernate.cache.use_second_level_cache" value="true"/>
      </properties>
   </persistence-unit>
</persistence>

Am I missing something ?

EJBs inside WEB-INF/lib/*.jar where successfuly scanned though...

John Ament
  • 11,595
  • 1
  • 36
  • 45
David Hofmann
  • 5,683
  • 12
  • 50
  • 78

3 Answers3

2

I guess that this answer also answers my question https://stackoverflow.com/a/6263592/39998

It's fine to have the entities inside the lib directory like WEB-INF/lib/entities.jar, but then the persistence.xml file must be inside WEB-INF/lib/entities.jar/META-INF/persistence.xml

If the persistence.xml file is inside WEB-INF/classes/META-INF/persistence.xml then the automatic scanning will occurr only inside WEB-INF/classes/*

Makes sense. (kind of...)

Community
  • 1
  • 1
David Hofmann
  • 5,683
  • 12
  • 50
  • 78
1

In general, this should work. I'm using JPA entity classes from a WEB-INF/lib/*.jar in most of my projects.

However, there may be issues when some of your classes are not in the same JAR as the persistence.xml descriptor.

Harald Wellmann
  • 12,615
  • 4
  • 41
  • 63
1

It's hard to say without seeing your persistence.xml file. You can specify exclude-unlisted-classes which will say only look at the listed classes. You must use jar-file to indicate what JAR files to scan (it's not clear from your question if you're already doing that.

John Ament
  • 11,595
  • 1
  • 36
  • 45
  • I put my persistence.xml in the question now. I know about 'eclude-unlisted-classes', I didn't know about 'jar-file' can you elaborate ? And then, By the spec, shouln't it scall all the classes and lib directory of 'WEB-INF' ? – David Hofmann Mar 01 '15 at 19:04
  • 1
    EJB and JPA are two different specs, with their own rules. You may want to review section 8.2.1.6 of the JPA spec to clarify how entities are found. By default, they're only scanned in the root of where the persistence unit is defined. for your use case, `jar-file` is the right thing to use. – John Ament Mar 01 '15 at 19:36