用Hibernate加入@Formula

时间:2017-02-05 14:44:39

标签: java hibernate join aggregate formula

当我加载实体时,我将使用@Formula注释来使用聚合函数。

如果我使用以下语句,我将获得例外。


    (SELECT sum(inovalue.vba) FROM INO i_no INNER JOIN i_no.listValues inovalue WHERE i_no.id = '91c78419-e528-453f-a60d-9f2050e12c79')


    Caused by: java.sql.SQLSyntaxErrorException: Syntax error: Encountered "WHERE" at line 1, column 401.
        at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source)
        at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
        at org.apache.derby.client.am.Connection.prepareStatement(Unknown Source)
        at com.mchange.v2.c3p0.impl.NewProxyConnection.prepareStatement(NewProxyConnection.java:213)
        at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534)
        at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:452)
        at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
        at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1700)
        at org.hibernate.loader.Loader.doQuery(Loader.java:801)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
        at org.hibernate.loader.Loader.loadCollection(Loader.java:2166)
        ... 17 more
    Caused by: org.apache.derby.client.am.SqlException: Syntax error: Encountered "WHERE" at line 1, column 401.
        at org.apache.derby.client.am.Statement.completeSqlca(Unknown Source)
        at org.apache.derby.client.net.NetStatementReply.parsePrepareError(Unknown Source)
        at org.apache.derby.client.net.NetStatementReply.parsePRPSQLSTTreply(Unknown Source)
        at org.apache.derby.client.net.NetStatementReply.readPrepareDescribeOutput(Unknown Source)
        at org.apache.derby.client.net.StatementReply.readPrepareDescribeOutput(Unknown Source)
        at org.apache.derby.client.net.NetStatement.readPrepareDescribeOutput_(Unknown Source)
        at org.apache.derby.client.am.Statement.readPrepareDescribeOutput(Unknown Source)
        at org.apache.derby.client.am.PreparedStatement.readPrepareDescribeInputOutput(Unknown Source)
        at org.apache.derby.client.am.PreparedStatement.flowPrepareDescribeInputOutput(Unknown Source)
        at org.apache.derby.client.am.PreparedStatement.prepare(Unknown Source)
        at org.apache.derby.client.am.Connection.prepareStatementX(Unknown Source)
        ... 26 more

如果我使用@Formular注释之外的语句,我将得到正确的结果。我不得不说我在另一个对象中使用这个注释,而不是在INO对象中。

这些是简化形式的实体。

实体Cs

@Entity
@Table(name = "cs")
public class Cs  {

    @Id
    @GeneratedValue(generator="uuid")
    @GenericGenerator(name="uuid", strategy="org.hibernate.id.UUIDGenerator")
    @Column(length=40)
    private String id = "";

    @Column(name = "inoId_ref_id", length = 40)
    private String inoId = null;

    @Formula("(SELECT sum(inovalue.vba) FROM Ino i_no INNER JOIN i_no.listValues inovalue WHERE i_no.id = inoId)")
    private Double lbst = null;

    public Cs() {
    }
}

实体Ino

@Entity 
@FilterDef(name="inoDateFilter", parameters=@ParamDef( name="inoDateFilterParam", type="integer" ) )
@Table(name="ino")
public class Ino {

    @Id
    @GeneratedValue(generator="uuid")
    @GenericGenerator(name="uuid", strategy="org.hibernate.id.UUIDGenerator")
    @Column(length=40)
    private String id = "";

    @OneToMany (orphanRemoval=true, cascade=CascadeType.ALL)
    @LazyCollection(value=LazyCollectionOption.FALSE)
    @JoinColumn(name = "ioifeflref_id")
    @Filter(name = "inoDateFilter", condition="year(datumVon) = :inoDateFilterParam")
    @OrderBy("datumVon ASC")
    private List<InoValue> listIntervallFeFl = new ArrayList<InoValue>();

    public INO() {
    }
}

实体InoValue

@Entity 
@Table(name="inovalue")
public class InoValue  {

    @Id
    @GeneratedValue(generator="uuid")
    @GenericGenerator(name="uuid", strategy="org.hibernate.id.UUIDGenerator")
    @Column(length=40)
    private String id = "";

    @Column(name = "vba")
    private Double vba = null;

     */
    public InoValue() {
    }
}

出了什么问题?为了测试我使用derby数据库。

有人有任何想法吗?

此致 MS-技术

1 个答案:

答案 0 :(得分:1)

确保@Formula注释的内容是有效的SQL,而不是HQL / JPQL。