作者:mis安小米 | 来源:互联网 | 2023-08-04 21:38
今天我在网上查的Hibernate学习的基础的第一个简单例子。实体类代码如下:packagedomain;publicclassNews{newsmemberspri
今天我在网上查的Hibernate学习的基础的第一个简单例子。
实体类代码如下:
package domain;
public class News {
//news members
private Integer userID;
private String userName;
private String rname;
private String pwd;
private String sex;
public Integer getUserID() {
return userID;
}
public void setUserID(Integer userID) {
this.userID = userID;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getRname() {
return rname;
}
public void setRname(String rname) {
this.rname = rname;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
private String email;
}
main方法类代码如下:
package lee;
import org.hibernate.*;
import org.hibernate.cfg.*;
import domain.*;
public class NewsManager {
public static void main(String[] args)throws Exception
{
//实例化Configuration;configure()方法会负责加载hibernate.cfg.xml文件
Configuration cOnf= new Configuration().configure("hibernate.cfg.xml");
//create SessionFactory
@SuppressWarnings("deprecation")
SessionFactory sf = conf.buildSessionFactory();
//create Session
Session sess = sf.openSession();
//create transaction
Transaction tx = sess.beginTransaction();
//To insert data in db by the method
News n = new News();
n.setEmail("af");
n.setPwd("124");
n.setRname("a");
n.setSex("male");
n.setUserID(1314321);
n.setUserName("fasfdas");
//save,execute save() insert data into db, commit()
sess.save(n);
tx.commit();
//close Session
sess.close();
sf.close();
}
}
XXX.cfg.xml的配置:
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
com.microsoft.jdbc.sqlserver.SQLServerDriver
123456as
jdbc:sqlserver://localhost:1433;DatabaseName=login
sa
true
update
org.hibernate.dialect.SQLServerDialect
XXX.hbm.xml的配置
以上代码出自http://login.sina.com.cn/sso/login.php?useticket=0&returntype=META&service=blog&gateway=1&url=http://blog.sina.com.cn/s/blog_86f4502c0101fcsv.html
可是在写完代码时,出现以下错误:
我导入的jar包为:
以上是通过build path导入的。
现在还在寻求解决办法。