Avatar billede fredand Forsker
27. januar 2006 - 20:11 Der er 3 kommentarer og
1 løsning

Whats wrong with this Hibernate question?

Hello!

I have implemented Hibernate as a databas layer and every thing seems to work well.

But I have some problems with my hql-statements. Hopefully you guys could give me a hint.

I got a table like:
/*
+-------------------+----------+------+-----+---------+----------------+
| Field            | Type    | Null | Key | Default | Extra          |
+-------------------+----------+------+-----+---------+----------------+
| id                | int(11)  | NO  | PRI | NULL    | auto_increment |
| nUserID          | int(11)  | NO  | MUL |        |                |
| mobilephonenumber | char(50) | NO  |    |        |                |
+-------------------+----------+------+-----+---------+----------------+
*/

This table is mapped to a class like:
package model.hibernate;

public class Product
{
    private Long id;
    private Long nUserID;
    private String mobilephonenumber;

    public Long getId()
    {
        return id;
    }

    private void setId(Long id)
    {
        this.id = id;
    }

    public Long getnUserID()
    {
        return nUserID;
    }

    public void setnUserID(Long nUserID)
    {
        this.nUserID = nUserID;
    }

      public String getMobilephonenumber()
      {
          return  mobilephonenumber;
      }

      public void setMobilephonenumber(String  a)
    {
        mobilephonenumber = a;
      }
}

It is mapped like:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class name="model.hibernate.Product" table="products">
        <id name="id" column="id">
            <generator class="increment"/>
        </id>
            <property name="nUserID"/>
            <property name="mobilephonenumber"/>

    </class>

</hibernate-mapping>

It is also registerd in the hibernate.cfg.xml like:
        <mapping resource="attentionizer/model/hibernate/Product.hbm.xml"/>

I tries to create a query like:
Object[] parameters = {customer.getId()};
Collection products = CollectionService.getCollection("from Product where Product.nUserID = ?", parameters );

The code for the query looks like:
    public static synchronized Collection getCollection(String hsql, Object[] parameters)
      {
        HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();

        Query query = HibernateUtil.getSessionFactory().getCurrentSession().createQuery(hsql);

        if(parameters != null)
        {
            for(int i = 0; i < parameters.length; i++)
            {
                query.setParameter(i, parameters[i]);
            }
        }

        List result = query.list();

        HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();
        HibernateUtil.getSessionFactory().close();

        return result;
    }

But I just get this error:

500 Internal Server Error
org.hibernate.hql.ast.QuerySyntaxException: Invalid path: 'Product.nUserID' [from model.hibernate.Product where Product.nUserID = ?]
    at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:59)
    at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:223)
    at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:156)
    at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:103)
    at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:473)
    at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1060)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1010)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
    at tools.CollectionService.getCollection(CollectionService.java:36)

So if any one could help me out it would be great!

Best regards
Fredrik
Avatar billede fredand Forsker
27. januar 2006 - 20:13 #1
I also have tried to have Product like:
public class Product
{
    private Long id;
    public Long nUserID;
    public String mobilephonenumber;
...

But it does not help and I do not think that this is the case.

/Fredrik
Avatar billede simonvalter Praktikant
27. januar 2006 - 21:41 #2
try:
Query q = s.createQuery("from Product p where p.nUserID = ?");

or try with package name
from model.hibernate.Product


How is it going with this question?
http://www.eksperten.dk/spm/677156
Avatar billede fredand Forsker
28. januar 2006 - 17:26 #3
Thanks mate!

You solved it!!!

Do you know why my attempt did not work???

Do you got any good tutorials on the net you would recomend?

Best regards
Fredrik

Btw give a svar so I can reward you!!
Avatar billede simonvalter Praktikant
30. januar 2006 - 11:41 #4
ok

I can only guess that the problem was that you used Product.nUserID without declaring  where Product was coming from. I belive p does that, but its been some time since i read on hql.

No i don't know any good tutorials, but i can recommend the book "Hibernate in Action" by Gavin king and Christian Bauer, developers of Hibernate. I have read few others who talk about hibernate, but that one is the best!
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester