?
Solved

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

Posted on 2008-06-11
8
Medium Priority
?
1,432 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Implementing Azure Infrastructure Exam 70-533

This course is designed to familiarize and instruct students in the content that is covered by Microsoft Exam 70-533, Implementing Microsoft Azure Solutions. It focuses on all the November 2016 objective domain topics.

 

Author Comment

by:hai_madhan
ID: 21774724
Yes.
0
 
LVL 8

Accepted Solution

by:
malfunction84 earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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:
Suggested Courses

719 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