所用工具eclipse sqlserver2000
1.建立web 工程bank 在src文件夹下建立两个包second 和sec.test
在second包下创建实体类JD.java QX.java 以及映射文件JD.hbm.xml QX.hbm.xml
在sec.test包下建立测试类Demol.java
2.引入hibernate的几个核心jar包,以及sqlserver2000的三个驱动包
JD.java代码
package second;
public class JD {
private Long jdid;
private String jdName;
private QX qx;
public JD() {
super();
}
public QX getQx() {
return qx;
}
public void setQx(QX qx) {
this.qx = qx;
}
public Long getJdid() {
return jdid;
}
public void setJdid(Long jdid) {
this.jdid = jdid;
}
public String getJdName() {
return jdName;
}
public void setJdName(String jdName) {
this.jdName = jdName;
}
}
QX.java代码
package second;
import java.util.HashSet;
import java.util.Set;
public class QX {
private int qxid;
private String qxName;
private Set jds=new HashSet();
public QX() {
super();
}
public Set getJds() {
return jds;
}
public void setJds(Set jds) {
this.jds = jds;
}
public int getQxid() {
return qxid;
}
public void setQxid(int qxid) {
this.qxid = qxid;
}
public String getQxName() {
return qxName;
}
public void setQxName(String qxName) {
this.qxName = qxName;
}
}
QX.hbm.xml文件
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="second"> <class name="QX" table="tbl_qx"> <id name="qxid" > <generator class="native" /> </id> <property name="qxName" > </property> <!-- jds属性,本类与JD是一对多 --> <set name="jds" inverse="true" cascade="all"> <key> <column name="qxid" /> </key> <one-to-many class="JD" /> </set> </class> </hibernate-mapping>
JD.hbm.xml文件
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="second"> <class name="JD" table="tbl_jd"> <id name="jdid" > <generator class="assigned" /> </id> <property name="jdName" > </property> <many-to-one name="qx" column="qxid" class="QX"/> </class> </hibernate-mapping>
demol.java代码
package sec.test;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import second.JD;
import second.QX;
public class Demol {
@Test
public void add(){
Configuration cfg=new Configuration();
cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session se=sf.openSession();
Transaction tr=se.beginTransaction();
try{
QX q=new QX();
JD j1=new JD();
JD j2=new JD();
JD j3=new JD();
q.setQxName("长安区");
j1.setJdid(61002901L);
j1.setJdName("五星");
j1.setQx(q);
j2.setJdid(61002902L);
j2.setJdName("栾镇");
j2.setQx(q);
j3.setJdid(61002903L);
j3.setJdName("土门");
j3.setQx(q);
Set s=new HashSet();
s.add(j1);
s.add(j2);
s.add(j3);
q.setJds(s);
se.save(q);
se.save(j1);
se.save(j2);
se.save(j3);
tr.commit();
}catch(Exception e){
if (null != tr)
tr.rollback();
}finally{
se.close();
}
}
@Test
public void delete(){
Configuration cfg=new Configuration();
cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session se=sf.openSession();
Transaction tr=se.beginTransaction();
try{
String hql="from JD where jdName=?";
System.out.println(tr);
Query query=se.createQuery(hql);
query.setString(0, "五星");
JD j=(JD)query.uniqueResult();
se.delete(j);
tr.commit();
}
catch(Exception e){
if (null != tr)
tr.rollback();
}finally{
se.close();
}
}
public static void main(String[] args) {
new Demol().delete();
}
}