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;
此致敬意。
http://stackoverflow.com/questions/2986318/calculated-property-with-jpa-hibernate – 2014-09-02 10:27:23
感谢您的链接。我读过它并感到困惑 - 它是HQL还是'@ formula'注释中的本地SQL。我所需要的只是尝试。 – gooamoko 2014-09-02 13:37:58