Solved

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

Posted on 2008-06-11
8
1,416 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:hai_madhan
Comment Utility
Yes.
0
 
LVL 8

Accepted Solution

by:
malfunction84 earned 500 total points
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

771 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

10 Experts available now in Live!

Get 1:1 Help Now