Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Caleb Smith   on Jun 08 In Java Category.

  
Question Answered By: Hilma Miller   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:

@Entity
@Table(name="PERSON")
class Person {
private String name;
private String family;

@OneToOne
private Address address;

}

@Entity
@Table(name="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....
}

Share: 

 

This Question has 1 more answer(s). View Complete Question Thread

 
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.


Tagged: