筆記:Hibernate 持久化類標註樣本-雙向1-N關係映射

來源:互聯網
上載者:User

標籤: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關係映射

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.