2014-09-02 73 views
1

我有PostgreSQL数据库,其中有一个功能month_traffic()功能结果

CREATE FUNCTION month_traffic(hostcode int) RETURNS numeric(14,2) AS $$ 
DECLARE 
start_date date := CAST((EXTRACT(year FROM now()) || '-' || EXTRACT(month FROM now()) || '-' || '01') AS date); 
end_date date := CAST((EXTRACT(year FROM now()) || '-' || EXTRACT(month FROM now()) + 1 || '-' || '01') AS date); 
BEGIN 
RETURN (SELECT SUM(day_mbytes) FROM daily WHERE (day_hstcode = hostcode) AND (day_date >= start_date) AND (day_date < end_date)); 
END; 
$$ LANGUAGE plpgsql; 

我也有Host

package ru.gooamoko.model; 

import java.io.Serializable; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.ManyToOne; 
import javax.persistence.Table; 
import org.hibernate.annotations.Formula; 

@Entity 
@Table(name="hosts") 
public class Host implements Serializable { 

    @Id 
    @Column(name="hst_pcode") 
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    private int id; 

    @ManyToOne 
    @JoinColumn(name="hst_grpcode", insertable=false, updatable=false) 
    private Department group; 

    @Column(name="hst_description", length=50, nullable=false) 
    private String description; 

    @Column(name="hst_net", nullable=false) 
    private short net; 

    @Column(name="hst_addr", nullable=false) 
    private short addr; 

    @Column(name="hst_ballance", nullable=false, columnDefinition="numeric(11,2) NOT NULL DEFAULT 0") 
    private float ballance; 

    @Column(name="hst_price", nullable=false, columnDefinition="numeric(5,2) NOT NULL DEFAULT 0") 
    private float price; 

    @Column(name="hst_enabled", nullable=false, columnDefinition="boolean NOT NULL DEFAULT false") 
    private boolean enabled; 

    @Column(name="hst_still", nullable=false, columnDefinition="boolean NOT NULL DEFAULT false") 
    private boolean steel; 

    // daily traffic 
    private transient float dayKBytes = 0; 
    // monthly traffic 
    private transient float monthMBytes = 0; 
    // Setters and getters 
    // . . . 
} 

是否有可能设置PostgreSQL函数的结果作为月份字段的值如

// month traffic 
@Formula("(select month_traffic(hst_pcode)") 
private transient float monthMBytes = 0; 

此致敬意。

+0

http://stackoverflow.com/questions/2986318/calculated-property-with-jpa-hibernate – 2014-09-02 10:27:23

+0

感谢您的链接。我读过它并感到困惑 - 它是HQL还是'@ formula'注释中的本地SQL。我所需要的只是尝试。 – gooamoko 2014-09-02 13:37:58

回答

1

我看了那么相关的问题,一些文档和...

  1. 我需要让现场的非暂时性的。
  2. 我不需要field setter。只有getter足够了。
  3. 在@Formula注释中,我编写了从函数中选择值的sql查询。
  4. PostgreSQL函数必须返回NOT NULL值。

所以,我的新功能是

CREATE OR REPLACE FUNCTION month_traffic(hostcode int) RETURNS numeric(14,2) AS $$ 
DECLARE 
    begin_date date := CAST((EXTRACT(year FROM now()) || '-' || EXTRACT(month FROM now()) || '-' || '01') AS date); 
    end_date date := CAST((EXTRACT(year FROM now()) || '-' || EXTRACT(month FROM now()) + 1 || '-' || '01') AS date); 
    result numeric(14,2) := 0; 
BEGIN 
    result := (SELECT SUM(day_mbytes) FROM daily 
    WHERE (day_hstcode = hostcode) AND (day_date >= begin_date) AND (day_date < end_date)); 
    IF (result IS NULL) THEN 
    result := 0; 
    END IF; 
    RETURN result; 
END; 
$$ LANGUAGE plpgsql 

和我的注释字段是

. . . 
    @Formula("(select month_traffic(hst_pcode))") 
    private float monthMBytes; 

    public float getMonthMBytes() { 
    return monthMBytes; 
    } 
    . . . 

而现在一切工作正常。感谢您的链接和意见。