我正試圖在我的一個Hibernate4.1實體類中使用@Formula註解。從一些文章中我瞭解到,這個註釋需要放在屬性定義上,所以我做了。但沒有發生。無論公式內容有多簡單,Hibernate只是直接將屬性放入生成的sql中,然後拋出一個「無效標識符」異常,因爲在物理表中顯然沒有與該名稱對應的列。Hibernate4.1是否仍支持@Formula註解?
我只能找到一些關於如何使用@Formula的文檔,但沒有人談論它是否仍然支持Hibernate4。那麼這個問題有一個特定的答案嗎?我能否以這種或那種方式使用這種註釋?
新增內容: 下面是示例代碼:
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;
}
}
向我們顯示您的代碼。 –