1、1对多(1)
public class Brand {
private int bid;
private String bname;
private Settel = new HashSet();}
hbm.xml映射文件
<set name="tel" lazy="false" cascade="all">
<key column="bid"/>
<one-to-many class="com.hw.entity.Tel"/>
</set>
1对多(多)
public class Tel {
private int tid;
private String tname;
private Date datea;
private String filepath;
private int bid;}
hbm.xml映射文件按基础配置
2、业务层
业务层只需要(多)对象即可。
3、测试类
public class Test {public static void main(String[] args) {BrandServiceImpl bd = new BrandServiceImpl(); //实例化“一”对象Setset = new HashSet(); //“多”set集合
Tel t1 = new Tel("vivo9",null,null,3); //创建“多”单个对象
Tel t2 = new Tel("vivo8",null,null,3);
Tel t3 = new Tel("vivo7",null,null,3);
set.add(t1);set.add(t2);set.add(t3); //添加“多”对象到set
Brand b = new Brand("步步高",set); //创建“一”单个对象
bd.addBrand(b); //添加到数据库
}
}