- This topic has 4 replies, 3 voices, and was last updated 20 years ago by
sardiniapoint.
-
AuthorPosts
-
preeteshMemberHi !!!
I am doing a project using struts and hibernate for accessing the database….but know ata point i have stuckedmy 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
May 13, 2005 at 9:14 am #229527
Riyad KallaMemberYou 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.
May 14, 2005 at 7:07 am #229560
preeteshMemberHi!!!!!!!!
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.
😕 😕June 16, 2005 at 10:26 am #231163
sardiniapointMemberHi 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 youRegards
RinaldoJune 17, 2005 at 4:28 am #231221
sardiniapointMemberHi,
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 ThanksRinaldo
-
AuthorPosts