---
title: "hibernate常用方法"
author: "神奇九寨沟"
date: "2008-10-30T14:31:31Z"
tags: []
slug: "hibernate"
draft: false
lastmod: "2008-10-30T14:31:31Z"
id: 260051
url: "https://webwork123.iteye.com/blog/260051"
---

1.主表A主键ID <------>对应字表B中字段main_id
保存表B到数据库时候肯定会保存表A对象,有3种方法得到表A对象


方法： 
            fiveRule.setUploadById(user.getEmployeeId());
            fiveRule.setUploadBy(user.getEmployeeName());
            fiveRule.setUploadDay(DateUtility.getCurrentDate());
            fiveRule.setUploadTime(DateUtility.getCurrentTime());
        //session保存完对象fiveRule后，对象就留在了session缓存中,此时能得到这条对象的主键ID值
               <span style="color:red">session.save(fiveRule);</span>   
//           log.info(fiveRule.getId());
//            log.info(item1.getWeight());
//这里得到fiveRule对象除了上面那种方法外,此外<span style="color:red">还可以</span>直接
// <span style="color:red">BizFiveRule f = new BizFiveRule(); f.setId("具体的值")（主键值）;</span>
// <span style="color:red">还可以</span>根据具体的ID值<span style="color:red">session.load()</span>一遍,效果和直接setId()是一模一样的 
            item1.setBizFiveRule(<span style="color:red">fiveRule</span>);
            item1.setWeight(String.valueOf(dweight));
     
        <span style="color:red">    session.save(item1);</span>
             <span style="color:red">tx.commit();</span>   //提交事务,把数据保存到数据库
             <span style="color:red">session.flush();</span> //辅助性的把session缓存中的东西再刷一遍,保证所有的记录保存到数据库中