1

I've read Do I need <class> elements in persistence.xml? and How to auto detect entities in JPA 2.0 and followed their tag useage in my persistence.xml, but Entitys are not being auto detected by Hibernate. When I remove the <class> tag this no longer works and I get a MappingException. For some reason I can't seem to enable auto detection. NOTE this is a stand-alone hibernate application (shouldn't matter as far as I know). I have this example:

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="Test_Project" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.gmail.physicistsarah.gradletestproject.core.Person</class>
    <!--Exclude unlisted class detection-->
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <!-- Scan for annotated classes and Hibernate mapping XML files -->
        <property name="hibernate.archive.autodetection" value="class, hbm"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/test_project_db?zeroDateTimeBehavior=convertToNull"/>
        <property name="javax.persistence.jdbc.user" value="root"/>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        <property name="javax.persistence.jdbc.password" value=""/>
        <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
        <property name="javax.persistence.schema-generation.database.action" value="none"/>
        <!--<property name="hibernate.show_sql" value="true"/>-->
        <property name="hibernate.hbm2ddl.auto" value="create"/>
    </properties>
</persistence-unit>

Person:

package com.gmail.physicistsarah.gradletestproject.core;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 *
 * @author Sarah Szabo
 */
@Entity
@Table(name = "Person")
public class Person {

@Id
@GeneratedValue
@Column(name = "Person_ID")
private final int id = 0;
@Column(name = "First_Name", nullable = false)
private final String firstName;
@Column(name = "Last_Name", nullable = false)
private final String lastName;

public Person(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
}
}

Init (Main class):

package com.gmail.physicistsarah.gradletestproject.core;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class Init {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("Test_Project");
    EntityManager manager = factory.createEntityManager();
    Session session = manager.unwrap(Session.class);
    Transaction transaction = session.getTransaction();
    transaction.begin();
    session.saveOrUpdate(new Person("Carl", "Gauss"));
    session.saveOrUpdate(new Person("Benoit", "Mandelbrot"));
    transaction.commit();
    factory.close();
}
}
Community
  • 1
  • 1
Sarah Szabo
  • 10,345
  • 9
  • 37
  • 60

1 Answers1

4

You should try using HibernatePersistenceProvider and remove the <class> element from the persistence.xml.

Also remove <exclude-unlisted-classes> since it is not applicable to Java SE persistence units as per the official schema definition http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/persistence/persistence_2_1.xsd

<?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="Test_Project" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <shared-cache-mode>ALL</shared-cache-mode>
    <validation-mode>AUTO</validation-mode>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/test_project_db?zeroDateTimeBehavior=convertToNull" />
        <property name="javax.persistence.jdbc.user" value="root" />
        <property name="javax.persistence.jdbc.password" value="" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
    </properties>
</persistence-unit>
</persistence>

I noticed that you are inter mixing JPA code with Hibernate code. If you're using JPA then try to stick to JPA.

Use the following code after creating the EntityManager:

EntityTransaction tran = manager.getTransaction();
tran.begin();

try {
    Person savedPerson1 = manager.merge(new Person("Carl", "Gauss"));
    Person savedPerson2 = manager.merge(new Person("Benoit", "Mandelbrot"));
    tran.commit();
} catch (Exception e) {
    tran.rollback();
}

manager.close();
factory.close();

Remember to access the returned entity from EntityManager.merge() call to access any auto-generated fields.

You should use the following Maven POM dependencies for the above to work as expected in a Java SE environment:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.3.8.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.1-api</artifactId>
    <version>1.0.0.Final</version>
</dependency>
Sourav Paul
  • 616
  • 5
  • 5