Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to make Hibernate use literal values rather than bind variables?

In Oracle I have a partitioned table. The partitions are of different sizes and have different data distribution.

I would like to have hibernate issue SQL statements that includes a literal value for the partition key column rather than a bind variable. It should use bind variables for any other values of course.

Using a literal for the partition key will allow Oracle to come up with a plan specific to the known partition and the collected statistics. This might also be useful for columns that have a histogram in place for skewed data.

It would be preferable to specify this in the entity otherwise we will need to do it in each query. Is there a way to do this in hibernate?

We are on hibernate 3.6.1 using the Oracle 10g Dialect.

If there is not a way to do it natively in Hibernate, can I create a user type or dialect or something to make this happen?

like image 206
WW. Avatar asked Nov 23 '11 23:11

WW.


2 Answers

You can use the namedNativeQuery here is a sample implementation.

Entity class is

@Entity
@Table(catalog = DBCatalog)
@org.hibernate.annotations.NamedNativeQuery(name="partitionTR1",query ="SELECT * FROM DATAMARTTRANSACTIONHISTORY PARTITION (tr1) where id=?",resultClass=DataMartTable.class)
public class DataMartTransactionHistory implements TransactionHistory {
    @Id
    @Column
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Enumerated(EnumType.ORDINAL)
    private TransactionStatus transactionStatus;
... other props...
}

and here is a dao implementation.

public DataMartTransactionHistory findDataMartTransactionHistoryTR1(Long id) {
    Query namedQuery = getSessionFactory().getCurrentSession().getNamedQuery("partitionTR1");
    namedQuery.setLong(0, id);
    return (DataMartTransactionHistory)namedQuery.list().get(0);
}
like image 35
dursun Avatar answered Oct 12 '22 10:10

dursun


No, literal values are not supported in Hibernate. I doubt if you can make a workaround but I guess you're looking for another solution.

like image 121
Wouter Dorgelo Avatar answered Oct 12 '22 08:10

Wouter Dorgelo