views:

27

answers:

1

I need to assign the result of a jpql result to a simple class java object

I have something like this

class myObject() {
@id
private Long id;
private String Name;
private String description;
...
//getters and setters
}

I need to somehow to store the result of this SQL query, example

// could be anytable SELECT DISTINCT c.table_id, c.name, NULL AS description FROM anyTable

How do I do this in JPQL and then assign the result to my object?

+1  A: 

The question is extremely unclear. So here is a vague answer:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("MyPu");
EntityManager em = emf.createEntityManager();    
Query q = em.createQuery("select f.id, f.name from Foo f");
List<Object[]> foos = (List<Object([]>)q.getResultList();

MyObject o = new MyObject();
o.setFoos(foos);
Pascal Thivent
sorry about that, you got it, that was i need formy problem is that i'm just starting with JPA and JPQL and JavaEE in general, so it's very confusing sometimesThanks for your answer
ErVeY
@ErVeY No problem, I was just saying that as a general advice as you will probably get better/faster answers if the problem is clear (and I was a bit guessing here). Anyway, you're welcome.
Pascal Thivent