As a business requirement, I have to do all data modifications through stored procedures. I'd like to get Hibernate to use my procedures seamlessly, but the only thing keeping me from doing this is my inability to specify user types for parameters on my native queries. I can specify type defs using annotations, and then annotate my fields with @Type. Then, hibernate will use my user type properly. However, when I call my named native query in the DAO, I need to explicitly set all of the parameter user types.
I have two problems.
1. Can I specify the parameter types in annotations related to my @NamedNativeQuery? If so, then I can use @SQLInsert and @SQLUpdate hints to get the persister to use my procedures for saves.
2. If I can't do #1... how can I reference my type defs by name? If you noticed in the DAO I'm forced to recreate them programatically because I can't seem to figure it out.
Btw, I'm using the JASYPT library's user types for encrypting columns...
Code:
@TypeDefs({
@TypeDef(name = "encrypted_string", typeClass = EncryptedStringType.class, parameters = { @Parameter(name = "encryptorRegisteredName", value = "hibernateStringEncryptor"), }),
@TypeDef(name = "encrypted_date_as_string", typeClass = EncryptedDateAsStringType.class, parameters = { @Parameter(name = "encryptorRegisteredName", value = "hibernateStringEncryptor"), }) })
@Entity
//@org.hibernate.annotations.NamedNativeQuery(name = "CRYPTO_CREATE", query = "call PKG_CRYPTO_TEST.PRC_CREATE(?, :P_THEDATA,:P_THEDATE)", resultClass = CryptoDomainObject.class, callable=true)
@NamedNativeQuery(name = "CRYPTO_CREATE", query = "{ call PKG_CRYPTO_TEST.PRC_CREATE(?, :theData,:theDate) }", resultClass = CryptoDomainObject.class, hints = { @javax.persistence.QueryHint(name = "org.hibernate.callable", value = "true") })
@Table(name="V_CRYPTO_TEST")
public class CryptoDomainObject implements Serializable {
private Integer id;
private String theData;
private Date theDate;
public void setTheData(String theData) {
this.theData = theData;
}
@Column(name = "THEDATA", nullable = false)
@Type(type = "encrypted_string")
public String getTheData() {
return theData;
}
public void setTheDate(Date date) {
this.theDate = date;
}
@Column(name = "THEDATE", nullable = false)
@Type(type = "encrypted_date_as_string")
public Date getTheDate() {
return theDate;
}
public void setId(Integer id) {
this.id = id;
}
@Id
@Column(name = "ID", nullable = false)
public Integer getId() {
return id;
}
}
@Repository("cryptoDAO")
public class CryptoDAO extends AbstractHibernateDaoSupport {
@Resource(name = "dsrSessionFactory")
public void init(SessionFactory sessionFactory) {
setSessionFactory(sessionFactory);
}
public CryptoDomainObject get(Integer id) {
return (CryptoDomainObject) getHibernateTemplate().get(
CryptoDomainObject.class, id);
}
@SuppressWarnings("unchecked")
@org.hibernate.annotations.Type(type = "encrypted_string")
public CryptoDomainObject create(final CryptoDomainObject domain) {
return (CryptoDomainObject) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(final Session session)
throws HibernateException, SQLException {
Properties properties = new Properties();
properties.setProperty("encryptorRegisteredName",
"hibernateStringEncryptor");
Type custom = session.getTypeHelper().custom(
EncryptedStringType.class, properties);
Type customDate = session.getTypeHelper().custom(
EncryptedDateAsStringType.class, properties);
Query query = session
.getNamedQuery("CRYPTO_CREATE")
.setParameter("theData", domain.getTheData(),
custom)
.setParameter("theDate", domain.getTheDate(),
customDate);
return query.list().get(0);
}
});
}
}