gpt4 book ai didi

java - Gradle Hibernate 配置需要映射类

转载 作者:行者123 更新时间:2023-12-01 11:39:44 25 4
gpt4 key购买 nike

我正在使用 Gradle 来管理 Hibernate 的依赖项(我之前使用的是 Maven),但我遇到了一些奇怪的事情。当我尝试将带注释的类的一个简单实例保存到数据库时,我最终得到一个 MappingException ,表示该类未映射。由于我存储实例的方式,这种情况有些独特。假设情况是这样的:我有一个经过正确注释的 hibernate 类,该类将使用 saveOrUpdate() 使用 EntityManager 中的 Session 进行保存。我有 persistance.xml,但没有 hibernate 配置文件。我依靠 EntityManager 对映射类的自动检测将其提供给 Session,以便它可以被识别为正确注释的。我正在使用 Hibernate 4.3.8。该代码在切换之前有效。所有方法调用都在事务上下文中进行,并且事务已提交并刷新 session 。

异常:

Caused by: org.hibernate.MappingException: Unknown entity: com.gmail.sarah.project.Rank

Gradle 依赖关系:

compile "org.hibernate:hibernate-core:4.3.8.Final"
compile "org.hibernate:hibernate-entitymanager:4.3.8.Final"
compile "org.hibernate:hibernate-ehcache:4.3.8.Final"
compile "mysql:mysql-connector-java:5.1.35"

编辑(测试用例也有同样的问题):主类:

package com.gmail.physicistsarah.gradletestproject.core;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Table;
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();
}

@Entity
@Table(name = "Person")
public static class Person {

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

}持久性.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>
<properties>
<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="create"/>
</properties>
</persistence-unit>

构建.gradle:

apply plugin: 'java'
apply plugin:'application'

sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

// NetBeans will automatically add "run" and "debug" tasks relying on the
// "mainClass" property. You may however define the property prior executing
// tasks by passing a "-PmainClass=<QUALIFIED_CLASS_NAME>" argument.
//
// Note however, that you may define your own "run" and "debug" task if you
// prefer. In this case NetBeans will not add these tasks but you may rely on
// your own implementation.
mainClassName = "com.gmail.physicistsarah.gradletestproject.core.Init"
if (!hasProperty('mainClass')) {
ext.mainClass = 'com.gmail.physicistsarah.gradletestproject.core.Init'
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}

task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}

artifacts {
archives sourcesJar
archives javadocJar
}
repositories {
mavenCentral()
// You may define additional repositories, or even remove "mavenCentral()".
// Read more about repositories here:
// http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:repositories
}

dependencies {
// TODO: Add dependencies here ...
// You can read more about how to add dependency here:
// http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:how_to_declare_your_dependencies
compile "org.hibernate:hibernate-core:4.3.8.Final"
compile "org.hibernate:hibernate-entitymanager:4.3.8.Final"
compile "org.hibernate:hibernate-ehcache:4.3.8.Final"
compile "mysql:mysql-connector-java:5.1.35"
testCompile group: 'junit', name: 'junit', version: '4.10'
}

异常(exception):

INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.gmail.physicistsarah.gradletestproject.core.Init$Person
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1096)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1443)
at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:242)
at org.hibernate.event.internal.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:511)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:100)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:684)
at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:676)
at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:671)
at com.gmail.physicistsarah.gradletestproject.core.Init.main(Init.java:23)

persistence.xml(和 META-INF 包)位于 resources/main 文件夹中。

最佳答案

您的代码中有很多错误:

  1. 实体不能是嵌套类
  2. 实体必须具有无参数构造函数
  3. 实体字段可能不是最终的
  4. 实体必须具有用 @Id 注释的属性
  5. 实体必须列在 persistence.xml 文件中:

    <class>com.foo.bar.EntityClassName</class>

关于java - Gradle Hibernate 配置需要映射类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29636638/

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