Does Hibernate4.1 still support @Formula annotation?

喜你入骨 提交于 2019-12-12 05:16:45

问题


I was trying to use @Formula annotation in one of my Hibernate4.1 entity classes. From some articles I learnt that this annotation need to be placed upon the property definition, and so I did. But nothing happened. No matter how simple the formula content was, Hibernate just kept putting the property directly into generated sql, and then threw an "invalid identifier" exception, because obviously there was no corresponding column with that name in the physical table.

There are only a few documentations that I could find about how to use @Formula, but none of them talked about whether or not it is still supported in Hibernate4. So does this question has a certain answer? And is it possible for me to use this annotation in one way or another?

Added contents: Here is the sample code:

package sample.model;

import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.Formula;


@Entity
@Table(name = "STAGE_TRACK", schema = "")
public class StageTrack implements java.io.Serializable {

    // Fields

    private BigDecimal id;
    private Date startTime;
    private Date endTime;

    @Formula("(END_TIME - START_TIME)*24*60")
    private BigDecimal duration;
    public BigDecimal getDuration() {
        return this.duration;

    }
    public void setDuration(BigDecimal duration) {
        this.duration = duration;
    }

    // Constructors
    /** default constructor */
    public StageTrack() {
    }

    /** minimal constructor */
    public StageTrack(BigDecimal id) {
        this.id = id;
    }

    /** full constructor */
    public StageTrack(BigDecimal id, Date startTime, Date endTime) {
        this.id = id;
        this.startTime = startTime;
        this.endTime = endTime;
    }

    // Property accessors
    @Id
    @Column(name = "ID", unique = true, nullable = false, precision = 22, scale = 0)
    public BigDecimal getId() {
        return this.id;
    }

    public void setId(BigDecimal id) {
        this.id = id;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "START_TIME", length = 7)
    public Date getStartTime() {
        return this.startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "END_TIME", length = 7)
    public Date getEndTime() {
        return this.endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

}

回答1:


You problem is that you place @Formula on a field, while other annotations are placed on getters, therefore @Formula is ignored.

You should choose one stragegy and apply it consistently (unless you explicily override it with @AccessType).

@Formula("(END_TIME - START_TIME)*24*60")
public BigDecimal getDuration() {
    return this.duration;

}


来源:https://stackoverflow.com/questions/14441570/does-hibernate4-1-still-support-formula-annotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!