HI,
I have a collection and i wanna insert all the elements of my table.
How i can do this oper开发者_StackOverflow社区ation in EJB QL?
For example: (this isn't my code) I have my Collection:
Collection<Person> coll = new Collectio<Person>
And i have my table Person
@entity
private class Person{
private id;
private name;
//getters setter
}
I have popolate the table and i wanna all the element in my collection.
Thanks.
Introduce Query in your JPA entity:
@Entity
@NamedQuery(name = "Person.findAll", query = "SELECT p from Person p")
public class Person {
private id;
private name;
//getters setter
}
Then inject EntityManager in your code and use query:
public class MyClass {
@PersistenceUnit(name = "MyEntitiesFromPersistenceXML")
private EntityManagerFactory emf;
public void myMethod() {
EntityManager entityManager = emf.createEntityManager();
Query query = entityManager.createNamedQuery("Person.findAll");
@SuppressWarnings("unchecked")
List<Person> persons = query.getResultList();
// query returns List which, in turn, extends Collection
}
}
Btw, why your class is private? It must be public.
精彩评论