facebook

regardin hql

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #229524 Reply

    preetesh
    Member

    Hi !!!
    I am doing a project using struts and hibernate for accessing the database….but know ata point i have stucked

    my problem is that…..i am not getting specific hql query to retrieve one full table. as in my project there are 20 tables….. but when i write a hql quer for a table “ASSET” of my project as

    select b from Asset as b order by b.assetNo;

    i am getting error…..

    net.sf.hibernate.QueryException:Unexpected token:as [select b from Asset as b order by b.assetNo;]

    so plz suggest some way to come out of it……

    — Preetesh

    #229527 Reply

    Riyad Kalla
    Member

    You are confusing straight SQL with Hibernate, keep in mind that your first “b” is trying to be mapped to an Object, if you only want 1 field from your table and are trying to do straight SQL, please re-read the section of the Hibernat docs on querying and performing normal queries.

    #229560 Reply

    preetesh
    Member

    Hi!!!!!!!!

    What r u told is right in this code the first b is mapping to the object and this object reference I’m using in my model to get list of all the assets in my database.

    the code for this method is as follows:

    public Asset[] getAllAssets(){
    /* will hold the assets we are going to return later */
    List assets = new ArrayList();
    /* a Hibernate session */
    Session session = null;
    /* we always need a transaction */
    Transaction tx = null;
    try {
    /* get session of the current thread */
    session= HibernetSessionFactory.currentSession();
    tx = session.beginTransaction();
    System.out.println(“Select query”);
    Query query = session.createQuery(“select b from Asset as b order by b.assetNo”);
    System.out.println(“After Select query”);
    for (Iterator iter = query.iterate(); iter.hasNext();) {
    System.out.println(“iterating”);
    assets.add((Asset) iter.next());
    }
    tx.commit();
    }catch (HibernateException e) {
    e.printStackTrace();
    //it is recommended to roll back the transaction
    //after an error occured
    if (tx != null) try {
    tx.rollback();
    } catch (HibernateException e1) {
    e1.printStackTrace();
    }
    } finally {
    try {
    if (session != null) session.close();
    } catch (HibernateException e1) {
    e1.printStackTrace();
    }
    }
    return (Asset[]) assets.toArray(new Asset[0]);
    }

    this method will return me list of object but it still giving me same error
    the Asset.Hbm.xml file is
    <class name=”Asset” table=”ASSET”>
    <id name=”assetNo” column=”ASSET_NO” type=”java.lang.String”>
    <generator class=”sequence”>
    <param name=”sequence”>seq_asset</param>
    </generator>
    </id>

    <property name=”assetDesc” column=”ASSET_DESC” type=”java.lang.String” />
    <property name=”modelNo” column=”MODEL_NO” type=”java.lang.String” />
    <property name=”serialNo” column=”SERIAL_NO” type=”java.lang.String” />
    <property name=”dateAcquired” column=”DATE_ACQUIRED” type=”java.util.Date” />
    <property name=”purchasedPrice” column=”PURCHASED_PRICE” type=”java.lang.String” />
    <property name=”manufacturers” column=”MANUFACTURERS” type=”java.lang.String” />

    <many-to-one name=”assetCategory” column=”ASSET_CATEGORY_CODE” class=”AssetCategory” />

    <many-to-one name=”assetStatus” column=”ASSET_STATUS_ID” class=”AssetStatus” />

    <many-to-one name=”employee” column=”EMPLOYEENO” class=”com.finolex.amms.model.employee.Employee” />

    <many-to-one name=”assetLocation” column=”LOCATION_NO” class=”com.finolex.amms.model.department.Location” />

    <many-to-one name=”assetDepartment” column=”DEPARTMENT_NO” class=”com.finolex.amms.model.department.Location” />
    </class>

    the Location table is having composite primary key.So i’m having problem in definning <set> in the Location table.So can you suggest me the mapping. The Location table is having Primary key as LocationNo and DepartmentNo which is foreign key references to Department table .Please guide me in this problem.As this is quiet complex .

    Thanking u in advance.
    😕 😕

    #231163 Reply

    Hi all
    have the same problem
    I need to retrieve all the data from a table and I try to use the following istruction:
    Transaction tx = null;
    try {
    session = HibernateSessionFactory.currentSession();
    tx = session.beginTransaction();
    Query query = session.createQuery(“select a from areageo as a”);
    for (Iterator iter = query.iterate(); iter.hasNext();) {
    Areageos.add((Areageo) iter.next());
    }
    ….
    compile ok
    use it with jboss receive the following error
    net.sf.hibernate.QueryException: unexpected token: as [select a from areageo as a]

    Please can somebody help me
    Thank you

    Regards
    Rinaldo

    #231221 Reply

    Hi,
    trying to change the query and now receive the following error
    net.sf.hibernate.QueryException: * only allowed inside aggregate function in SELECT [select * from areageo]
    I’m new with hibernate and very confused can you please help me to know what kind of error I’m doing.
    Many Thanks

    Rinaldo

Viewing 5 posts - 1 through 5 (of 5 total)
Reply To: regardin hql

You must be logged in to post in the forum log in