Hibernate 5 :- org.hibernate.MappingException: Unknown entity
当我尝试将休眠5.0与mysql集成时收到错误消息org.hibernate.MappingException: Unknown entity
这似乎是hibernate5.0.0和5.0.1的问题。这在休眠 4.3.9 中工作正常
Maven dependices
<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>
休眠.cfg.xml
<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>
HibernateMain.java 代码
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();
}
}
用户.java
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;
}
}