Я получаю сообщение об ошибке org.hibernate.MappingException: Unknown entity
, когда я пытаюсь интегрировать hibernate 5.0 с mysql
Это, похоже, проблема с hibernate5.0.0 и 5.0.1. Это отлично работает с спящим 4.3.9
Зависимости Maven
<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>
hibernate.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();
}
}
User.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;
}
}