码迷,mamicode.com
首页 > Web开发 > 详细

笔记:Hibernate 持久化类标注示例-双向1-N关系映射

时间:2017-01-30 21:59:51      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:ova   关系   cep   active   public   manage   getter   tde   als   

  • 1-N关系的1端持久化类

    package org.drsoft.hibernate.model.oneToMany;

    ? ?

    import java.util.Date;

    import java.util.Set;

    ? ?

    import javax.persistence.*;

    ? ?

    @Entity

    @Table(name = "OneToMany_One")

    public class OneToManyForOneModel {

    @Id

    @Column(name = "Record_ID")

    @GeneratedValue(strategy = GenerationType.IDENTITY)

    private int recordID;

    ? ?

    @Column(name = "Total_Trade_Amt")

    private long totalTradeAmt;

    ? ?

    @Column(name = "Modify_Time")

    private Date modifyTime;

    ? ?

    @Column(name = "Create_Time", insertable = false, updatable = false)

    private Date createTime;

    ? ?

    @OneToMany(cascade = {

    CascadeType.PERSIST }, fetch = FetchType.LAZY, targetEntity = OneToManyForManyModel.class, orphanRemoval = false,mappedBy="oneToManyForOneModel")

    private Set<OneToManyForManyModel> details;

    ? ?

    省略setter和getter

    }

? ?

  • 1-N关系的N端持久化类

    package org.drsoft.hibernate.model.oneToMany;

    ? ?

    import java.util.Date;

    ? ?

    import javax.persistence.*;

    ? ?

    @Entity

    @Table(name = "OneToMany_Many")

    public class OneToManyForManyModel {

    ? ?

    @Id

    @Column(name = "Detail_ID")

    @GeneratedValue(strategy = GenerationType.IDENTITY)

    private int detailID;

    ? ?

    @Column(name = "Trade_Amt")

    private long tradeAmt;

    ? ?

    @Column(name = "Create_Time", insertable = false, updatable = false)

    private Date createTime;

    ? ?

    @ManyToOne

    @JoinColumn(name = "Ref_Record_ID", referencedColumnName = "Record_ID")

    private OneToManyForOneModel oneToManyForOneModel;

    ? ?

    省略setter和getter

    }

    ? ?

  • 测试调用

    package org.drsoft.hibernate.manager.oneToMany;

    ? ?

    import java.util.*;

    ? ?

    import org.drsoft.hibernate.model.oneToMany.*;

    import org.drsoft.utils.hibernateUtils;

    import org.hibernate.Session;

    import org.hibernate.Transaction;

    ? ?

    public class OneToManyManager {

    public static void main(String[] args) {

    Session curSession = hibernateUtils.openSession();

    Transaction curTransaction = null;

    Calendar calendar = Calendar.getInstance(Locale.CHINA);

    try {

    System.out.println("#1 创建 One 和 Many数据");

    // #1 创建 One 和 Many数据

    OneToManyForOneModel oneToManyForOneModel = new OneToManyForOneModel();

    ? ?

    long totalTradeAmt = 0;

    for (int i = 0; i < 5; i++) {

    OneToManyForManyModel oneToManyForManyModel = new OneToManyForManyModel();

    oneToManyForManyModel.setTradeAmt(calendar.get(Calendar.SECOND) + i);

    totalTradeAmt = totalTradeAmt + oneToManyForManyModel.getTradeAmt();

    if (oneToManyForOneModel.getDetails() == null) {

    oneToManyForOneModel.setDetails(new HashSet<OneToManyForManyModel>());

    System.out.println("创建 HashSet<OneToManyForManyModel>()");

    }

    oneToManyForOneModel.getDetails().add(oneToManyForManyModel);

    }

    ? ?

    oneToManyForOneModel.setTotalTradeAmt(totalTradeAmt);

    oneToManyForOneModel.setModifyTime(new Date());

    ? ?

    curTransaction = hibernateUtils.beginTransaction(curSession);

    curSession.persist(oneToManyForOneModel);

    curTransaction.commit();

    ? ?

    System.out.println("#2 查询 One 数据");

    // #2 查询 One 数据

    oneToManyForOneModel = (OneToManyForOneModel) curSession.load(OneToManyForOneModel.class, new Integer(1));

    System.out.println("OneToManyForOneModel RecordID=" + oneToManyForOneModel.getRecordID()

    + "\tTotalTradeAmt=" + oneToManyForOneModel.getTotalTradeAmt());

    ? ?

    Set<OneToManyForManyModel> details = oneToManyForOneModel.getDetails();

    Iterator<OneToManyForManyModel> it = details.iterator();

    while (it.hasNext()) {

    OneToManyForManyModel obj = it.next();

    System.out.println(

    "OneToManyForManyModel DetailID=" + obj.getDetailID() + "\tTradeAmt=" + obj.getTradeAmt());

    }

    } catch (Exception e) {

    if (curTransaction != null && curTransaction.isActive()) {

    curTransaction.rollback();

    }

    ? ?

    throw e;

    } finally {

    if (curSession != null) {

    curSession.close();

    }

    }

    }

    }

    ? ?

? ?

笔记:Hibernate 持久化类标注示例-双向1-N关系映射

标签:ova   关系   cep   active   public   manage   getter   tde   als   

原文地址:http://www.cnblogs.com/li3807/p/6358383.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!