hibernate.cfg.xml
root
123
com.mysql.jdbc.Driver
jdbc:mysql:///localhost:3306/hibernate?useUnicode=true&characterEncoding=UTF-8
org.hibernate.dialect.MySQLDialect
true
true
create
Students.hbm.xml
Students.java
import java.util.Date;
//学生类
public class Students {
private int sid;
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) {
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;
}
public String toString() {
return "Students [sid=" + sid + ", sname=" + sname + ", gender="
+ gender + ", birthday=" + birthday + ", address=" + address
+ "]";
}
}
StudentsTest.java
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class StudentsTest {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void init(){
Configuration configuration=new Configuration().configure();
sessionFactory=configuration.buildSessionFactory();
session= sessionFactory.openSession();
transaction=session.beginTransaction();
}
@After
public void destory(){
transaction.commit();
session.close();
sessionFactory.close();
}
@Test
public void testSaveStudents(){
Students s = new Students(1,"张三丰","男",new Date(),"武当山");
session.save(s);
}
}
报错信息:
二月 16, 2017 1:14:19 上午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.7.Final}
二月 16, 2017 1:14:19 上午 org.hibernate.cfg.Environment
INFO: HHH000206: hibernate.properties not found
二月 16, 2017 1:14:19 上午 org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.... Use namespace http://www.hibernate.org/dtd/... instead. Support for obsolete DTD/XSD namespaces may be removed at any time.
提示找不到hibernate.properties 这个文件。。怎么回事?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
搞定了 首先参考这篇文章,http://blog.163.com/xh_ding/b...


因为Hibernate 4废弃buildSessionFactory,所以先按照我给的网页去修改,可能会报错,导入这两个包import org.hibernate.service.ServiceRegistryBuilder;
运行成功后如图
其实我5.10号就答辩了,现在才学hibernate,真是作死啊
不会啊...该方法可以用啊