-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: JPA formula Annotation
PostPosted: Wed Jul 08, 2009 7:39 pm 
Newbie

Joined: Wed Jul 08, 2009 7:32 pm
Posts: 3
I have figured out that I can use hibernate to get the sum of a number of entities using HQL as follows...

Code:
public Long getEnvelopeTotal(AbstractEnvelope envelope) {
    String query = "select sum(t.amount) from User_Transaction t";
    Long result = (Long) hibernateUtil.getSession().createQuery(query).uniqueResult();
    return result;
}


What I am wondering if it is possible to use hibernate in such a way that the property "total" is set via custom HQL query instead of being mapped to a simply database column.

ex:

Code:
@SomeMagicAnnotation(query="select sum(t.amount) from User_Transaction t")
private Long total;


Somebody on StackOverflow.com suggested using the @Forumula annotation but I cannot find enough documentation on it. Currently I have tried the following and found it does not work

Code:
@Entity(name = "Abstract_Envelope")
public abstract class AbstractEnvelope extends AbstractAccount {

    @OneToMany(mappedBy = "abstractEnvelope")
    private List<UserTransaction> userTransactions;
    private Long total;

    public List<UserTransaction> getUserTransactions() {
        return userTransactions;
    }

    public void setUserTransactions(List<UserTransaction> userTransactions) {
        this.userTransactions = userTransactions;
    }

    @Formula(value = "select sum(t.amount) from User_Transaction t")
    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }
}


Code:
@Entity(name = "User_Transaction")
@Inheritance(strategy = InheritanceType.JOINED)
public class UserTransaction extends AbstractPersistentObject {

    private String name;
    private Long amount;
    @Temporal(TemporalType.DATE)
    private Date transactionDate;
    private String checkNumber;
    private String note;
    @ManyToOne
    private SpendingAccount spendingAccount;
    @ManyToOne
    private AbstractEnvelope abstractEnvelope;
    @OneToMany(mappedBy = "parentTransaction")
    private List<SplitTransaction> splitTransactions;

    public AbstractEnvelope getAbstractEnvelope() {
        return abstractEnvelope;
    }

    public void setAbstractEnvelope(AbstractEnvelope abstractEnvelope) {
        this.abstractEnvelope = abstractEnvelope;
    }

    public Long getAmount() {
        return amount;
    }

    public void setAmount(Long amount) {
        this.amount = amount;
    }

    public String getCheckNumber() {
        return checkNumber;
    }

    public void setCheckNumber(String checkNumber) {
        this.checkNumber = checkNumber;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public Date getTransactionDate() {
        return transactionDate;
    }

    public void setTransactionDate(Date transactionDate) {
        this.transactionDate = transactionDate;
    }

    public SpendingAccount getSpendingAccount() {
        return spendingAccount;
    }

    public void setSpendingAccount(SpendingAccount spendingAccount) {
        this.spendingAccount = spendingAccount;
    }
}


Top
 Profile  
 
 Post subject: Re: JPA formula Annotation
PostPosted: Fri Jul 10, 2009 12:51 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
As commented on stackoverflow:

What is the exact problem you are seeing? Exceptions, wrong results ...

I'd expect a some sqlException. If so check the generated sql statement. Maybe you have to add () around your formula.

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


Top
 Profile  
 
 Post subject: Re: JPA formula Annotation
PostPosted: Fri Jul 10, 2009 2:53 pm 
Newbie

Joined: Wed Jul 08, 2009 7:32 pm
Posts: 3
Here are the log entries I see when my unit tests fail. They fail this way when I use Derby and they pass when using HSQLDB.

Code:
Hibernate: select this_.id as id0_6_, this_.version as version0_6_, this_.applicationInstance_id as applicat2_9_6_, this_.name as name9_6_, select sum(t.amount) from User_Transaction t where t.abstractenvelope_id = this_.id as formula0_6_, this_.clazz_ as clazz_6_, applicatio2_.id as id0_0_, applicatio2_.version as version0_0_, applicatio2_.accountTransfersEnvelope_id as accountT1_2_0_, applicatio2_.creditCardPaymentsEnvelope_id as creditCa2_2_0_, applicatio2_.endUser_id as endUser3_2_0_, applicatio2_.incomeCashPoolEnvelope_id as incomeCa4_2_0_, applicatio2_.newTransactionsEnvelope_id as newTrans5_2_0_, accounttra3_.id as id0_1_, accounttra3_.version as version0_1_, select sum(t.amount) from User_Transaction t where t.abstractenvelope_id = accounttra3_.id as formula0_1_, creditcard4_.id as id0_2_, creditcard4_.version as version0_2_, select sum(t.amount) from User_Transaction t where t.abstractenvelope_id = creditcard4_.id as formula0_2_, enduser5_.id as id0_3_, enduser5_.version as version0_3_, enduser5_.email as email1_3_, enduser5_.password as password1_3_, incomecash6_.id as id0_4_, incomecash6_.version as version0_4_, select sum(t.amount) from User_Transaction t where t.abstractenvelope_id = incomecash6_.id as formula0_4_, newtransac7_.id as id0_5_, newtransac7_.version as version0_5_, select sum(t.amount) from User_Transaction t where t.abstractenvelope_id = newtransac7_.id as formula0_5_ from ( select id, nullif('x','x') as applicationInstance_id, nullif('x','x') as name, version, 5 as clazz_ from New_Transactions_Envelope union all select id, nullif('x','x') as applicationInstance_id, nullif('x','x') as name, version, 6 as clazz_ from Account_Transfers_Envelope union all select id, nullif('x','x') as applicationInstance_id, nullif('x','x') as name, version, 7 as clazz_ from Income_Cash_Pool union all select id, nullif('x','x') as applicationInstance_id, nullif('x','x') as name, version, 8 as clazz_ from Credit_Card_Payments_Envelope union all select id, applicationInstance_id, name, version, 9 as clazz_ from User_Envelope ) this_ left outer join Application_Instance applicatio2_ on this_.applicationInstance_id=applicatio2_.id left outer join Account_Transfers_Envelope accounttra3_ on applicatio2_.accountTransfersEnvelope_id=accounttra3_.id left outer join Credit_Card_Payments_Envelope creditcard4_ on applicatio2_.creditCardPaymentsEnvelope_id=creditcard4_.id left outer join End_User enduser5_ on applicatio2_.endUser_id=enduser5_.id left outer join Income_Cash_Pool incomecash6_ on applicatio2_.incomeCashPoolEnvelope_id=incomecash6_.id left outer join New_Transactions_Envelope newtransac7_ on applicatio2_.newTransactionsEnvelope_id=newtransac7_.id


[2009-07-10 12:52:41,612] WARN 1994[main] - org.hibernate.util.JDBCExceptionReporter.logExceptions(JDBCExceptionReporter.java:77) - SQL Error: -1, SQLState: 42X01
[2009-07-10 12:52:41,612]ERROR 1994[main] - org.hibernate.util.JDBCExceptionReporter.logExceptions(JDBCExceptionReporter.java:78) - Syntax error: Encountered "select" at line 1, column 130.


Top
 Profile  
 
 Post subject: Re: JPA formula Annotation
PostPosted: Fri Jul 10, 2009 4:35 pm 
Newbie

Joined: Wed Jul 08, 2009 7:32 pm
Posts: 3
When I try this with MySQL instead here is the output...

Code:
getEnvelopeTotal
Hibernate:
    select
        this_.id as id2_6_,
        this_.version as version2_6_,
        this_.applicationInstance_id as applicat5_2_6_,
        this_.name as name2_6_,
        select
            sum(t.amount)
        from
            User_Transaction t
        where
            t.abstractenvelope_id = this_.id as formula0_6_, this_.DTYPE as DTYPE2_6_, applicatio2_.id as id1_0_, applicatio2_.version as version1_0_, applicatio2_.accountTransfersEnvelope_id as accountT3_1_0_, applicatio2_.creditCardPaymentsEnvelope_id as creditCa4_1_0_, applicatio2_.endUser_id as endUser5_1_0_, applicatio2_.incomeCashPoolEnvelope_id as incomeCa6_1_0_, applicatio2_.newTransactionsEnvelope_id as newTrans7_1_0_, accounttra3_.id as id2_1_, accounttra3_.version as version2_1_, select
                sum(t.amount)
            from
                User_Transaction t
            where
                t.abstractenvelope_id = accounttra3_.id as formula0_1_, creditcard4_.id as id2_2_, creditcard4_.version as version2_2_, select
                    sum(t.amount)
                from
                    User_Transaction t
                where
                    t.abstractenvelope_id = creditcard4_.id as formula0_2_, enduser5_.id as id0_3_, enduser5_.version as version0_3_, enduser5_.email as email0_3_, enduser5_.password as password0_3_, incomecash6_.id as id2_4_, incomecash6_.version as version2_4_, select
                        sum(t.amount)
                    from
                        User_Transaction t
                    where
                        t.abstractenvelope_id = incomecash6_.id as formula0_4_, newtransac7_.id as id2_5_, newtransac7_.version as version2_5_, select
                            sum(t.amount)
                        from
                            User_Transaction t
                        where
                            t.abstractenvelope_id = newtransac7_.id as formula0_5_
                        from
                            Abstract_Envelope this_
                        left outer join
                            Application_Instance applicatio2_
                                on this_.applicationInstance_id=applicatio2_.id
                        left outer join
                            Abstract_Envelope accounttra3_
                                on applicatio2_.accountTransfersEnvelope_id=accounttra3_.id
                        left outer join
                            Abstract_Envelope creditcard4_
                                on applicatio2_.creditCardPaymentsEnvelope_id=creditcard4_.id
                        left outer join
                            End_User enduser5_
                                on applicatio2_.endUser_id=enduser5_.id
                        left outer join
                            Abstract_Envelope incomecash6_
                                on applicatio2_.incomeCashPoolEnvelope_id=incomecash6_.id
                        left outer join
                            Abstract_Envelope newtransac7_
                                on applicatio2_.newTransactionsEnvelope_id=newtransac7_.id
[2009-07-10 14:33:10,317] WARN     0[main] - org.hibernate.util.JDBCExceptionReporter.logExceptions(JDBCExceptionReporter.java:77) - SQL Error: 1064, SQLState: 42000
[2009-07-10 14:33:10,320]ERROR     3[main] - org.hibernate.util.JDBCExceptionReporter.logExceptions(JDBCExceptionReporter.java:78) - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select sum(t.amount) from User_Transaction t where t.abstractenvelope_id = this_' at line 1


Top
 Profile  
 
 Post subject: Re: JPA formula Annotation
PostPosted: Sat Jul 11, 2009 12:05 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
Try this:
Code:
@Formula(value = "(select sum(t.amount) from User_Transaction t)")

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


Top
 Profile  
 
 Post subject: Re: JPA formula Annotation
PostPosted: Wed Sep 08, 2010 7:01 am 
Newbie

Joined: Fri Jul 02, 2010 5:07 am
Posts: 3
Hi,

i'm new with Hibernate and i would like to know if it is possible to do this:

@Entity
@Table(name="TBL_STUDENT")
public class Student implements Serializable {

@Column(name="USER_NAME")
private String name;

@Formula(value="(select l.fullname from Land l where l.key=landKey)") /*HQL, Siehe Student-Entity-Member 'landKey'*/
private String land;

@Column(name="LAND_REF)
private String landKey;
}



@Entity
@Table(name="TBL_LAND")
public class Land implements Serializable {
@Column(name="LAND_KEY")
private String key;
@Column(name="LAND_FULLNAME")
private String fullname;
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.