-->
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: SOS: hibernate query, findId no result, but other well!
PostPosted: Wed Aug 23, 2006 2:04 am 
Newbie

Joined: Wed Aug 23, 2006 1:54 am
Posts: 5
I use "STRUCT+SPRING+HIBERNATE" framework to connect ORACLE database:
all configuration is right, execute as follows:
....
Session session=SessionFactoryUtils.getSession(usernameDAO.getSessionFactory(), false);
Query q=session.createQuery("from Username user where user.username='AAA1'");
Iterator it=q.list().iterator();
Username name=new Username();
while(it.hasNext()){
name=(Username)it.next();
}
System.out.println(name.getDepartment());
.....
here i can get the result from the console: test department
but if i execute as follow:
Username name=this.usernameDAO.findById("AAA1");
System.out.println(name.getDepartment());
i see the sql was executed from the console, but no result printed and there is an null exception throwed, name is null

Username.hbm.xml configurtation is:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="com.test.Username" table="USERNAME" schema="TT">
<id name="username" type="string">
<column name="USERNAME" length="8" />
<generator class="assigned" />
</id>
<property name="longname" type="string">
<column name="LONGNAME" length="60" />
</property>
<property name="department" type="string">
<column name="DEPARTMENT" length="30" />
</property>
<property name="functionnametype" type="string">
<column name="FUNCTIONNAMETYPE" length="8" />
</property>
<property name="versionno" type="long">
<column name="VERSIONNO" precision="22" scale="0" not-null="true" />
</property>
<property name="updatedwhen" type="date">
<column name="UPDATEDWHEN" length="7" not-null="true" />
</property>
<property name="updatedby" type="long">
<column name="UPDATEDBY" precision="22" scale="0" not-null="true" />
</property>
<property name="userid" type="long">
<column name="USERID" precision="22" scale="0" />
</property>
<property name="password" type="string">
<column name="PASSWORD" length="40" not-null="true" />
</property>
<property name="usermenu" type="string">
<column name="USERMENU" length="30" />
</property>
<property name="validInDb" type="string">
<column name="VALID_IN_DB" length="30" />
</property>
</class>
</hibernate-mapping>
I dont know why?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 23, 2006 3:39 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
Post usernameDAO.findById method


Top
 Profile  
 
 Post subject: UsernameDAO code
PostPosted: Wed Aug 23, 2006 3:44 am 
Newbie

Joined: Wed Aug 23, 2006 1:54 am
Posts: 5
public class UsernameDAO extends HibernateDaoSupport {

private static final Log log = LogFactory.getLog(UsernameDAO.class);

protected void initDao() {
//do nothing
}

public void save(Username transientInstance) {
log.debug("saving Username instance");
try {
getHibernateTemplate().saveOrUpdate(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}

public void delete(Username persistentInstance) {
log.debug("deleting Username instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}

public Username findById( java.lang.String id) {
log.debug("getting Username instance with id: " + id);
try {
Username instance = (Username) getHibernateTemplate()
.get("com.shenzhenair.medet.bo.users.Username", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}


public List findByExample(Username instance) {
log.debug("finding Username instance by example");
try {
List results = getSession()
.createCriteria("com.shenzhenair.medet.bo.users.Username")
.add(Example.create(instance))
.list();
log.debug("find by example successful, result size: " + results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}

public Username merge(Username detachedInstance) {
log.debug("merging Username instance");
try {
Username result = (Username) getHibernateTemplate()
.merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}

public void attachDirty(Username instance) {
log.debug("attaching dirty Username instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}

public void attachClean(Username instance) {
log.debug("attaching clean Username instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}

public static UsernameDAO getFromApplicationContext(ApplicationContext ctx) {
return (UsernameDAO) ctx.getBean("UsernameDAO");
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 23, 2006 3:59 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
In your map file you are using com.test.Username but in the dao you are using com.shenzhenair.medet.bo.users.Username.
Why?


Top
 Profile  
 
 Post subject: change it
PostPosted: Wed Aug 23, 2006 4:09 am 
Newbie

Joined: Wed Aug 23, 2006 1:54 am
Posts: 5
because i post the question, i want to keep some information about the detail.
com.shenzhenair.medet.bo.users.Username is the original, i just change it. i forget to change it to com.test.Username . that is the same !


Top
 Profile  
 
 Post subject: anybody can help me ?
PostPosted: Wed Aug 23, 2006 9:55 am 
Newbie

Joined: Wed Aug 23, 2006 1:54 am
Posts: 5
i debug the process:
public Object execute(HibernateCallback action, boolean exposeNativeSession) throws DataAccessException {
Session session = getSession();
boolean existingTransaction = SessionFactoryUtils.isSessionTransactional(session, getSessionFactory());
if (existingTransaction) {
logger.debug("Found thread-bound Session for HibernateTemplate");
}

FlushMode previousFlushMode = null;
try {
previousFlushMode = applyFlushMode(session, existingTransaction);
enableFilters(session);
Session sessionToExpose = (exposeNativeSession ? session : createSessionProxy(session));
Object result = action.doInHibernate(sessionToExpose);
flushIfNecessary(session, existingTransaction);
return result;
}
catch (HibernateException ex) {
throw convertHibernateAccessException(ex);
}
catch (SQLException ex) {
throw convertJdbcAccessException(ex);
}
catch (RuntimeException ex) {
// Callback code threw application exception...
throw ex;
}
finally {
if (existingTransaction) {
logger.debug("Not closing pre-bound Hibernate Session after HibernateTemplate");
disableFilters(session);
if (previousFlushMode != null) {
session.setFlushMode(previousFlushMode);
}
}
else {
// Never use deferred close for an explicitly new Session.
if (isAlwaysUseNewSession()) {
SessionFactoryUtils.closeSession(session);
}
else {
SessionFactoryUtils.closeSessionOrRegisterDeferredClose(session, getSessionFactory());
}
}
}
}
when debug into:
Session sessionToExpose = (exposeNativeSession ? session : createSessionProxy(session));
the console shows:
Hibernate: /* load com.shenzhenair.medet.bo.users.Username */ select username0_.USERNAME as USERNAME0_, username0_.LONGNAME as LONGNAME19_0_, username0_.DEPARTMENT as DEPARTMENT19_0_, username0_.FUNCTIONNAMETYPE as FUNCTION4_19_0_, username0_.VERSIONNO as VERSIONNO19_0_, username0_.UPDATEDWHEN as UPDATEDW6_19_0_, username0_.UPDATEDBY as UPDATEDBY19_0_, username0_.USERID as USERID19_0_, username0_.PASSWORD as PASSWORD19_0_, username0_.USERMENU as USERMENU19_0_, username0_.VALID_IN_DB as VALID11_19_0_ from AMICOS.USERNAME username0_ where username0_.USERNAME=?
but run to next command:
Object result = action.doInHibernate(sessionToExpose);
it shows that the result is null!
in fact, in database there is a results, why it canot show?


Top
 Profile  
 
 Post subject: have anybody know how to fix it
PostPosted: Wed Aug 23, 2006 9:28 pm 
Newbie

Joined: Wed Aug 23, 2006 1:54 am
Posts: 5
have anybody know how to fix it? thank you!


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.