Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Need some ideas for migrating from JDBC to JPA

  Asked By: Caleb    Date: Jun 08    Category: Java    Views: 1215

Let change the topic, I understood that I'm a TEXT COMPILER MACHINE that MUST BE ABLE to do something without sending any NEGH NEGH and STUPID texts to forums to be HIRED for whole my life by a representative person of an unknown company who is LOOKING FOR ME :)) and This is the real meaning of respect.
and the word HIRE is the most common and formal word used in the TITLE of not Indian but U.S., Canadian, Australian companies job offers, and not other terms. :D

Suppose there is a traditional "john smith's standard" framework for persistence layer used directly JDBC and the non-standard POJOs are somehow bound to Database Views (combination of some tables) for "select" operation, and bound to only one of those tables for insert, update and delete operations. and there is no implementation of "instead-of" triggers for these views.
To have the lowest cost of re-factoring and migrating to JPA, the best solution I find up to now is to bind POJOs as JPA entities to the Database Views, and implement the instead-of insert, update and delete triggers for Views.

Is there any other way to get rid of implementation of instead-of triggers, or a better solution. Generally the JPA entity is bound to a view, so some fields are read-only and some are insert-able and update-able. Maybe using some JPA annotations to bind not readonly fields seperately directly to the table.



2 Answers Found

Answer #1    Answered By: Hilma Miller     Answered On: Jun 08

I don't think it is a simple-to-do job  but I suggest you not to binding your views to the entities. This may works for a while but a disaster in the future.
I recommend you to create entities each for a corresponding table  in DB but for your views... for each view  you may create a View class and to populate those classes you can use EJBQL functionality. Look at following sample:

class Person {
private String name;
private String family;

private Address address;


class Address {
private String street;
private String number;
private String phone;


Now you need a view which is similar to a real  view in DB:

public class PersonsAddrView {
private String name;
private String family;
private String street;
private String phone;


Note that preceding class has no JPA annotations and it's not an entity. Now to populate this class use following query:

String query = "select new PersonsAddressView(e.name,e.family, e.address.street, e.address.phone) " +
" from Person as e" +
" where e.name like 'J%' ";
Query q = entityManager.createQuery(query);

List<PersonsAddressView> resultView = (List<PersonsAddress>) q.getResultList();

but please note that you need to define a constructor for your view class as follows:

public PersonsAddressView(String name, String family, String st, String phone){
this.name = name;
//rest of the code....

Answer #2    Answered By: Earl Stone     Answered On: Jun 08

Yes it is not so simple, and using Views as you said can be a disaster generally. That's why I'm thinking about using inheritance capability of JPA or something like that.

Thanks for you suggestion, but I need more votes to avoid using Views :P
No Ideas

Didn't find what you were looking for? Find more on Need some ideas for migrating from JDBC to JPA Or get search suggestion and latest updates.