-->
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.  [ 1 post ] 
Author Message
 Post subject: stored procedures, parameter type annotations
PostPosted: Wed May 18, 2011 10:24 am 
Newbie

Joined: Wed May 18, 2011 9:51 am
Posts: 1
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);
               }
            });
   }
}



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

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.