现在的位置: 首页 > 综合 > 正文

使用hibernate 提取属性 hibernate 注解@Formula

2018年05月09日 ⁄ 综合 ⁄ 共 2371字 ⁄ 字号 评论关闭

1 关于hibernate的@Formula用法和作用可以参照别的文章

2 hibernate提取属性也叫做计算属性,该属性值是一个值读属性,是通过使用sql语句获取得到的,常用的是统计数据

3 案例:我有一个employee实体类,该类有主键Id或name,月薪水(monthlySalary)等属性(字段),你现在有一个想法想要获取该employee的年薪,计算方式是月薪*12个月=年薪。

package net.ozar.exp.entity;
 
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
 
@Entity
@Table(name="EMPLOYEE")
public class Employee implements java.io.Serializable {
 
    private static final long serialVersionUID = -7311873726885796936L;
 
    @Id
    @Column(name="ID")
    private Integer id;
 
    @Column(name="FIRST_NAME", length=31)
    private String firstName;
 
    @Column(name="LAST_NAME", length=31)
    private String lastName;
 
    @Column(name="MONTHLY_SALARY")
    private float monthlySalary;
 
    public Employee() {
    }
 
    // getters and setters
     // ...
 
    public float getMonthlySalary() {
        return monthlySalary;
    }
 
    public void setMonthlySalary(float monthlySalary) {
        this.monthlySalary = monthlySalary;
    }
 
     /* This artificial property - as I call it - is a kind of a calculated property, but not with Hibernate derived property support - not just yet */
    public float getYearlySalary() {
        return this.monthlySalary * 12;
    }
 
}

4 上面这个是没有加入hibernate @@Formula支持的用法,现在我们加入这个注解实现:

package net.ozar.exp.entity;
 
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
 
import org.hibernate.annotations.Formula;
 
@Entity
@Table(name="EMPLOYEE")
public class Employee implements java.io.Serializable {
 
    private static final long serialVersionUID = -7311873726885796936L;
 
    @Id
    @Column(name="ID")
    private Integer id;
 
    @Column(name="FIRST_NAME", length=31)
    private String firstName;
 
    @Column(name="LAST_NAME", length=31)
    private String lastName;
 
    @Column(name="MONTHLY_SALARY")
    private float monthlySalary;
 
    @Formula("MONTHLY_SALARY*12")
        private float yearlySalary;
 
    public Employee() {
    }
 
    public Integer getId() {
        return id;
    }
 
    public void setId(Integer id) {
        this.id = id;
    }
 
    public String getFirstName() {
        return firstName;
    }
 
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
 
    public String getLastName() {
        return lastName;
    }
 
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
 
    public float getMonthlySalary() {
        return monthlySalary;
    }
 
    public void setMonthlySalary(float monthlySalary) {
        this.monthlySalary = monthlySalary;
    }
 
    public float getYearlySalary() {
        return yearlySalary;
    }
 
}

5 注意到上面这个案例的@Formla的值是关联到“MONTHLY_SALARY”而yearlySalry属性是没有存储如数据库的。

6 一些更为复杂用法

@Formula("(select min(l.creation_date) from Logs l where l.customer_id = id)")
private Date firstLoginDate;

注意:最後面的customer_id=id中后面的id的值也就当前对象的id值

抱歉!评论已关闭.