hibarnate 配置文件(hibarnate.cfg.xml) jdbc:oracle:thin:@localhost:1521:orcl C##QDT 19950926 oracle.jdbc.driver.OracleDriver org.hibernate.dialect.Oracle9Dialect true true create Car.hbm.xml 实体类 Car(实际只需要get set 及无
Car.hbm.xmljdbc:oracle:thin:@localhost:1521:orcl C##QDT 19950926 oracle.jdbc.driver.OracleDriver org.hibernate.dialect.Oracle9Dialect true true create
实体类 Car(实际只需要get set 及无参构造函数)
package javabean;
public class Car {
private String cid;// 主键
private String password;//密码
private String cname;// 客户名称
private String gender;// 客户性别
private String birthday;// 客户生日
public Car() {
super();
// TODO Auto-generated constructor stub
}
public Car(String cid, String password, String cname, String gender,
String birthday) {
super();
this.cid = cid;
this.password = password;
this.cname = cname;
this.gender = gender;
this.birthday = birthday;
}
/**
* @return the cid
*/
public String getCid() {
return cid;
}
/**
* @param cid the cid to set
*/
public void setCid(String cid) {
this.cid = cid;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return the cname
*/
public String getCname() {
return cname;
}
/**
* @param cname the cname to set
*/
public void setCname(String cname) {
this.cname = cname;
}
/**
* @return the gender
*/
public String getGender() {
return gender;
}
/**
* @param gender the gender to set
*/
public void setGender(String gender) {
this.gender = gender;
}
/**
* @return the birthday
*/
public String getBirthday() {
return birthday;
}
/**
* @param birthday the birthday to set
*/
public void setBirthday(String birthday) {
this.birthday = birthday;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((birthday == null) ? 0 : birthday.hashCode());
result = prime * result + ((cid == null) ? 0 : cid.hashCode());
result = prime * result + ((cname == null) ? 0 : cname.hashCode());
result = prime * result + ((gender == null) ? 0 : gender.hashCode());
result = prime * result
+ ((password == null) ? 0 : password.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Car other = (Car) obj;
if (birthday == null) {
if (other.birthday != null)
return false;
} else if (!birthday.equals(other.birthday))
return false;
if (cid == null) {
if (other.cid != null)
return false;
} else if (!cid.equals(other.cid))
return false;
if (cname == null) {
if (other.cname != null)
return false;
} else if (!cname.equals(other.cname))
return false;
if (gender == null) {
if (other.gender != null)
return false;
} else if (!gender.equals(other.gender))
return false;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
return true;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Car [cid=" + cid + ", password=" + password + ", cname="
+ cname + ", gender=" + gender + ", birthday=" + birthday + "]";
}
}
测试代码
package hibernate;
import javabean.Car;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class StudentTest_2 {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void fun_1(){
//创建配置对象
Configuration config = new Configuration().configure();
//Configuration config=new Configuration();
//创建服务注册对象
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//创建会话工厂
sessionFactory=config.buildSessionFactory(serviceRegistry);
//会话对象
session =sessionFactory.openSession();
//开启事务
transaction=session.beginTransaction();
}
@Test
public void fun_2(){
Car car=new Car("223456","123456","zhangsan","man","1995-09-26");
session.save(car);
}
@After
public void fun_3(){
transaction.commit();//提交事务
session.close();//关闭会话
sessionFactory.close();//关闭会话工厂
}
}
