2

I have a project and I'm having trouble configuring it to work with JPA and hibernate.

I have a class with the @Entity annotation, and when I try to persist an instance of it, I get this error:

java.lang.IllegalArgumentException: Unknown entity: com.package.to.MyEntityClass
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1149)
    ... (stack trace of my code trying to persist an instance built from a request to a servlet)

Apparently Hibernate can't see that MyEntityClass is supposed to be an entity.

However if I add <class> elements to my persistence.xml file, the entity can be found, and I get errors that its @OneToOne annotation references an unknown entity, which I'm assuming I also need to add to the persistence.xml file.

Aren't classes with @Entity annotations supposed to be discovered automatically? I looked at this question and I tried to reference my .war file in the <jar-file> tag as it recommends but it didn't work. (I get a file not found exception)

Any idea how I can fix this? I'm using JBoss EAP 7 to deploy, and this is my persistence.xml:

<persistence 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"
  version="2.1">

  <persistence-unit name="unit1" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <jta-data-source>java:/OracleDS</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="hibernate.show_sql" value="true" />
      <property name="hibernate.format_sql" value="true" />
      <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
      <property name="hibernate.hbm2ddl.auto" value="validate" />

      <!-- Configuring Connection Pool -->
      <property name="hibernate.c3p0.min_size" value="5" />
      <property name="hibernate.c3p0.max_size" value="10" />
      <property name="hibernate.c3p0.timeout" value="500" />
      <property name="hibernate.c3p0.max_statements" value="30" />
      <property name="hibernate.c3p0.idle_test_period" value="2000" />
    </properties>
  </persistence-unit>
</persistence>
devil0150
  • 1,350
  • 3
  • 13
  • 36
  • how does your deployment look like? Where is the persistence.xml located? Why have you transaction-type RESOURCE_LOCAL and not JTA? Why do you have connection pool configuration? – Simon Martinelli Feb 23 '18 at 21:12
  • @SimonMartinelli The path of the file is `/WEB-INF/classes/META-INF/persistence.xml` in the .war. What do you mean about deployment? I'm using maven to build the .war and I copy it to the standalone/deployments/ folder of jboss. Should I not configure the pool? I'm using RESOURCE_LOCAL for now because I'm trying to learn how to set it up first and that's the value I found in some tutorials. I will probably switch to JTA once I get the DB working. Or does it need to be JTA to work? – devil0150 Feb 23 '18 at 21:23

0 Answers0