Solved

How to use functions in the select clause of a NamedQuery in JPA

Posted on 2008-06-11
8
1,417 Views
Last Modified: 2013-11-24
Hi

We are in the process of migrating an application to JPA(Hibernate implementation).
Previously the application was using direct sqls.

One of the table has it's data in encrypted format using Oracle DES encryption.
The encryption logic has been implemented in a user defined oracle encrypt function and it is invoked in the trigger.
The decryption is done while fetching the data by invoking the user defined oracle decrypt function in the
select clause, some thing like this:

      select id, decrypt(name) as name, decrypt(location) as location from Company

Now as we are migrating the application to JPA, I'm having difficulty in fetching the data from this table.
The encryption works fine as usual with by the triggers. But I do not know how to call the decrypt function when we
fetch the Company entities.
 
Is it possible to use the oracle functions in the NamedQuery's select clause. If so, how? Or is there any other way
to do this?

Your help is much appreciated.

Thanks,
Madhan
0
Comment
Question by:hai_madhan
  • 3
  • 3
8 Comments
 
LVL 8

Expert Comment

by:malfunction84
ID: 21766059
Have you tried the .createNativeQuery() method?  You have to be careful to select all of the columns.
Query q = em.createNativeQuery("select id, decrypt(name) as name, decrypt(location) as location from company", Company.class);

List<Company> list = q.getResultList();

Open in new window

0
 

Author Comment

by:hai_madhan
ID: 21771856
Thanks malfunction84,

I did try the NativeQuery option. Looks like NativeQueries are the answer to my question.
But I faced a different issue though.  I'm not able to fetch the related entities.

For example one of the Company's attribute is country which. I don't know how to query that along with the company.

Could you please give me a sample to fetch an entity and the related entities using the NativeQuery.

Many Thanks.
Madhan
0
 
LVL 8

Expert Comment

by:malfunction84
ID: 21772151
Darn, I was afraid of that.  When you select the columns of the Company table, do you return the Company's "country_id" foreign key as well?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:hai_madhan
ID: 21774724
Yes.
0
 
LVL 8

Accepted Solution

by:
malfunction84 earned 500 total points
ID: 21776200
Hmm, it would appear that the JPA by itself doesn't support this.

I was about to suggest calling em.refresh() on each instance in that List, but I don't think that would work.  The reason: the Entity mapping itself would still be fetching encrypted values from the database, since it doesn't know to call decrypt() on the raw data from those columns.

Then I was going to suggest replacing the @Column annotations for the encrypted columns with @org.hibernate.annotations.Formula.  The problem with that is that those columns would then be read-only.  Even if it weren't, there's no way to specify another formula to use when inserting (to encrypt).

Toplink, itself an Oracle product, offers a DescriptorCustomizer which you can use to customize how queries are constructed.  An example is in the following thread:
http://forum.java.sun.com/thread.jspa?messageID=10205608&tstart=0

Sorry, that's all I've got.  I don't think it's possible.
0
 

Author Comment

by:hai_madhan
ID: 21794140
Unfortunately I couldn't find any solution for this. So we decided to implement the encryption in java level and remove the trigger based encryption.

Anyway Thanks for your replies malfunction84.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now