gpt4 book ai didi

java - Persistence.xml 中的 Hibernate 自动检测不起作用

转载 作者:行者123 更新时间:2023-11-29 03:10:19 26 4
gpt4 key购买 nike

我读过 Do I need <class> elements in persistence.xml?How to auto detect entities in JPA 2.0并在我的 persistence.xml 中跟踪他们的标签用法,但是 Entity Hibernate 不会自动检测到 s。当我删除 <class>标记这不再有效,我得到一个 MappingException .出于某种原因,我似乎无法启用自动检测。注意这是一个独立的 hibernate 应用程序(据我所知应该无关紧要)。我有这个例子:

持久性.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>

人:

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

初始化(主类):

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

最佳答案

您应该尝试使用 HibernatePersistenceProvider并删除 <class>来自 persistence.xml 的元素.

同时删除 <exclude-unlisted-classes>因为根据官方模式定义,它不适用于 Java SE 持久性单元 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>

我注意到您将 JPA 代码与 Hibernate 代码混合在一起。如果您使用的是 JPA,请尝试坚持使用 JPA。

创建 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();

请记住从 EntityManager.merge() 访问返回的实体调用以访问任何自动生成的字段。

您应该使用以下 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>

关于java - Persistence.xml 中的 Hibernate 自动检测不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29724048/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com