-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: how to use select query in hibernate
PostPosted: Tue Feb 06, 2007 7:12 am 
Beginner
Beginner

Joined: Thu Jan 04, 2007 5:46 am
Posts: 31
Hi guys
I created a table sample and mapped in config.xml.When i tried to insert through java by create query method . it shows me error

org.hibernate.hql.ast.QuerySyntaxException: sample is not mapped [select sam_id,sam_name from sample sample]
at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:158)
at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:87)
at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:70)
at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:265)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3056)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)


can anybody help me


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 7:37 am 
Newbie

Joined: Mon Jan 22, 2007 3:45 pm
Posts: 9
Hi mnrnjn,

Hibernate cannot find the mapping file. Ensure that config.xml is included in hibernate-configuration->session-factory->mapping-resource


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 7:42 am 
Newbie

Joined: Tue Sep 05, 2006 10:39 am
Posts: 13
It's difficult to say what you are doing wrong, but you say "mapped in config.xml" which sounds dubious. You have a mapping file, e.g. Sample.hbm.xml? Do you have that configured as a mapping resource for your Hibernate session factory?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 7:46 am 
Beginner
Beginner

Joined: Thu Jan 04, 2007 5:46 am
Posts: 31
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.connection.username">test</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool_size">1</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
-->
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>



<!-- Mapping files -->
<mapping resource="metexim/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>


this is my hibernate conf.xml
i do have mapping file user.hbm.xml

i worked when is inserted a data into my database

but not working when iam retriving data

help me


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 8:05 am 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi mnrnjn,

change it to

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.connection.username">test</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool_size">1</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
-->
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>



<!-- Mapping files -->
<mapping resource="metexim/User.hbm.xml"/>
<mapping resource="metexim/Sample.hbm.xml"/>

</session-factory>
</hibernate-configuration>

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 06, 2007 8:14 am 
Beginner
Beginner

Joined: Thu Jan 04, 2007 5:46 am
Posts: 31
Hi
thanks for ur reply. I changed but i get the same error


here is my code
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
// session.beginTransaction();
System.out.println("0");

String SQL_QUERY ="from sample sample";
System.out.println("1");
Query query = session.createQuery(SQL_QUERY);
System.out.println("2");
for(Iterator it=query.iterate();it.hasNext();)
{
System.out.println("3");
Sample sample=(Sample)it.next();
System.out.println("ID: " + sample.getId());
System.out.println("First Name: " + sample.getName());
}
System.out.println("Done");
//session.getTransaction().commit();
session.close();


===========================

package metexim;

import java.util.Date;

public class Sample {
private Long id;

private String title;
//private Date date;

public Sample() {}

public Long getId() {
return id;
}

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

/*public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}
*/
public String getName() {
return title;
}

public void setName(String title) {
this.title = title;
}
}


hepl me


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 07, 2007 5:26 am 
Newbie

Joined: Mon Jan 22, 2007 10:23 am
Posts: 3
Use this
String SQL_QUERY ="from metexim.Sample sample"; instead of
String SQL_QUERY ="from sample sample";


We have to give the fully qualified name of the peristent Entity in the select query or simply the ClassName. The Entity name is case sensitive


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.