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