以下代码适用于Hibernate 4.3,但是当我使用Hibernate 5.0尝试相同的代码时,会导致以下错误:
Exception in thread "main" org.hibernate.MappingException: Unknown entity: entity.Message
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 client.Main.main(Main.java:14)
有人可以帮我理解为什么会这样吗?
以下是用于测试示例的代码:
Main.java
package client;
import org.hibernate.Session;
import util.HibernateUtil;
import entity.Message;
public class Main {
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Message message = new Message( "Hello Hibernate 5" );
session.save(message);
session.getTransaction().commit();
session.close();
}
}
HibernateUtil.java
package util;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
return configuration.buildSessionFactory( new StandardServiceRegistryBuilder().applySettings( configuration.getProperties() ).build() );
}
catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Message.java
package entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="message")
public class Message {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="ID")
private Long id;
@Column(name="TEXT")
private String text;
public Message() {}
public Message(String text) {
this.text = text;
}
}
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>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hello</property>
<property name="connection.username">root</property>
<property name="connection.password">pass</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping class="entity.Message"/>
</session-factory>
</hibernate-configuration>
SQL数据库架构(RDBMS:MYSQL)
CREATE DATABASE `hello`;
USE `hello`;
CREATE TABLE `message` (
`ID` BIGINT(20) NOT NULL AUTO_INCREMENT,
`TEXT` VARCHAR(255) NULL DEFAULT NULL,
PRIMARY KEY (`ID`)
);
答案 0 :(得分:11)
更新:在Hibernate 5.0.x中,标准服务注册表的配置已弃用
相反,您应该使用元数据:
进行引导在HibernateUtil
课程中,您应该添加
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();
}
作为旁注,请将Message Class的ID声明更改为
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="ID")
private Long id;
因此hibernate可以正确使用mySql的自动增量,否则会引发异常。
答案 1 :(得分:0)
或者,您可以在Hibernate 5+中执行以下操作,并将SessionFactory
/ Session
创建类 extend
下面的HibernateHelper
类:
package com.company.hibernatehelper;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateHelper {
private static SessionFactory sessionFactory = null;
public static SessionFactory getSessionFactory() {
if(sessionFactory == null) {
sessionFactory = new Configuration().configure().buildSessionFactory();
}
return sessionFactory;
}
}
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateHelper {
private static SessionFactory sessionFactory = null;
public static SessionFactory getSessionFactory() {
if(sessionFactory == null) {
sessionFactory = new Configuration().configure().buildSessionFactory();
}
return sessionFactory;
}
}