org.hibernate.MappingException: Unknown entity: annotations.用户
考虑层次结构:
以及以下类和 xml:
HibernateUtil.java
package annotations;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
/**
*
* @author X3
*
*/
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static final String HIBERNATE_CFG = "hibernateAnnotations.cfg.xml";
private static SessionFactory buildSessionFactory()
{
Configuration cfg = new Configuration().addResource(HIBERNATE_CFG).configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().
applySettings(cfg.getProperties()).buildServiceRegistry();
SessionFactory sessionFactory = cfg.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
用户.java
package annotations;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.sql.Timestamp;
@Entity
@Table(name = "Users")
public class Users {
@Id
@GeneratedValue
@Column(name = "USER_ID")
private long userID;
@Column(name = "USERNAME", nullable = false, length = 100)
private String username;
@Column(name = "MessageTimeDate", nullable = false)
private java.sql.Timestamp datetime;
@Column(name = "UserMessage", nullable = false)
private String message;
public Users(String username , String message)
{
java.util.Date date = new java.util.Date();
this.datetime = new Timestamp(date.getTime());
this.username = username;
this.message = message;
}
public long getUserID() {
return userID;
}
public void setUserID(long userID) {
this.userID = userID;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public java.sql.Timestamp getDatetime() {
return datetime;
}
public void setDatetime(java.sql.Timestamp datetime) {
this.datetime = datetime;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
主要.java
package annotations;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
public class Main {
public static void main(String[] args) {
try
{
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
Users user1 = new Users("Jack" , "Hello");
session.save(user1);
session.getTransaction().commit();
session.close();
}
catch (Exception e)
{
System.out.println(e.toString());
e.getStackTrace();
}
}
}
和休眠注释.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/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/CHAT_DB</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="annotations.Users"></mapping>
</session-factory>
</hibernate-configuration>
当我运行main()时,输出窗口中出现以下错误:
org.hibernate.MappingException: Unknown entity: annotations.Users
但是实体在包装中,那么有什么问题呢?Users
annotations