首页 > Web开发 > 详细

Hibernate设置派生属性(formula)

时间:2015-06-30 12:26:39      阅读:303      评论:0      收藏:0      [点我收藏+]

一、Customer中包含的字段:

private static final long serialVersionUID = 1L;
    private Integer id;
    private String sex;
    private double salary;
    private Double comm;
    private String birth;
    private Double total;
    private String desc;

二、Hibernate配置文件

<class name="Customer" table="customers" catalog="hjd">
        <id name="id" column="id">
            <generator class="native"></generator>
        </id>
        <property name="sex" length="4"></property>
        <property name="salary" />
        <property name="comm"/>
        <!-- 类中没有birth的get set 方法 -->
        <property name="birth" type="date" access="field"/>
        
        <!-- 映射Java持久化类中不存在的属性,只要用于HQL中,当数据库中有某列,二实体中不存在 -->
        <property name="deptName" type="string" access="noop"/>
        
        <property name="total" formula="(select c.salary+c.comm from customers c where c.id=id)"></property>
        <property name="desc" column="`desc`"/>
    </class>

三、测试

获得每个部门发出的工资总数

@Test
    public void getObject() {
        Session session = HiberSessionFactory.getSession();
        /*Customer c=(Customer) session.get(Customer.class, 2);
        System.out.println(c.getTotal());*/
        @SuppressWarnings("unchecked")
        List<Customer> list=session.createCriteria(Customer.class).add(Restrictions.eq("deptName", "军")).list();
        int total=0;
        for(Customer c:list){
            total+=c.getTotal();
        }
        System.out.println(total);
        HiberSessionFactory.closeSession();
    }

 

Hibernate设置派生属性(formula)

原文:http://www.cnblogs.com/ly-radiata/p/4462535.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!