Mam*_*arg 14 java mysql hibernate hibernate-5.x
org.hibernate.MappingException: Unknown entity
当我尝试将hibernate 5.0与mysql集成时,我收到错误消息
这似乎是hibernate5.0.0和5.0.1的问题.这适用于hibernate 4.3.9
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.0.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3307/SampleDB
</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping class="UserA.User"></mapping>
</session-factory>
Run Code Online (Sandbox Code Playgroud)
package UserA;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.mapping.Map;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.service.ServiceRegistry;
public class HibernateMain {
public static void main(String[] args) {
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
SessionFactory sf = configuration.buildSessionFactory(sr);
User user1 = new User();
user1.setUserName("Arpit");
user1.setUserMessage("Hello world from arpit");
user1.setUserId(22);
Session ss = sf.openSession();
ss.beginTransaction();
// saving objects to session
ss.save(user1);
ss.getTransaction().commit();
ss.close();
}
}
Run Code Online (Sandbox Code Playgroud)
package UserA;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity(name="User_table")
public class User {
@Id
int userId;
@Column(name = "User_Name")
String userName;
@Column(name = "User_Message")
String userMessage;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserMessage() {
return userMessage;
}
public void setUserMessage(String userMessage) {
this.userMessage = userMessage;
}
}
Run Code Online (Sandbox Code Playgroud)
v.l*_*nev 28
我已经修复了Hibernate 5的相同问题.此代码中存在问题
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
SessionFactory sf = configuration.buildSessionFactory(sr);
Run Code Online (Sandbox Code Playgroud)
这段代码适用于Hibernate 4.3.5,但相同的代码对Hibernate 5也有同样的问题.
当你这样做时configuration.buildSessionFactory(sr)
,使用Hibernate 5,Configuration
丢失了有关通过调用获取的映射的所有信息configuration.configure()
.
解
要解决这个问题,如果你使用标准的配置文件hibernate.cfg.xml
和hibernate.properties
,您可以通过这种方式创建会话工厂(无ServiceRegistry
)
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Run Code Online (Sandbox Code Playgroud)
加载属性
如果你在其他文件中有属性hibernate.properties
,你可以使用构建会话工厂StandardServiceRegistryBuilder
(无论如何,如果你有hibernate.properties
和其他文件,它将被加载)
将属性作为资源加载
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().
configure().loadProperties("hibernate-h2.properties").build();
SessionFactory sf = new Configuration().buildSessionFactory(serviceRegistry);
Run Code Online (Sandbox Code Playgroud)
您需要hibernate-h2.properties
在类路径(sources文件夹的根目录,资源文件夹)中.您也可以从根源文件夹指定路径
/com/github/xxx/model/hibernate-h2.properties
.
从文件系统中的路径加载属性
File propertiesPath = new File("some_path");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().
configure().loadProperties(propertiesPath).build();
SessionFactory sf = new Configuration().buildSessionFactory(serviceRegistry);
Run Code Online (Sandbox Code Playgroud)
你可以在这里使用这种方法找到一个示例控制台应用程序fluent-hibernate-mysql.它使用实用程序类从fluent-hibernate库构建会话工厂.
Hibernate 5教程不正确
Hibernate 5教程1.1.6中有一个不正确的例子.启动和帮助.它使用此代码
return new Configuration().configure().buildSessionFactory(
new StandardServiceRegistryBuilder().build() );
Run Code Online (Sandbox Code Playgroud)
它没有做正确的配置.
在Hibernate 5中,您需要构建StandardServiceRegistry
和Metadata
构建SessionFactory
.您可以使用以下内容HibernateUtil
进行构建SessionFactory
.hibernate.cfg.xml
应该位于应用程序的类路径的根目录中.
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder() .configure("hibernate.cfg.xml").build();
Metadata metadata = new MetadataSources(standardRegistry).getMetadataBuilder().build();
return metadata.getSessionFactoryBuilder().build();
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Run Code Online (Sandbox Code Playgroud)
此外,如果你正在使用Hibernate 5并使用它@Id
作为你的标识符生成策略,那么GenerationType.AUTO
默认使用MySQL将使用"序列"身份生成器com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'SampleDB.hibernate_sequence' doesn't exist
,如果你没有在你的配置中使用它,它会给你一个例外.标识符属性的实体.因此,使用Hibernate 5,请GenerationType.IDENTITY
改用.
小智 0
你没有添加配置文件
configuration.configure("/hibernate.cfg.xml");
Run Code Online (Sandbox Code Playgroud)