Come assegnare una query JPQL ad un semplice oggetto Java?
Domanda
Ho bisogno di assegnare il risultato di un risultato jpql ad un oggetto di classe Java semplice
Ho qualcosa di simile
class myObject() {
@id
private Long id;
private String Name;
private String description;
...
//getters and setters
}
Ho bisogno di qualche modo per memorizzare il risultato di questa query SQL, ad esempio
// potrebbe essere anytable SELEZIONARE c.table_id DISTINCT, c.name, null come descrizione DA anyTable
Come faccio a fare questo in JPQL e poi assegnare il risultato a mio oggetto?
Soluzione
La questione è estremamente chiara. Così qui è una risposta vaga:
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);
Altri suggerimenti
Credo, è la risposta!
SELECT NEW entities.PersonWell(c.peopleId, c.name, c.age, c.height, c.weight)
FROM
People AS c;
Ed è un costruttore di classe entities.PersonWell:
public PersonWell(Integer peopleId, String name, short age, short height, short weight, short speechspeed) {
this.peopleId = peopleId;
this.name = name;
this.age = age;
this.height = height;
this.weight = weight;
this.speechspeed = speechspeed;
}
C'è un po 'di codice per un estrazione di seguito:
List<PersonWell> resultList = query.getResultList();
Mi auguro, Vi aiuterà! :)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow