Solved

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

Posted on 2008-06-11
8
1,426 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

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

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

734 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