org.hibernate.MappingException:未知实体

时间:2016-08-05 21:02:14

标签: java hibernate

这个问题与MappingException相同主题的其他问题不同:未知实体,因为我的hibernate配置中有这一行:

<mapping class="bbb.Students" />

我的实体类如下:

 package bbb;

 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.Id;
 import javax.persistence.Table;

@Entity
@Table(name = "Students")
public class Students implements java.io.Serializable {

private String id;
private String name;
private String number;


@Id
@Column(name = "ID", unique = true, nullable = false)
public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}

@Column(name = "NAME", nullable = false)
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

@Column(name = "NUMBER", nullable = false)
public String getNumber() {
    return number;
}
public void setNumber(String number) {
    this.number = number;
}
}

我有数据库测试和学生手动创建的表。

错误是:

 org.hibernate.MappingException: Unknown entity: bbb.Students

Exception in thread "main" org.hibernate.MappingException: Unknown entity: bbb.Students
    at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:776)
    at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1533)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:104)
    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:682)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:674)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:669)
    at bbb.App.main(App.java:39)

Main类如下:

package bbb;



import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class App 
{
public static void main( String[] args )
{

    System.out.println("Hibernate one to one (Annotation)");

    Configuration conf = new Configuration();
    SessionFactory sf = conf.configure()
    .buildSessionFactory(
         new StandardServiceRegistryBuilder()  
         .applySettings(conf.getProperties())
            .build());

    Session session = sf.openSession();
    session.beginTransaction();

    Students s = new Students();

    s.setId("1");
    s.setName("Joe");
    s.setNumber("12345");



    session.save(s);
    session.getTransaction().commit();

    System.out.println("Done");
}
}

完整的hibernate.config如下:

 <?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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
 <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
 <property name="hibernate.connection.username">root</property>
 <property name="hibernate.connection.password">root</property>
 <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
 <property name="show_sql">true</property>
 <mapping class="bbb.Students" />
 </session-factory>
 </hibernate-configuration>

1 个答案:

答案 0 :(得分:1)

刚刚在基于Hibernate的项目中导入了您的Students类。一切都很好。在运行应用程序之前,我创建了schema&#34; test&#34;手动。使用main方法发布一个类。可能有些不对劲。

更新: 导入了您的主要方法。该应用程序仍然有效。无法重现问题。

max_connections