Hibernate4.1仍然支持@Formula注释吗? [英] Does Hibernate4.1 still support @Formula annotation?

查看:86
本文介绍了Hibernate4.1仍然支持@Formula注释吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的一个Hibernate4.1实体类中使用@Formula注释.从一些文章中,我了解到该注释需要放在属性定义上,因此我做到了.但是什么也没发生.无论公式内容多么简单,Hibernate都会继续将属性直接放入生成的sql中,然后抛出无效标识符"异常,因为显然物理表中没有与该名称对应的列.

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.

我只能找到一些有关如何使用@Formula的文档,但是没有人讨论过Hibernate4是否仍支持它.那么这个问题有一定答案吗?我有可能以一种或另一种方式使用此注释吗?

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

}

推荐答案

您的问题是,将@Formula放在字段上,而其他注释放在吸气剂上,因此@Formula被忽略.

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

您应该选择一种策略并一致地应用它(除非您用@AccessType明确覆盖它).

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;

}

这篇关于Hibernate4.1仍然支持@Formula注释吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆