Hibernate binding problem

hi folk.

i'm considering some examples of jboss, "jbossatwork" http://www.jbossatwork.com/downloads.html. i somehow understand first 4, but i'm now stuck in 5th.

as i get, the problem is in binding hibernate. see, in 4th example (ch04) it binds through JDBC (JDBCCarDAO), and in 5th (ch05/05a-list) it tries to do through hibernate (HibernateCarDAO). when i try to run it through hibernate, in jboss a message like "hibernte not bound" appears. pls, could anybody help me to find the problem of hibernate?

lema

Message was edited by:

lema

[578 byte] By [lemaa] at [2007-11-27 11:29:42]
# 1

this is a part from ControllerServlet.java:

if(VIEW_CAR_LIST_ACTION.equals(actionName))

{

CarDAO carDAO = new HibernateCarDAO();

request.setAttribute("carList", carDAO.findAll());

destinationPage = "/carList.jsp";

this is HibernateCarDAO.java

package com.jbossatwork.dao;

import java.util.*;

import org.hibernate.*;

import com.jbossatwork.dto.CarDTO;

import com.jbossatwork.util.*;

public class HibernateCarDAO implements CarDAO

{

private List carList;

private static final String HIBERNATE_SESSION_FACTORY="java:comp/env/hibernate/SessionFactory";

public HibernateCarDAO()

{}

public List findAll()

{

List carList = new ArrayList();

Session session = null;

try

{

session = ServiceLocator.getHibernateSession(HIBERNATE_SESSION_FACTORY);

Criteria criteria = session.createCriteria(CarDTO.class);

carList = criteria.list();

}

catch (Exception e)

{

System.out.println(e);

}

finally

{

try

{

if (session != null) {session.close();}

}

catch (Exception e)

{

System.out.println(e);

}

}

return carList;

}

}

this is JDBCCarDAO.java:

package com.jbossatwork.dao;

import java.util.*;

import java.sql.*;

import javax.sql.*;

import com.jbossatwork.dto.CarDTO;

import com.jbossatwork.util.*;

public class JDBCCarDAO implements CarDAO

{

private List carList;

private static final String DATA_SOURCE="java:comp/env/jdbc/JBossAtWorkDS";

public JDBCCarDAO()

{}

public List findAll()

{

List carList = new ArrayList();

DataSource dataSource = null;

Connection conn = null;

Statement stmt = null;

ResultSet rs = null;

try

{

dataSource = ServiceLocator.getDataSource(DATA_SOURCE);

conn = dataSource.getConnection();

stmt = conn.createStatement();

rs = stmt.executeQuery("select * from CAR");

while(rs.next())

{

CarDTO car = new CarDTO();

car.setId(rs.getInt("ID"));

car.setMake(rs.getString("MAKE"));

car.setModel(rs.getString("MODEL"));

car.setModelYear(rs.getString("MODEL_YEAR"));

carList.add(car);

}

}

catch (Exception e)

{

System.out.println(e);

}

finally

{

try

{

if(rs != null){rs.close();}

if(stmt != null){stmt.close();}

if(conn != null){conn.close();}

}

catch(Exception e)

{

System.out.println(e);

}

}

return carList;

}

}

lemaa at 2007-7-29 16:29:31 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

when i change the part from ControllerServlet.java into:

if(VIEW_CAR_LIST_ACTION.equals(actionName))

{

CarDAO carDAO = new JDBCCarDAO();

request.setAttribute("carList", carDAO.findAll());

destinationPage = "/carList.jsp";

it works well, but if i leave it unchanged jboss gives a message like:

INFO [STDOUT] com.jbossatwork.util.ServiceLocatorException: com.jbossatwork.util.ServiceLocatorException: javax.naming.NamingException: Could not dereference object [Root exception is javax.naming.NameNotFoundException: hibernate not bound]

Message was edited by:

lema

lemaa at 2007-7-29 16:29:31 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

can it be because of hbm.xml file? Because, i was expecting to obtain car.hbm.xml but it gave me CarDAO.hbm.xml.

just i am following what the book says of jbossatwork examples.

here it is, CarDAO.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping

>

<class

name="com.jbossatwork.dto.CarDTO"

table="CAR"

>

<id

name="id"

column="ID"

type="int"

>

<generator class="native">

<!--

To add non XDoclet generator parameters, create a file named

hibernate-generator-params-CarDTO.xml

containing the additional parameters and place it in your merge dir.

-->

</generator>

</id>

<property

name="make"

type="java.lang.String"

update="true"

insert="true"

column="MAKE"

/>

<property

name="model"

type="java.lang.String"

update="true"

insert="true"

column="MODEL"

/>

<property

name="modelYear"

type="java.lang.String"

update="true"

insert="true"

column="MODEL_YEAR"

/>

<!--

To add non XDoclet property mappings, create a file named

hibernate-properties-CarDTO.xml

containing the additional properties and place it in your merge dir.

-->

</class>

</hibernate-mapping>

lemaa at 2007-7-29 16:29:31 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

hallloooo? isn't there any to help me pls

lemaa at 2007-7-29 16:29:31 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

You haven't configured the session factory as a JNDI resource. Either you've omitted it completely, or you've incorrectly configured it.

dcmintera at 2007-7-29 16:29:31 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6

pls say, where and how is it configured?

***********************************in ServiceLocator.java?

package com.jbossatwork.util;

import javax.naming.*;

import javax.sql.*;

import org.hibernate.SessionFactory;

import org.hibernate.Session;

/**

* <code>ServiceLocator</code> encapsulates JNDI lookups to make it

* easier to access JNDI-based resources (EJBs, DataSources,

* JMS Destinations, and so on).

*

*/

public class ServiceLocator {

/**

* Making the default (no arg) constructor private

* ensures that this class cannnot be instantiated.

*/

private ServiceLocator() {}

/**

* Gets a <code>DataSource</code> using the given JNDI name.

*

* @param dataSourceJndiName The <code>DataSource</code>'s JNDI name.

*

* @return DataSource The <code>DataSource</code>.

*

* @throws ServiceLocatorException If there are JNDI lookup problems.

*

* @see javax.sql.DataSource

*/

public static DataSource getDataSource(String dataSourceJndiName) throws ServiceLocatorException {

DataSource dataSource = null;

try {

Context ctx = new InitialContext();

dataSource = (DataSource) ctx.lookup(dataSourceJndiName);

} catch (ClassCastException cce) {

throw new ServiceLocatorException(cce);

} catch (NamingException ne) {

throw new ServiceLocatorException(ne);

}

return dataSource;

}

public static SessionFactory getHibernateSessionFactory(String jndiSessionFactoryName) throws ServiceLocatorException {

SessionFactory sessionFactory = null;

try {

Context ctx = new InitialContext();

sessionFactory = (SessionFactory) ctx.lookup(jndiSessionFactoryName);

} catch (ClassCastException cce) {

throw new ServiceLocatorException(cce);

} catch (NamingException ne) {

throw new ServiceLocatorException(ne);

}

return sessionFactory;

}

public static Session getHibernateSession(String jndiSessionFactoryName) throws ServiceLocatorException {

Session session = null;

try

{

session = getHibernateSessionFactory(jndiSessionFactoryName).openSession();

}

catch (Exception e)

{

throw new ServiceLocatorException(e);

}

return session;

}

}

*********************************** or in hibernate-service.xml?

<server>

<mbean code="org.jboss.hibernate.jmx.Hibernate"

name="jboss.har:service=Hibernate">

<attribute name="DatasourceName">java:/JBossAtWorkDS</attribute>

<attribute name="Dialect">org.hibernate.dialect.HSQLDialect</attribute>

<attribute name="SessionFactoryName">hibernate/SessionFactory</attribute>

<attribute name="CacheProviderClass">org.hibernate.cache.HashtableCacheProvider</attribute>

</mbean>

</server>

lemaa at 2007-7-29 16:29:31 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7

i guess it is done in jboss-web.xml, right?

<resource-ref>

<res-ref-name>jdbc/JBossAtWorkDS</res-ref-name>

<jndi-name>java:/JBossAtWorkDS</jndi-name>

</resource-ref>

<resource-ref>

<res-ref-name>hibernate/SessionFactory</res-ref-name>

<jndi-name>java:/hibernate/SessionFactory</jndi-name>

</resource-ref>

lemaa at 2007-7-29 16:29:31 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8

It'll be JBoss specific and I don't use it, so I can't tell you.

The name needs to be visible to applications within the container (like your one) with the fully qualified path "java:comp/env/hibernate/SessionFactory" as used in your DAO implementation.

dcmintera at 2007-7-29 16:29:31 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 9

what a pity.

and the jboss-web.xml is not changed anyhow, it is set in gensrc folder while ant'***.

lemaa at 2007-7-29 16:29:31 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 10

Amusingly, you seem to have fallen foul of the naughty word filter :-)

dcmintera at 2007-7-29 16:29:31 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 11

noo, i wanted to say "anting" but through the sign ' . i don't use naughty words eve in my mother tongue

lemaa at 2007-7-29 16:29:31 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 12

anyway, i am some sure that the problem lays in reply3.

but the hbm.xml file is set by default too while anting. i am now looking for a file which sets the name CarDAO.hbm.xml instead of car.hbm.xml

lemaa at 2007-7-29 16:29:31 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 13

ohh changing the name didn't help. so maybe the reply3 has nothing in this problem

lemaa at 2007-7-29 16:29:31 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 14

No, it's nothing to do with the mapping files. The exception you're getting is quite clear:

"javax.naming.NamingException: Could not dereference object [Root exception is javax.naming.NameNotFoundException: hibernate not bound]"

That means exactly what it says - there's no JNDI resource with the name you're asking for anywhere in the JNDI container.

dcmintera at 2007-7-29 16:29:31 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 15

is it meant, that i do not have a needed library on jboss?

or maybe there is a wrong package somewhere in configuration?

i'm some confused about the packages/paths, it is net.sf.hibernate.. but in some files it is org.hibernate..

--hibernate-service.xml

<server>

<mbean code="org.jboss.hibernate.jmx.Hibernate"

name="jboss.har:service=Hibernate">

<attribute name="DatasourceName">java:/JBossAtWorkDS</attribute>

<attribute name="Dialect">org.hibernate.dialect.HSQLDialect</attribute>

<attribute name="SessionFactoryName">java:/hibernate/SessionFactory</attribute>

<attribute name="CacheProviderClass">org.hibernate.cache.HashtableCacheProvider</attribute>

</mbean>

</server>

web.xml

...

<resource-ref >

<res-ref-name>hibernate/SessionFactory</res-ref-name>

<res-type>net.sf.hibernate.SessionFactory</res-type>

<res-auth>Container</res-auth>

</resource-ref>

...

-jboss-web.xml

...

<resource-ref>

<res-ref-name>hibernate/SessionFactory</res-ref-name>

<jndi-name>java:/hibernate/SessionFactory</jndi-name>

</resource-ref>

...

lemaa at 2007-7-29 16:29:35 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 16

-HibernateCarDAO.java

...

public class HibernateCarDAO implements CarDAO

{

private List carList;

private static final String HIBERNATE_SESSION_FACTORY="java:comp/env/hibernate/SessionFactory";

public HibernateCarDAO()

{}

...

lemaa at 2007-7-29 16:29:35 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 17

> net.sf.hibernate.SessionFactory

This definitely ought to be org.hibernate.SessionFactory. The "net.sf" packages are from Hibernate 2 whereas you appear to be using Hibernate 3

dcmintera at 2007-7-29 16:29:35 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 18

oh ya. my examples are written with the hibernate version2(because in the book the explanation of codes use net.sf.hibernate). but i got the hibernate3.jar from EJB3 folder.

so could you pls give a link where i can get the hibernate2.jar etc., if you know any?

lemaa at 2007-7-29 16:29:35 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 19

> oh ya. my examples are written with the hibernate

> version2(because in the book the explanation of

> codes use net.sf.hibernate). but i got the hibernate3.jar

> from EJB3 folder.

Didn't anything strike you as... wrong... about that?

The package name should give you a clue as to where to find them. You might find it easier to get a more up-to-date book and build it with H3 instead of H2 though.

dcmintera at 2007-7-29 16:29:35 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...