Hi all,
following the article [url=Use Hibernate’s Custom Loaders to fake an aggregation view]http://www.cereslogic.com/pages/2008/06/23/use-hibernates-custom-loaders-to-fake/[/url] i'm trying to make work my "fake view".
Here's my code: (the property names are in Italian, but their meaning is not 'important)
Code:
@javax.persistence.Entity
@Entity(dynamicInsert=false, dynamicUpdate=false, mutable=false)
@Loader(namedQuery="loadArticoloView")
@SqlResultSetMapping(name="loadArticoloView",
                     entities=@EntityResult(entityClass=ArticoloView.class))
@NamedNativeQuery(name="loadArticoloView", 
                  query="select " +
                        "a.id as id, nome, descrizione, posizione, prezzo, coefficientePrezzo, prezzoFinito, " +
                        "c.testo as categoria, " +
                        "m.quantita as quantita, " +
                        "(select nome" +
                        "from " +
                        "articolo_fattura " +
                        "left join fornitore on articolo_fattura.id_fornitore = fornitore.id " +
                        "where id_articolo = a.id " +
                        "order by data_fattura DESC " +
                        "limit 0,1) as fornitore " +
                        "from " +
                        "articolo a " +
                        "left join codcategoria c on c.id = a.id_categoria " +
                        "left join magazzino m on m.id_articolo = a.id " +
                        "where a.id = ?", 
                        resultSetMapping="loadArticoloView")
public class ArticoloView {
   String id;
   String nome;
   String descrizione;
   String posizione;
   Float prezzo;
   Float coefficientePrezzo;
   Float prezzoFinito;
   String categoria;
   Integer quantita;
   String fornitore;
   
   
   @Id
   public String getId() {
      return id;
   }
   public void setId(String id) {
      this.id = id;
   }   
   
        // getter and setter...
   
}
and i'm trying to do this:
Code:
Query q = em.createQuery("Select count(*) from ArticoloView", Long.class);
System.out.println(q.getResultList().get(0).toString());
hibernate instead execute this query : 
Code:
select count(*) as col_0_0_ from ArticoloView articolovi0_
and seem to ignore mine.
in the console outpout i've found : (I do not know if it means something)
Code:
1621 [http-8080-2] INFO org.hibernate.cfg.AnnotationBinder - Binding entity from annotated class: com.example.testhibernate3.hibernate.view.ArticoloView
1624 [http-8080-2] INFO org.hibernate.cfg.annotations.QueryBinder - Binding named native query: loadArticoloView => select a.id as id, nome, descrizione, posizione, prezzo, coefficientePrezzo, prezzoFinito, c.testo as categoria, m.quantita as quantita, (select nomefrom articolo_fattura left join fornitore on articolo_fattura.id_fornitore = fornitore.id where id_articolo = a.id order by data_fattura DESC limit 0,1) as fornitore from articolo a left join codcategoria c on c.id = a.id_categoria left join magazzino m on m.id_articolo = a.id where a.id = ?
1625 [http-8080-2] INFO org.hibernate.cfg.annotations.EntityBinder - Bind entity com.example.testhibernate3.hibernate.view.ArticoloView on table ArticoloView
Someone know where's my error ?