gpt4 book ai didi

java - 线程 "main"中的异常 org.hibernate.MappingException : Unknown entity

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

阅读 PluralSight 上的“Hibernate 简介”教程。我有这个异常错误。完整的错误是:

Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.simpleprogrammer.User
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:776)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1451)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:100)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:678)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:670)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:665)
at com.simpleprogrammer.Program.main(Program.java:15)

不知道哪里出了问题。我创建了 User.java pojo。我创建了一个与 pojo 匹配的表。我创建了映射,然后将映射添加到 hibernate.cfg.xml 文件。但是,仍然出现错误。谁能帮我解决这个问题?

hibernate .cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.default_schema">protein_tracker</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping class="com.simpleprogrammer.User" resource="com/simpleprogrammer/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>

程序.java

public class Program {
public static void main(String[] args) {
System.out.println("Hello World!");
Session session = HibernateUtilities.getSessionFactory().openSession();
session.beginTransaction();

User user = new User();
user.setName("Joe");
user.setGoal(250);
session.save(user);

session.getTransaction().commit();
session.close();
HibernateUtilities.getSessionFactory().close();
}
}

用户.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Oct 17, 2015 11:44:47 AM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.simpleprogrammer.User" table="USERS">
<id name="id" type="int">
<column name="ID" />
<generator class="identity" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="total" type="int">
<column name="TOTAL" />
</property>
<property name="goal" type="int">
<column name="GOAL" />
</property>
</class>
</hibernate-mapping>

用户.java

package com.simpleprogrammer;

public class User {
private int id;
private String name;
private int total;
private int goal;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getGoal() {
return goal;
}
public void setGoal(int goal) {
this.goal = goal;
}
}

hibernate 工具.java

package com.simpleprogrammer;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtilities {

private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

static {
try {
Configuration configuration = new Configuration().configure();

serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
catch(HibernateException exception) {
System.out.println("Problem creating session factory!");
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}

最佳答案

LinhSays 您好,我已经使用 Hibernate 5 执行了您的代码,但我遇到了完全相同的错误。我用 Hibernate 4 执行过,没有问题。

Pluralsight 教程是为 Hibernate 4 创建的。这是一个与 Hibernate 5 一起工作的 session 工厂构建器:

public class HibernateUtilities_5 {

private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
try {
StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder()
.configure("hibernate.cfg.xml").build();
Metadata metadata = new MetadataSources(standardRegistry).getMetadataBuilder().build();
return metadata.getSessionFactoryBuilder().build();

} catch (HibernateException he) {
System.out.println("Session Factory creation failure");
throw he;
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}

关于java - 线程 "main"中的异常 org.hibernate.MappingException : Unknown entity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33194384/

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