On 10/05/10 14.43, gilbertoca wrote:
When multiple entities are returned by a SQL query, the entities
must be specified and mapped to the column results of the SQL statement in a
[B]SqlResultSetMapping metadata[/B] definition.
.
The following query and SqlResultSetMapping metadata illustrates the return
of multiple entity
types and assumes default metadata and column name defaults.
Query q = em.createNativeQuery(
"SELECT o.id, o.quantity, o.item, i.id, i.name, i.description "+
"FROM Order o, Item i " +
"WHERE (o.quantity> 25) AND (o.item = i.id)",
"OrderItemResults");
@SqlResultSetMapping(name="OrderItemResults",
entities={
@EntityResult(entityClass=com.acme.Order.class),
@EntityResult(entityClass=com.acme.Item.class)
})
this example retrieves two entities directly in the same query... I
think that this is not my situation:
I have a query:
SELECT d FROM Regista d WHERE d.nome='Alan Parker'
and a nativeQuey
SELECT * FROM regista WHERE nome='Alan Parker'
in both I
0- create the EntityManager
1- create the query
2- retrieve the first element of the result
3- close the EntityManager
4- print the test field "Regista.film" (a list of objects,
fetch=FetchType.EAGER)
using query spet 4 prints out the list, using nativeQuery prints out "null"
Edoardo
this is a part of my code:
==========================================
public class TestFetchType {
public static void main(String args[]) throws Exception{
EntityManagerFactory factory;
EntityManager em;
Regista r1,r2;
Query query;
// QUERY
factory = Persistence.createEntityManagerFactory(
"openjpa-demo", System.getProperties());
em = factory.createEntityManager();
query = em.createQuery(
"SELECT d FROM Regista d WHERE d.nome='Alan Parker'",Regista.class);
List<Regista> list1 = (List<Regista>)(query.getResultList());
r1 = list1.get(0);
em.close();
System.out.println(r1.getFilm());
// NATIVE QUERY
em = factory.createEntityManager();
query = em.createNativeQuery(
"SELECT * FROM regista WHERE nome='Alan Parker'",Regista.class);
List<Regista> list2 = (List<Regista>)(query.getResultList());
r2 = list2.get(0);
em.close();
System.out.println(r2.getFilm());
}
}
==== Regista.java ========
@Entity
@SequenceGenerator( name="SEQ_REGISTA",
sequenceName="regista_idregista_seq", allocationSize=20 )
public class Regista {
@Id @GeneratedValue(strategy=GenerationType.SEQUENCE,
generator="SEQ_REGISTA")
int idRegista;
String nome;
int annoNascita;
@OneToMany(cascade={CascadeType.ALL}, mappedBy="regista",
targetEntity=it.aspix.demoJPA.obj.Film.class, fetch=FetchType.EAGER)
@OrderBy("rilascio")
List<Film> film;
/* getter and setters */
}
============================