You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Edoardo Panfili <ed...@aspix.it> on 2010/05/07 22:18:54 UTC
fetch=FetchType.EAGER and createNativeQuery
first: sorry for my english!
I have a persistent class "Person" with this field (and others)
==========
@ManyToOne(cascade=CascadeType.REFRESH, fetch=FetchType.EAGER)
Address address;
==========
In another class I retrieve a list of Person using createNativeQuery and
then i close the EntityManager, surplisingly (for me!) the "address"
field is not retrieved.
After some documentation reading I can't figure the reason (but maybe i
miss this informations).
I did a try using "createQuery" instead of "createNativeQuery" and all
works fine.
Is this the desidered behaviour?
thank you
Edoardo
Re: fetch=FetchType.EAGER and createNativeQuery
Posted by Edoardo Panfili <ed...@aspix.it>.
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 */
}
============================
Re: fetch=FetchType.EAGER and createNativeQuery
Posted by gilbertoca <gi...@gmail.com>.
Java Persistence 2.0, Final Release:
[QUOTE]
3.8.15 SQL Queries
Queries may be expressed in native SQL. The result of a native
SQL query may consist of entities, scalar values, or a combination of the
two. The entities returned by a query may be of different entity types.
The SQL query facility is intended to provide support for those
cases where it is necessary to
use the native SQL of the target database in use (and/or where
the Java Persistence query language cannot be used). Native SQL queries are
not expected to be portable across databases.
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)
})
[/QUOTE]
So, you need to take care of the entity's relationships.
Gilberto
--
View this message in context: http://openjpa.208410.n2.nabble.com/fetch-FetchType-EAGER-and-createNativeQuery-tp5021253p5030337.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.