vrvictor
vrvictor

Reputation: 95

query in hibernate with annotations

when i try to complete select with hibernate, thrown this exception:

org.hibernate.hql.internal.ast.QuerySyntaxException: formato is not mapped [from formato]
    at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)
    at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)
    at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:93)
    at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:324)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3420)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3309)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:706)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:562)

i check my object annotations and think that its ok, this is the code:

@Entity
@Table(name="formato")
public class Formato implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 5849413670083213438L;
    @Id
    @Column(name="id")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    @Column(name="formato")
    private String formato;
    @Column(name="tipoItem")
    private int tipoItem;

in BD the atributtes have the same name

obviously put getters and setters, and my DAO:

@Transactional
    public List<Formato> findAllFormatos() {
        Transaction tx = null;
        session = sessionFactory.getCurrentSession();
        try {
            tx = session.beginTransaction();
            List formatos = session.createQuery("from formato").list();
            System.out.println("Pase :'D");
            tx.commit();
            return formatos;

        } catch (Exception e) {
            // TODO: handle exception
            if (tx != null) tx.rollback();
            e.printStackTrace();
            return null;
        }


    }

finally controller but i think that problem is in the DAO

@RequestMapping(value = "timbre", method = RequestMethod.GET)
    public String redirigir(@RequestParam("id") int id, Model model) {

        Timbre t = null;
        if (id == 1){
            t = new Timbre();
            model.addAttribute("t", t);

            List<Formato> formatos= timbreDAO.findAllFormatos();
            model.addAttribute("formatos", formatos);


            return "formTimbre";

        }   
        else if (id == 2)
            return "modificarTimbre";
        else if (id == 3)
            return "eliminarTimbre";
        else if (id == 4)
            return "buscarTimbre";
        else
            return "timbre";

    }

Upvotes: 0

Views: 179

Answers (2)

cheddarDev
cheddarDev

Reputation: 242

In the HQL , you should use the java class name and property name of the mapped @Entity instead of the actual table name and column name , so the HQL should be :

List<Formato> formatos = (List<Formato>)session.createQuery("from Formato").list();

Upvotes: 2

Zeus
Zeus

Reputation: 6576

You get that exception if you do not have the annotated entity declared in SessionFactory. If you are using spring, then in the sessionFactory Bean specify the following. It could be the .cfg file also.

<property name="annotatedClasses">
         <list>
 <value>com.somefolder.Formato</value>
</list>
</property>

Upvotes: 0

Related Questions