出錯(cuò)啦,org.hibernate.MappingException: Unknown entity: lin.Students
package lin;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
//學(xué)生類(lèi)
@Entity
@Table(name="students_info")
public class Students {
// 1.公有的類(lèi)
// 2、提供公有的不帶參數(shù)的默認(rèn)的構(gòu)造方法
// 3.屬性私有
// 4.屬性setter,getter封裝
@Id
private int sid; // 學(xué)號(hào)
private String sname; // 姓名
private String gender; // 性別
private Date birthday; // 出生日期
private String address;// 地址
public Students() {
}
public Students(int sid, String sname, String gender, Date birthday, String address) {
// super();
this.sid = sid;
this.sname = sname;
this.gender = gender;
this.birthday = birthday;
this.address = address;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Students [sid=" + sid + ", sname=" + sname + ", gender=" + gender + ", birthday=" + birthday
+ ", address=" + address + "]";
}
}
<?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="connection.username">root</property>
? <property name="connection.password">123456</property>
? <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
? <property name="connection.url">jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8</property>
? <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
? <property name="show_sql">true</property>
? <property name="hibernate.format_sql">true</property>
? <property name="hdm2ddl.auto">create</property>
??
? <property name="hibernate.current_session_context_class">thread</property>
? <mapping class="lin.Students"/>
?</session-factory>
</hibernate-configuration>
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import lin.Students;
//測(cè)試類(lèi)
public class StudentsTest{
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void init(){
//創(chuàng)建配置對(duì)象
//不帶參數(shù)的configure()方法默認(rèn)加載hibernate.cfg.xml文件
//如果傳入abc.xml作為參數(shù),則不再加載hibernate.cfg.xml,改為加載abc.xml
Configuration config = new Configuration().configure();
//創(chuàng)建服務(wù)注冊(cè)對(duì)象
ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
//創(chuàng)建會(huì)話工廠對(duì)象
sessionFactory = config.buildSessionFactory(serviceRegistry);
//創(chuàng)建會(huì)話對(duì)象
session=sessionFactory.openSession();
//開(kāi)啟事務(wù)
transaction=session.beginTransaction();
}
@After
public void destroy(){
transaction.commit(); //提交事務(wù)
session.close(); // 關(guān)閉會(huì)話
sessionFactory.close();//關(guān)閉會(huì)話工廠
}
@Test
public void testSaveStudents()
{
//生成學(xué)生對(duì)象
Students s =new Students(1,"張三豐","男",new Date(),"武當(dāng)山");
session.save(s); //保存對(duì)象進(jìn)數(shù)據(jù)庫(kù)
}
}
運(yùn)行結(jié)果:
org.hibernate.MappingException: Unknown entity: lin.Students
at org.hibernate.metamodel.internal.MetamodelImpl.entityPersister(MetamodelImpl.java:620)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1596)
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:668)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:660)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:655)
at StudentsTest.testSaveStudents(StudentsTest.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
2016-07-21
建議還是用老師的包
2016-07-21
這是另外一種方法,
后來(lái)用了最新的Hibernate,需要做些更改,
Configuration config = new Configuration().configure();
config.addClass(Students.class); ????//**這里需要新加
ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
2016-07-21
1.看一下映射文件Student.hbm.xml建了沒(méi),保存的位置對(duì)不對(duì)?
2.這里面沒(méi)有寫(xiě)mapping resourse,映射文件就必須放在classpath下