Hibernate自動檢測持久性。xml不工作

[英]Hibernate Auto Detection in Persistence.xml Not Working


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中嗎?以及如何在JPA 2.0中自動檢測實體並在我的持久性中跟蹤它們的標記使用。但是,Hibernate不會自動檢測實體。當我刪除 標記時,它不再工作,我得到一個MappingException。由於某種原因,我似乎不能啟用自動檢測。請注意,這是一個獨立的hibernate應用程序(據我所知不重要)。我有這個例子:

Persistence.xml:

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):

Init(主類):

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();
}
}

1 个解决方案

#1


4  

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

您應該嘗試使用HibernatePersistenceProvider並從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

還可以刪除< exclusive -unlisted-classes>,因為根據官方模式定義http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/persistence_2_xsd不適用於Java SE持久性單元

<?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.

我注意到您正在將JPA代碼與Hibernate代碼混合在一起。如果您正在使用JPA,那么請盡量堅持使用JPA。

Use the following code after creating the EntityManager:

創建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.

記住訪問EntityManager.merge()調用返回的實體以訪問任何自動生成的字段。

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

您應該使用以下Maven POM依賴項在Java SE環境中工作:

<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>

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/04/18/72fef2c197fb25347cc4ab1c22e804ec.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com