-->
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.  [ 3 posts ] 
Author Message
 Post subject: Named Sql Query
PostPosted: Wed Nov 19, 2008 11:39 pm 
Newbie

Joined: Wed Nov 19, 2008 11:32 pm
Posts: 2
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0

Mapping documents:<?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>
<sql-query name="depts">
<return alias="dept"
class="com.test.hibernate.DeptDTO"/>
SELECT d.dept_id AS {d.dept_id}, d.dept_name AS {d.dept_name} FROM dept d
</sql-query>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():Query query = session.getNamedQuery("depts");
List deptDTO = query.list();


Full stack trace of any exception that occurs: SEVERE: Error in named query: depts
org.hibernate.MappingException: Unknown entity: com.test.hibernate.DeptDTO
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:548)
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.getSQLLoadable(SQLQueryReturnProcessor.java:312)
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.processRootReturn(SQLQueryReturnProcessor.java:353)
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.processReturn(SQLQueryReturnProcessor.java:332)
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.process(SQLQueryReturnProcessor.java:148)
at org.hibernate.loader.custom.sql.SQLCustomQuery.<init>(SQLCustomQuery.java:64)
at org.hibernate.engine.query.NativeSQLQueryPlan.<init>(NativeSQLQueryPlan.java:43)
at org.hibernate.engine.query.QueryPlanCache.getNativeSQLQueryPlan(QueryPlanCache.java:114)
at org.hibernate.impl.SessionFactoryImpl.checkNamedQueries(SessionFactoryImpl.java:444)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:351)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1218)
at com.test.hibernate.util.SessionFactoryUtil.<clinit>(SessionFactoryUtil.java:31)
at com.test.hibernate.java.NamedQuery.main(NamedQuery.java:33)
Initial SessionFactory creation failed.org.hibernate.HibernateException: Errors in named queries: depts
Exception in thread "main" java.lang.NullPointerException
at com.test.hibernate.java.NamedQuery.main(NamedQuery.java:46)


Name and version of the database you are using:MySQL 5.0

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

The DTO that has been set in .hbm.xml package com.test.hibernate;

/**
* @author I097630
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class DeptDTO {

private int dept_id;
private String dept_name;

/**
* @return Returns the dept_id.
*/
public int getDept_id() {
return dept_id;
}
/**
* @param dept_id The dept_id to set.
*/
public void setDept_id(int dept_id) {
this.dept_id = dept_id;
}
/**
* @return Returns the dept_name.
*/
public String getDept_name() {
return dept_name;
}
/**
* @param dept_name The dept_name to set.
*/
public void setDept_name(String dept_name) {
this.dept_name = dept_name;
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 19, 2008 11:48 pm 
Regular
Regular

Joined: Tue Sep 26, 2006 11:37 am
Posts: 115
Location: Sacramento, CA
Did you forget to add com.test.hibernate.DeptDTO to your mappings in hibernate.cfg.xml?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2008 9:27 am 
Newbie

Joined: Wed Nov 19, 2008 11:32 pm
Posts: 2
I Mapped the DeptDTO in hbm.xml file
My hibernate.cfg.xml file is: <?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.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">chachu07</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<mapping resource="externhqlfrom.hbm.xml"/>
</session-factory>
</hibernate-configuration>


Should i map the DTO in hibernate.cfg.xml file? If Yes, can you provide me an example.

Thanks,
Senthil.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.