0

I'm currently learning Hibernate and JPA and I just stumbled upon the following issue:

I have an entity class called User, and this class is annotated with the @Entity (JPA) annotation. My persistence.xml file does NOT have a mapping for it, yet I can use it on my EntityManager without issues.

I just created another entity, called Comment (some packages below), this one is also annotated with @Entity and is also NOT on my persistence.xml file. This one however, throws a org.hibernate.MappingException: Unknown entity exception when used on the EntityManager.

My persistence.xml file:

<?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="net.notfab.hibernatetest" transaction-type="RESOURCE_LOCAL">
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <!-- JPA Properties -->
            <property name="javax.persistence.provider" value="org.hibernate.jpa.HibernatePersistenceProvider"/>
            <property name="javax.persistence.jdbc.driver" value="org.mariadb.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.url"
                      value="jdbc:mariadb://IP:PORT...."/>
            <property name="javax.persistence.jdbc.user" value=""/>
            <property name="javax.persistence.jdbc.password" value=""/>

            <!-- Hibernate Properties -->
            <property name="hibernate.connection.provider_class"
                      value="org.hibernate.hikaricp.internal.HikariCPConnectionProvider"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MariaDBDialect"/>
            <property name="hibernate.connection.CharSet" value="utf8mb4_bin"/>
            <property name="hibernate.connection.characterEncoding" value="utf8mb4_bin"/>
            <property name="hibernate.connection.useUnicode" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="validate" />
            <property name="hibernate.archive.autodetection" value="class, hbm" />

            <!-- Hikari Properties -->
            <property name="hibernate.hikari.minimumIdle" value="5"/>
            <property name="hibernate.hikari.maximumPoolSize" value="10"/>
            <property name="hibernate.hikari.idleTimeout" value="15000"/>
            <property name="hibernate.hikari.leakDetectionThreshold" value="30000"/> <!-- 30 Seconds -->
            <property name="hibernate.hikari.poolName" value="TestPool"/>
        </properties>
    </persistence-unit>
</persistence>

What may be causing this weird behavior?

Is there a way for me to continue using the annotated classes without having to declare them on the XML file? (It's 2018 already - Annotations are way better!).

Note: This question is similar but their end goal ended up being mapping on the XML, which I'm trying to avoid by all means. I'm also looking for an explanation to the above-mentioned behavior.

Edit: As requested by the comments, here is the full stacktrace https://hastebin.com/haguniheca.cs (as well as here https://hasteb.in/ipuzojoc.cs in case hastebin goes down again).

In this scenario, I'm not using Spring.

Edit 2: Here is my entity.

Edit 3: This is a gradle project, running off of a standalone (shaded) jar file on Java SE, currently using the following list of dependencies for persistence (among others):

compile group: 'org.hibernate', name: 'hibernate-hikaricp', version: '5.3.3.Final'
compile group: 'org.mariadb.jdbc', name: 'mariadb-java-client', version: '2.2.6'

Edit 4: As per Eugen's comment I did some testing and the cause of the issue seems to be IntelliJ. By running the shaded jar file on it's own from the command line, the issue is fixed. (Maybe the classpath is different when running off of the application debug?). The question then becomes "how can I fix IntelliJ's run configuration for my app?".

Fabricio20
  • 492
  • 8
  • 19
  • 1
    Can you post the error stacktrace? Also the entity source code .. –  Dec 10 '18 at 21:39
  • 1
    @Spara Where would those options go? Under properties? Under the persistence-unit? By the way, keep in mind I'm not using Spring in this scenario (I've noticed Spring has an "annotation-based" mode thing). – Fabricio20 Dec 10 '18 at 21:40
  • 1
    @Spara The OP doesn't use Spring. –  Dec 10 '18 at 21:40
  • 1
    Please post the `Comments` source code. –  Dec 10 '18 at 21:46
  • 1
    Edited with the entity source code and the stacktrace. Forgive my lombok abuse. – Fabricio20 Dec 10 '18 at 21:46
  • 1
    Is this a Maven project? –  Dec 10 '18 at 21:47
  • 1
    Edited once again with the specifications about the project and it's build phase. – Fabricio20 Dec 10 '18 at 21:52
  • 1
    Using XML mapping and annotation based mapping are alternatives so if you have one, no need for second. – Antoniossss Dec 10 '18 at 21:52
  • Are you sure `net.notfab.hibernatetest.entities` is not in the test area? –  Dec 10 '18 at 21:54
  • Yes, I currently have no tests for this project (test folder is empty). After shading the jar, the files are properly moved to the root -> net folder as well as the persistence.xml file being moved to the META-INF folder. – Fabricio20 Dec 10 '18 at 21:56
  • How do you run your project? –  Dec 10 '18 at 21:57
  • Currently running off of IntelliJ's debug feature (application). The classpath defined is the one for the _main module (Hibernatetest_main). – Fabricio20 Dec 10 '18 at 22:00
  • 1
    Strange, try to run the executable jar with `java -jar ...`. –  Dec 10 '18 at 22:03
  • @EugenCovaci Strange, looks like running directly from the command line threw up a SchemaManagementException upon startup for one of my other entities (which wasn't being thrown when running off of IntelliJ). I'm gonna fix this one and see if the issue persists outside of IntelliJ and then properly update the question. – Fabricio20 Dec 10 '18 at 22:08
  • How do you actually know you can use that entity? Please include some test code (even the most dumb and simples one) – Antoniossss Dec 10 '18 at 22:20

0 Answers0