level 类:
package hbm;
import java.util.Set;
/**
* @hibernate.class table = "level"
* where = " visible = 0 "
* @author Administrator
*
*/
public class Level {
private long id ;
private String name ;
private Level father ;
private Set<Level> childSet ;
private Set<Level> mmfathers ;
private Set<Level> mmchilds ;
private int visible ;
public Level(){}
public Level(String name){ this.name = name ; }
public Level(String name,int visible){this.visible = visible; this.name = name ; }
/**
* @hibernate.id generator-class = "identity"
* @return
*/
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
/**
* @hibernate.property
* length = "20"
* @return
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
* @hibernate.many-to-one
* cascade = "save-update"
* inverse = "false"
* column = "fid"
* @return
*/
public Level getFather() {
return father;
}
public void setFather(Level father) {
this.father = father;
}
/**
* @hibernate.set
* lazy = "true"
* table = "Level"
* cascade = "save-update"
* where = " visible = 0 "
* @hibernate.collection-key column = "fid"
* @hibernate.collection-one-to-many class = "hbm.Level"
* @return
*/
public Set<Level> getChildSet() {
return childSet;
}
public void setChildSet(Set<Level> childSet) {
this.childSet = childSet;
}
/**
* @hibernate.property
* @return
*/
public int getVisible() {
return visible;
}
public void setVisible(int visible) {
this.visible = visible;
}
/**
* @hibernate.set
* table = "level2level_mm"
* lazy = "true"
* inverse = "false"
* cascade = "save-update"
* @hibernate.collection-key column = "mmchilds"
* @hibernate.collection-many-to-many
* class = "hbm.Level"
* column = "mmfathers"
* @return
*/
public Set<Level> getMmfathers() {
return mmfathers;
}
public void setMmfathers(Set<Level> mmfathers) {
this.mmfathers = mmfathers;
}
/**
* @hibernate.set
* table = "level2level_mm"
* lazy = "true"
* inverse = "false"
* cascade = "save-update"
* @hibernate.collection-key column = "mmfathers"
* @hibernate.collection-many-to-many
* class = "hbm.Level"
* column = "mmchilds"
* @return
*/
public Set<Level> getMmchilds() {
return mmchilds;
}
public void setMmchilds(Set<Level> mmchilds) {
this.mmchilds = mmchilds;
}
}
测试类:
package test;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import hbm.Level;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Assert;
import org.junit.Test;
import unit.HibernateUtil;
public class HbnUnit {
@Test //树形结构 数据添加
public void level() throws Exception {
Session session = HibernateUtil.currentSession();
Transaction tr = session.beginTransaction();
Level level = new Level();
level.setName("f1");
Set<Level> set = new HashSet<Level>();
set.add(new Level("c1"));
set.add(new Level("c2",1));
set.add(new Level("c3"));
set.add(new Level("c4",1 ));
set.add(new Level("c5" ));
set.add(new Level("c6",1 ));
level.setChildSet(set);
session.save(level);
session.flush() ;
session.clear();
tr.commit();
}
@Test //树形结构 数据验证
public void sAll() throws Exception {
Session session = HibernateUtil.currentSession();
List<Level> list = session.createQuery(" from Level tl where tl.father is null ").list();
for( Level tmp : list ){
System.out.println( tmp.getName()+"====="+(tmp.getVisible()==0) );
Assert.assertTrue(" 查出删除数据 ", tmp.getVisible()==0);
for( Level tt : tmp.getChildSet() ){
Assert.assertTrue(" 查出删除数据 ", tt.getVisible()==0);
}
}
Assert.assertTrue(" 没有查出数据 ", list.size()>0 );
session.clear();
}
public static long l1mm =0l ;
public static long l2mm =0l ;
public static long l3mm =0l ;
public static long l5mm =0l ;
@Test //多对多结构 数据添加
public void contact() throws Exception {
Session session = HibernateUtil.currentSession();
Transaction tr = session.beginTransaction();
// 1 对 2,3 / 2 对 3,4 / 5 对 1,2,3,4
Level level1 = new Level();
level1.setName("f1");
Level level2 = new Level();
level2.setName("f2");
Level level3 = new Level();
level3.setName("f3");
Level level4 = new Level();
level4.setName("f4");
Level level5 = new Level();
level5.setName("f5");
level1.setMmchilds(new HashSet<Level>());
level1.getMmchilds().add(level2);//2
level1.getMmchilds().add(level3);
level2.setMmchilds(new HashSet<Level>());
level2.setMmfathers(new HashSet<Level>());
level2.getMmchilds().add(level3);//1
level2.getMmfathers().add(level4);//1
level3.setMmfathers(new HashSet<Level>());
level3.getMmfathers().add(level4);//1
level5.setMmchilds(new HashSet<Level>());
level5.getMmchilds().add(level1);//4
level5.getMmchilds().add(level2);
level5.getMmchilds().add(level3);
level5.getMmchilds().add(level4);
session.save(level1);
session.save(level2);
session.save(level3);
session.save(level4);
session.save(level5);
tr.commit();
l1mm = level1.getId() ;
l2mm = level2.getId() ;
l3mm = level3.getId() ;
l5mm = level5.getId() ;
session.clear();
}
@Test //多对多结构 数据添加
public void contactTest() throws Exception {
Session session = HibernateUtil.currentSession();
Level level1 = (Level)session.get(Level.class, l1mm);
System.out.println(" level1 --- ");
System.out.println( level1.getMmchilds().size() );
System.out.println( level1.getMmfathers()!=null?level1.getMmfathers().size():0 );
Level level2 = (Level)session.get(Level.class, l2mm);
System.out.println(" level2 --- ");
System.out.println( level2.getMmchilds().size() );
System.out.println( level2.getMmfathers()!=null?level2.getMmfathers().size():0 );
Level level3 = (Level)session.get(Level.class, l3mm);
System.out.println(" level3 --- ");
System.out.println( level3.getMmchilds()!=null?level3.getMmchilds().size():0 );
System.out.println( level3.getMmfathers()!=null?level3.getMmfathers().size():0 );
Level level5 = (Level)session.get(Level.class, l5mm);
System.out.println(" level5 --- ");
System.out.println( level5.getMmchilds().size() );
System.out.println( level5.getMmfathers()!=null?level5.getMmfathers().size():0 );
}
}
hbm 配置结构:
<property name="connection.driver_class">
org.gjt.mm.mysql.Driver
</property>
<property name="connection.username">root</property>
<property name="connection.url">
<![CDATA[
jdbc:mysql://localhost:3306/test?characterEncoding=utf8&connectionCollation=gbk_chinese_ci
]]>
</property>
<property name="dialect">
org.hibernate.dialect.MySQLInnoDBDialect
</property>
<property name="show_sql">true</property>
<!-- JDBC connection pool (use the built-in) -->
<!-- Mapping files -->
<mapping resource="hbm/Level.hbm.xml" />
结果:
Hibernate: insert into level (name, fid, visible) values (?, ?, ?)
Hibernate: insert into level (name, fid, visible) values (?, ?, ?)
Hibernate: insert into level (name, fid, visible) values (?, ?, ?)
Hibernate: insert into level (name, fid, visible) values (?, ?, ?)
Hibernate: insert into level (name, fid, visible) values (?, ?, ?)
Hibernate: insert into level (name, fid, visible) values (?, ?, ?)
Hibernate: insert into level (name, fid, visible) values (?, ?, ?)
Hibernate: update level set fid=? where id=?
Hibernate: update level set fid=? where id=?
Hibernate: update level set fid=? where id=?
Hibernate: update level set fid=? where id=?
Hibernate: update level set fid=? where id=?
Hibernate: update level set fid=? where id=?
Hibernate: select level0_.id as id0_, level0_.name as name0_, level0_.fid as fid0_, level0_.visible as visible0_ from level level0_ where ( level0_.visible = 0 ) and (level0_.fid is null)
f1=====true
Hibernate: select childset0_.fid as fid1_, childset0_.id as id1_, childset0_.id as id0_0_, childset0_.name as name0_0_, childset0_.fid as fid0_0_, childset0_.visible as visible0_0_ from level childset0_ where ( childset0_.visible = 0 ) and childset0_.fid=?
Hibernate: insert into level (name, fid, visible) values (?, ?, ?)
Hibernate: insert into level (name, fid, visible) values (?, ?, ?)
Hibernate: insert into level (name, fid, visible) values (?, ?, ?)
Hibernate: insert into level (name, fid, visible) values (?, ?, ?)
Hibernate: insert into level (name, fid, visible) values (?, ?, ?)
Hibernate: insert into level2level_mm (mmfathers, mmchilds) values (?, ?)
Hibernate: insert into level2level_mm (mmfathers, mmchilds) values (?, ?)
Hibernate: insert into level2level_mm (mmchilds, mmfathers) values (?, ?)
Hibernate: insert into level2level_mm (mmchilds, mmfathers) values (?, ?)
Hibernate: insert into level2level_mm (mmfathers, mmchilds) values (?, ?)
Hibernate: insert into level2level_mm (mmfathers, mmchilds) values (?, ?)
Hibernate: insert into level2level_mm (mmfathers, mmchilds) values (?, ?)
Hibernate: insert into level2level_mm (mmfathers, mmchilds) values (?, ?)
Hibernate: insert into level2level_mm (mmfathers, mmchilds) values (?, ?)
Hibernate: select level0_.id as id0_0_, level0_.name as name0_0_, level0_.fid as fid0_0_, level0_.visible as visible0_0_ from level level0_ where level0_.id=? and ( level0_.visible = 0 )
level1 ---
Hibernate: select mmchilds0_.mmfathers as mmfathers0_, mmchilds0_.mmchilds as mmchilds0_ from level2level_mm mmchilds0_ where mmchilds0_.mmfathers=?
2
Hibernate: select mmfathers0_.mmchilds as mmchilds0_, mmfathers0_.mmfathers as mmfathers0_ from level2level_mm mmfathers0_ where mmfathers0_.mmchilds=?
1
Hibernate: select level0_.id as id0_0_, level0_.name as name0_0_, level0_.fid as fid0_0_, level0_.visible as visible0_0_ from level level0_ where level0_.id=? and ( level0_.visible = 0 )
level2 ---
Hibernate: select mmchilds0_.mmfathers as mmfathers0_, mmchilds0_.mmchilds as mmchilds0_ from level2level_mm mmchilds0_ where mmchilds0_.mmfathers=?
1
Hibernate: select mmfathers0_.mmchilds as mmchilds0_, mmfathers0_.mmfathers as mmfathers0_ from level2level_mm mmfathers0_ where mmfathers0_.mmchilds=?
3
Hibernate: select level0_.id as id0_0_, level0_.name as name0_0_, level0_.fid as fid0_0_, level0_.visible as visible0_0_ from level level0_ where level0_.id=? and ( level0_.visible = 0 )
level3 ---
Hibernate: select mmchilds0_.mmfathers as mmfathers0_, mmchilds0_.mmchilds as mmchilds0_ from level2level_mm mmchilds0_ where mmchilds0_.mmfathers=?
0
Hibernate: select mmfathers0_.mmchilds as mmchilds0_, mmfathers0_.mmfathers as mmfathers0_ from level2level_mm mmfathers0_ where mmfathers0_.mmchilds=?
4
Hibernate: select level0_.id as id0_0_, level0_.name as name0_0_, level0_.fid as fid0_0_, level0_.visible as visible0_0_ from level level0_ where level0_.id=? and ( level0_.visible = 0 )
level5 ---
Hibernate: select mmchilds0_.mmfathers as mmfathers0_, mmchilds0_.mmchilds as mmchilds0_ from level2level_mm mmchilds0_ where mmchilds0_.mmfathers=?
4
Hibernate: select mmfathers0_.mmchilds as mmchilds0_, mmfathers0_.mmfathers as mmfathers0_ from level2level_mm mmfathers0_ where mmfathers0_.mmchilds=?
0