函数结果AS类字段值

时间:2014-09-02 06:56:52

标签: java hibernate postgresql

我有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函数的结果设置为monthMBytes的字段值,如

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

最好的问候。

1 个答案:

答案 0 :(得分:1)

我阅读了相关的SO问题,一些文件和......

  1. 我需要让字段非瞬态。
  2. 我不需要现场制定者。只有getter才能获得。
  3. 在@Formula注释中,我编写sql查询,从函数中选择值。
  4. PostgreSQL函数必须返回 NOT NULL 值。
  5. 所以,我的新功能是

    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;
      }
      . . .
    

    现在一切正常。感谢您的链接和评论。