本站首页    管理页面    写新日志    退出


«January 2020»
1234
567891011
12131415161718
19202122232425
262728293031


公告

不知不觉6年了


我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:〾堯仸〾的天空
日志总数:139
评论数量:503
留言数量:16
访问次数:2644005
建立时间:2005年4月23日




[java]hibernate many 2 many 的配置
原创空间,  软件技术

〾堯仸〾 发表于 2008-3-13 15:37:44

//实体 帐单public class Agrpribill implements java.io.Serializable {    // Fields    private Long agrpribillid;    private Set<Payinvoice> payinvoices = new HashSet<Payinvoice>(0);         ....              public void addAgrpribill(Agrpribill agrpribill){            if(!agrpribills.contains(agrpribill)) {                agrpribills.add(agrpribill);                agrpribill.addpayinvoice(this);            }        }    }        xml 配置文件       <set name="agrpribills" table="AgrPriBillPayInvoice">            <key>                <column name="PAYINVOICEID" />              </key>            <many-to-many  column="agrpribillid" class="com.intohotel.Agrpribill" />   </set>                //实体 发票     public class Payinvoice implements java.io.Serializable {    // Fields    private Long payinvoiceid;    private String invoicecode;    private Date recvdate;    private String recvoperator;    private Set agrpribills  = new HashSet(0);        ....        public void addpayinvoice(Payinvoice payinvoice){            this.payinvoices.add(payinvoice);    }        }        xml  文件               <set name="payinvoices"  table="AgrPriBillPayInvoice">            <key>              <column name="agrpribillid"/>            </key>            <many-to-many  column="payinvoiceid" class="com.intohotel.Payinvoice" />        </set>                    //---------------------------------------------------------    @Test    public void testSavePayAndArg(){        Agrpribill arg = new Agrpribill();        arg.setBillno("1111111");        //agrpribillDAO.save(arg);        Payinvoice  payinvoice  = new Payinvoice();        payinvoice.setInvoicecode("code1111 ");        payinvoice.setRecvdate(new Date());        payinvoice.setRecvoperator("销售员111111");        //payinvoice.addAgrpribill(arg);        payinvoice.getAgrpribills().add(arg);        payinvoiceDAO.save(payinvoice);                报错         5:04:24,484 DEBUG SQL:346 - select S_PAYINVOICE.nextval from dual      15:04:24,656 DEBUG SQL:346 - insert into PAYINVOICE (INVOICECODE, RECVDATE, RECVOPERATOR, payinvoiceid) values (?, ?, ?, ?)      15:04:24,687 DEBUG SQL:346 - insert into AgrPriBillPayInvoice (PAYINVOICEID, agrpribillid) values (?, ?)   //----------------------------------------------------------          @Test    public void testSavePayAndArg(){        Agrpribill arg = new Agrpribill();        arg.setBillno("1111111");        agrpribillDAO.save(arg);        Payinvoice  payinvoice  = new Payinvoice();        payinvoice.setInvoicecode("code1111 ");        payinvoice.setRecvdate(new Date());        payinvoice.setRecvoperator("销售员111111");        //payinvoice.addAgrpribill(arg);        payinvoice.getAgrpribills().add(arg);        payinvoiceDAO.save(payinvoice);    }    //成功插入    15:06:01,015 DEBUG SQL:346 - select S_AGRPRIBILL.nextval from dual    15:06:01,140 DEBUG SQL:346 - select S_PAYINVOICE.nextval from dual    15:06:01,171 DEBUG SQL:346 - insert into AGRPRIBILL (PAYAFFAIR, OCCURDATE, SUMMONEY, BILLNO, SPCODE, CORPCODE, PAYTYPE, MANAGERCODE, CARDNO, NAME, IDCARDNO, REMARK, BILLTYPE, AUDITSTATE, AUDITOR, COPIER, LOGINCODE, CORPDEPART, UUID, INVOICELAUNCHDATE, INVOICESUMMONEY, JOURNEYFORM, BAOXIANFORM, FORMRECEIPTDATE, INVOICERECEIPTDATE, INVOICERECEIPTOR, STDSUMMONEY, REALOCCURDATE, SALSECODE, SALSEDEPART, agrpribillid) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)    15:06:01,171 DEBUG SQL:346 - insert into PAYINVOICE (INVOICECODE, RECVDATE, RECVOPERATOR, payinvoiceid) values (?, ?, ?, ?)    15:06:01,187 DEBUG SQL:346 - insert into AgrPriBillPayInvoice (PAYINVOICEID, agrpribillid) values (?, ?)    }        //---------------------------------------------------    Payinvoice.hbm.xml ,加入 inverse="true"   inverse的默认值为false        15:07:28,625 DEBUG SQL:346 - select S_AGRPRIBILL.nextval from dual    15:07:28,750 DEBUG SQL:346 - select S_PAYINVOICE.nextval from dual    15:07:28,781 DEBUG SQL:346 - insert into AGRPRIBILL (PAYAFFAIR, OCCURDATE, SUMMONEY, BILLNO, SPCODE, CORPCODE, PAYTYPE, MANAGERCODE, CARDNO, NAME, IDCARDNO, REMARK, BILLTYPE, AUDITSTATE, AUDITOR, COPIER, LOGINCODE, CORPDEPART, UUID, INVOICELAUNCHDATE, INVOICESUMMONEY, JOURNEYFORM, BAOXIANFORM, FORMRECEIPTDATE, INVOICERECEIPTDATE, INVOICERECEIPTOR, STDSUMMONEY, REALOCCURDATE, SALSECODE, SALSEDEPART, agrpribillid) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)    15:07:28,796 DEBUG SQL:346 - insert into PAYINVOICE (INVOICECODE, RECVDATE, RECVOPERATOR, payinvoiceid) values (?, ?, ?, ?)    //没有插入中间表              //--------------------------------------------  //反过来 ,一样ok       @Test    public void testSaveAgrAndPay() {        Payinvoice  payinvoice  = new Payinvoice();        payinvoice.setInvoicecode("code1111 ");        payinvoice.setRecvdate(new Date());        payinvoice.setRecvoperator("销售员111111");        payinvoiceDAO.save(payinvoice);        Agrpribill arg = new Agrpribill();        arg.setBillno("1111111");        arg.getPayinvoices().add(payinvoice);        agrpribillDAO.save(arg);    }    15:11:36,187 DEBUG SQL:346 - select S_PAYINVOICE.nextval from dual    15:11:36,328 DEBUG SQL:346 - select S_AGRPRIBILL.nextval from dual    15:11:36,375 DEBUG SQL:346 - insert into PAYINVOICE (INVOICECODE, RECVDATE, RECVOPERATOR, payinvoiceid) values (?, ?, ?, ?)    15:11:36,375 DEBUG SQL:346 - insert into AGRPRIBILL (PAYAFFAIR, OCCURDATE, SUMMONEY, BILLNO, SPCODE, CORPCODE, PAYTYPE, MANAGERCODE, CARDNO, NAME, IDCARDNO, REMARK, BILLTYPE, AUDITSTATE, AUDITOR, COPIER, LOGINCODE, CORPDEPART, UUID, INVOICELAUNCHDATE, INVOICESUMMONEY, JOURNEYFORM, BAOXIANFORM, FORMRECEIPTDATE, INVOICERECEIPTDATE, INVOICERECEIPTOR, STDSUMMONEY, REALOCCURDATE, SALSECODE, SALSEDEPART, agrpribillid) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)    15:11:36,390 DEBUG SQL:346 - insert into AgrPriBillPayInvoice (agrpribillid, payinvoiceid) values (?, ?)///====================================// 删除 ,主控制方 //=====================================    @Test    public void testDelPay(){        Payinvoice  payinvoice  = payinvoiceDAO.findById(56L);        payinvoiceDAO.delete(payinvoice);    }    15:18:17,046 DEBUG SQL:346 - select payinvoice0_.payinvoiceid as payinvoi1_0_0_, payinvoice0_.INVOICECODE as INVOICEC2_0_0_, payinvoice0_.RECVDATE as RECVDATE0_0_, payinvoice0_.RECVOPERATOR as RECVOPER4_0_0_ from PAYINVOICE payinvoice0_ where payinvoice0_.payinvoiceid=?    15:18:17,234 DEBUG SQL:346 - delete from AgrPriBillPayInvoice where PAYINVOICEID=?    15:18:17,250 DEBUG SQL:346 - delete from PAYINVOICE where payinvoiceid=?   //没有删除 agr            //------------------------------   // 删除 被空方   //==============================       @Test    public void testDelAgr(){        Agrpribill arg =  agrpribillDAO.findById(52L);        agrpribillDAO.delete(arg);    }    15:21:42,625 DEBUG SQL:346 - select agrpribill0_.agrpribillid as agrpribi1_2_0_, agrpribill0_.PAYAFFAIR as PAYAFFAIR2_0_, agrpribill0_.OCCURDATE as OCCURDATE2_0_, agrpribill0_.SUMMONEY as SUMMONEY2_0_, agrpribill0_.BILLNO as BILLNO2_0_, agrpribill0_.SPCODE as SPCODE2_0_, agrpribill0_.CORPCODE as CORPCODE2_0_, agrpribill0_.PAYTYPE as PAYTYPE2_0_, agrpribill0_.MANAGERCODE as MANAGERC9_2_0_, agrpribill0_.CARDNO as CARDNO2_0_, agrpribill0_.NAME as NAME2_0_, agrpribill0_.IDCARDNO as IDCARDNO2_0_, agrpribill0_.REMARK as REMARK2_0_, agrpribill0_.BILLTYPE as BILLTYPE2_0_, agrpribill0_.AUDITSTATE as AUDITSTATE2_0_, agrpribill0_.AUDITOR as AUDITOR2_0_, agrpribill0_.COPIER as COPIER2_0_, agrpribill0_.LOGINCODE as LOGINCODE2_0_, agrpribill0_.CORPDEPART as CORPDEPART2_0_, agrpribill0_.UUID as UUID2_0_, agrpribill0_.INVOICELAUNCHDATE as INVOICE21_2_0_, agrpribill0_.INVOICESUMMONEY as INVOICE22_2_0_, agrpribill0_.JOURNEYFORM as JOURNEY23_2_0_, agrpribill0_.BAOXIANFORM as BAOXIAN24_2_0_, agrpribill0_.FORMRECEIPTDATE as FORMREC25_2_0_, agrpribill0_.INVOICERECEIPTDATE as INVOICE26_2_0_, agrpribill0_.INVOICERECEIPTOR as INVOICE27_2_0_, agrpribill0_.STDSUMMONEY as STDSUMM28_2_0_, agrpribill0_.REALOCCURDATE as REALOCC29_2_0_, agrpribill0_.SALSECODE as SALSECODE2_0_, agrpribill0_.SALSEDEPART as SALSEDE31_2_0_ from AGRPRIBILL agrpribill0_ where agrpribill0_.agrpribillid=?    15:21:42,812 DEBUG SQL:346 - delete from AGRPRIBILL where agrpribillid=?     //没有删除 中间表                     //====================================     // 主控删除三个表      //====================================         @Test     public void tsetDelall(){        Payinvoice  payinvoice  = payinvoiceDAO.findById(53L);        Set<Agrpribill> agrpribills  = payinvoice.getAgrpribills();        for(Agrpribill agr :agrpribills) {            agr.getPayinvoices().remove(payinvoice);        }        payinvoiceDAO.delete(payinvoice);    }        15:31:38,765 DEBUG SQL:346 - select payinvoice0_.payinvoiceid as payinvoi1_0_0_, payinvoice0_.INVOICECODE as INVOICEC2_0_0_, payinvoice0_.RECVDATE as RECVDATE0_0_, payinvoice0_.RECVOPERATOR as RECVOPER4_0_0_ from PAYINVOICE payinvoice0_ where payinvoice0_.payinvoiceid=?    15:31:38,906 DEBUG SQL:346 - select agrpribill0_.PAYINVOICEID as PAYINVOI1_1_, agrpribill0_.agrpribillid as agrpribi2_1_, agrpribill1_.agrpribillid as agrpribi1_2_0_, agrpribill1_.PAYAFFAIR as PAYAFFAIR2_0_, agrpribill1_.OCCURDATE as OCCURDATE2_0_, agrpribill1_.SUMMONEY as SUMMONEY2_0_, agrpribill1_.BILLNO as BILLNO2_0_, agrpribill1_.SPCODE as SPCODE2_0_, agrpribill1_.CORPCODE as CORPCODE2_0_, agrpribill1_.PAYTYPE as PAYTYPE2_0_, agrpribill1_.MANAGERCODE as MANAGERC9_2_0_, agrpribill1_.CARDNO as CARDNO2_0_, agrpribill1_.NAME as NAME2_0_, agrpribill1_.IDCARDNO as IDCARDNO2_0_, agrpribill1_.REMARK as REMARK2_0_, agrpribill1_.BILLTYPE as BILLTYPE2_0_, agrpribill1_.AUDITSTATE as AUDITSTATE2_0_, agrpribill1_.AUDITOR as AUDITOR2_0_, agrpribill1_.COPIER as COPIER2_0_, agrpribill1_.LOGINCODE as LOGINCODE2_0_, agrpribill1_.CORPDEPART as CORPDEPART2_0_, agrpribill1_.UUID as UUID2_0_, agrpribill1_.INVOICELAUNCHDATE as INVOICE21_2_0_, agrpribill1_.INVOICESUMMONEY as INVOICE22_2_0_, agrpribill1_.JOURNEYFORM as JOURNEY23_2_0_, agrpribill1_.BAOXIANFORM as BAOXIAN24_2_0_, agrpribill1_.FORMRECEIPTDATE as FORMREC25_2_0_, agrpribill1_.INVOICERECEIPTDATE as INVOICE26_2_0_, agrpribill1_.INVOICERECEIPTOR as INVOICE27_2_0_, agrpribill1_.STDSUMMONEY as STDSUMM28_2_0_, agrpribill1_.REALOCCURDATE as REALOCC29_2_0_, agrpribill1_.SALSECODE as SALSECODE2_0_, agrpribill1_.SALSEDEPART as SALSEDE31_2_0_ from AgrPriBillPayInvoice agrpribill0_ left outer join AGRPRIBILL agrpribill1_ on agrpribill0_.agrpribillid=agrpribill1_.agrpribillid where agrpribill0_.PAYINVOICEID=?    15:31:38,953 DEBUG SQL:346 - select payinvoice0_.agrpribillid as agrpribi2_1_, payinvoice0_.payinvoiceid as payinvoi1_1_, payinvoice1_.payinvoiceid as payinvoi1_0_0_, payinvoice1_.INVOICECODE as INVOICEC2_0_0_, payinvoice1_.RECVDATE as RECVDATE0_0_, payinvoice1_.RECVOPERATOR as RECVOPER4_0_0_ from AgrPriBillPayInvoice payinvoice0_ left outer join PAYINVOICE payinvoice1_ on payinvoice0_.payinvoiceid=payinvoice1_.payinvoiceid where payinvoice0_.agrpribillid=?    15:31:39,015 DEBUG SQL:346 - delete from AgrPriBillPayInvoice where PAYINVOICEID=?    15:31:39,015 DEBUG SQL:346 - delete from PAYINVOICE where payinvoiceid=?        //--------------------------    // 删除中间表     //---------------------------        @Test     public void testDelArgPay() {        Payinvoice  payinvoice  = payinvoiceDAO.findById(58L);        payinvoice.setAgrpribills(null);        payinvoiceDAO.save(payinvoice);    }   //查询String hql  ="select agr.agrpribillid from AgrPriBill agr where not exists(select p from agr.payinvoices p) ";参考 http://www.javaeye.com/topic/154587


阅读全文(5233) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.031 second(s), page refreshed 144345889 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号