Hibernate学习第一步

今天我在网上查的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的配置:

<?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="hibernate.connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
        <property name="hibernate.connection.password">123456as</property>
        <property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=login</property>
        <property name="hibernate.connection.username">sa</property>
        <property name="show_sql">true</property>
        <property name="hibernate.format_sql">update</property>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
        <!-- property name="hibernate.jdbc.batch_size">15</property-->
    </session-factory>
</hibernate-configuration>
XXX.hbm.xml的配置
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Mar 30, 2014 12:59:27 AM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="domain.News" table="NEWS">
        <id name="userID" type="java.lang.Integer">
            <column name="USERID" />
            <generator class="assigned" />
        </id>
        <property name="userName" type="java.lang.String">
            <column name="USERNAME" />
        </property>
        <property name="rname" type="java.lang.String">
            <column name="RNAME" />
        </property>
        <property name="pwd" type="java.lang.String">
            <column name="PWD" />
        </property>
        <property name="sex" type="java.lang.String">
            <column name="SEX" />
        </property>
        <property name="email" type="java.lang.String">
            <column name="EMAIL" />
        </property>
    </class>
</hibernate-mapping>


以上代码出自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导入的。

现在还在寻求解决办法。



Hibernate学习第一步,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。