I am trying to retrieve data from PostgreSql database through Hibernate. I am using the Hibernate Entity Manager 3.4.0.GA download but changed my persistence.xml to refer
http://java.sun.com/xml/ns/persistence/ ... ce_1_0.xsd, as I was receiving an error with persistence_2.0.xsd (I submitted a different topic for that error).
I am getting the below error on datasource. Please note that I am going through all the available context and listing them before letting EntityManager load the datasource. jdbc/postgres is available as a datasource as per debugging messages.
Code:
PU. before initialcontext
org.apache.naming.NamingContext jdbc
org.apache.tomcat.jdbc.pool.DataSource postgres
PU.After Context list
ERROR: org.hibernate.connection.DatasourceConnectionProvider - Could not find datasource: jdbc/postgres
javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
at org.apache.naming.NamingContext.lookup(NamingContext.java:770)
at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:152)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:75)
at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:137)
at org.hibernate.ejb.InjectionSettingsFactory.createConnectionProvider(InjectionSettingsFactory.java:29)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:89)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2101)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1325)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
My buildEntityManager method is as below.
Code:
private static EntityManagerFactory buildEntityManagerFactory(){
EntityManagerFactory em = null;
System.out.println("PU. before initialcontext");
try {
Context ctx1 = (Context) new InitialContext().lookup("java:comp/env");
listContext(ctx1, "");
System.out.println("PU.After Context list");
} catch (NamingException nae) {
System.out.println("PU. NAE Message = " + nae.getMessage());
nae.printStackTrace();
}
try{
em = Persistence.createEntityManagerFactory("manager1");
} catch(Exception e){
e.printStackTrace();
}
return em;
}
private static final void listContext(Context ctx, String indent) {
try {
NamingEnumeration list = ctx.listBindings("");
while (list.hasMore()) {
Binding item = (Binding) list.next();
String className = item.getClassName();
String name = item.getName();
System.out.println(indent + className + " " + name);
Object o = item.getObject();
if (o instanceof javax.naming.Context) {
listContext((Context) o, indent + " ");
}
}
} catch (NamingException ex) {
System.out.println("JNDI failure: " + ex);
ex.printStackTrace();
}
}
My persistence.xml is as below
Code:
<persistence 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"
version="1.0">
<persistence-unit name="manager1">
<jta-data-source>jdbc/postgres</jta-data-source>
<!-- By default, Bean Validation (and Hibernate Validator) is activated -->
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="validate"/>
<property name="hibernate.id.new_generator_mappings" value="true"/>
<!-- Enable c3p0 connection pool management -->
<property name="hibernate.c3p0.min_size" value="5"/>
<property name="hibernate.c3p0.max_size" value="20"/>
<property name="hibernate.c3p0.timeout" value="1800"/>
<property name="hibernate.c3p0.max_statements" value="50"/>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class" value="thread"/>
</properties>
</persistence-unit>
</persistence>
Thanks for your help.