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?
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);
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With