Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:3.0.5
Mapping documents:<hibernate-mapping>
<class name="bnice.golf.data.Player" table="PLAYER">
<id name="playerId" column="PLAYER_ID" unsaved-value="null">
<generator class="identity"/>
</id>
<property name="username" type="string" column="username" length="16" not-null="true"/>
<property name="password" length="16" column="password" not-null="true"/>
<property name="firstName" column="first_name" length="16" not-null="true"/>
<property name="lastName" column="last_name" length="16" not-null="true"/>
<property name="handicap"/>
<many-to-one name="address" column="address_id" class="Address"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
public Player login(final String username, final String password) {
HibernateTemplate template = new HibernateTemplate(this.sessionFactory);
return (Player) template.execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Query query = session.createQuery(
"from bnice.golf.data.Player player where player.username=? and password=?");
query.setString(0, username);
query.setString(1, password);
System.out.println("Size:"+query.list().size()+" "+username+" "+password+" "+query.getQueryString());
return query.uniqueResult();
}
});
}
Full stack trace of any exception that occurs:
Name and version of the database you are using:MySQL 4
The generated SQL (show_sql=true):
from bnice.golf.data.Player player where username=? and password=?
Debug level Hibernate log excerpt:
I'm using hibernate with Spring, here is my Spring configuratin:
Code:
<!-- DATABASE -->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/myGolf</value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/bnice/golf/data</value>
</list>
</property>
</bean>
<!-- DAO -->
<bean id="playerDAO" class="bnice.golf.dao.PlayerDAOImpl">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
When I run this, I get no rows returned from the query, even though there is a row that matches in the database. The println for query.list() always returns "Size: 0". It looks like my database information is set up correctly because I can take out the Hibernate code and put straight JDBC code in and I am able to retrieve the row:
Code:
DataSource ds = null;
try {
try {
Context ic = (Context) new InitialContext().lookup("java:comp/env");
ds = (DataSource) ic.lookup("jdbc/myGolf");
} catch (Exception e) {
System.out.println("Error getting DS.");
}
Connection con = ds.getConnection();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("Select * from player where username='bcnice99' and password='password'");
if (rs.next()) {
System.out.println("Found");
}
else {
System.out.println("not Found");
}
con.close();
} catch (Exception e) {
System.out.println("Error getting DataSource");
}
return showForm(req, res, errors);
Any ideas why this is not working? I'm new to Hibernate so any help is greatly appreciated!
thanks
Brian