Hi
I have a mysql database from which i got netbeans to generate some entities. Netbeans also automatically generates the persistence.xml, sun-resources.xml and hibernate.cfg.xml file. I am new to hibernate so I don't know much about setting it up. All I know is that right now it isn't working and I have tried loads of things which does not work. I've made a unit test and not mattter what I do I get the error:
INFO: JNDI InitialContext properties:{}
09-07-2010 11:37:29 org.hibernate.connection.DatasourceConnectionProvider configure
SEVERE: Could not find datasource: coffee
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
My code is as follows:
The hibernate.cfg.cml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://mysql.dinhost.net:3306/coffeedrinkers</property>
<property name="hibernate.connection.username">bla</property>
<property name="hibernate.connection.password">bla</property>
</session-factory>
</hibernate-configuration>
The persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="WMCPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>coffee</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
The sun-resources.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 Resource Definitions //EN" "http://www.sun.com/software/appserver/dtds/sun-resources_1_3.dtd">
<resources>
<jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="mysql_coffeedrinkers_AnAmuserPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
<property name="serverName" value="mysql.dinhost.net"/>
<property name="portNumber" value="3306"/>
<property name="databaseName" value="coffeedrinkers"/>
<property name="User" value="bla"/>
<property name="Password" value="bla"/>
<property name="URL" value="jdbc:mysql://mysql.dinhost.net:3306/coffeedrinkers"/>
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
</jdbc-connection-pool>
<jdbc-resource enabled="true" pool-name="mysql_coffeedrinkers_AnAmuserPool" jndi-name="coffee" object-type="user"/>
</resources>
The test:
@Test
public void userManagerTest() {
try{
boolean boo = UserManager.userExists("[email protected]", MD5Sum.MD5("Kaffe"));
assertEquals("Did not find user as expected", Boolean.TRUE, boo);
System.out.println("UserManager Test completed succesfully");
}
catch (AssertionError e)
{
System.out.println(e.getMessage());
}
catch(Exception e) {
e.printStackTrace();
}
}
and the UserManager class:
public class UserManager {
/**
* Checks if user with the given email and password exists in the database
* @param email
* @param password
* @return
*/
public static boolean userExists(String email, String password) {
try{
EntityManagerFactory emf = Persistence.createEntityManagerFactory("WMCPU");
EntityManager em = emf.createEntityManager();
Query query = em.createNamedQuery("BasicUser.findByEmail");
query.setParameter("email", email);
BasicUser user = (BasicUser) query.getSingleResult();
if(user != null && user.getPassword().equals(password)) {
return true;
}
}
catch(Exception e) {
e.printStackTrace();
}
return false;
}
}
Maybe I should say that the entire application is a webapplication running on glassfish v3.
Thanks to anybody who can help.