Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to setup hibernate datasouce in Java SE
    text
    copied!<p>I read almost all thread about How configure hibernate datasource but I can't find help. I mean in <code>hibernate.cfg.xml</code> element <code>&lt;property name="hibernate.connection.datasource"&gt; ? &lt;/property&gt;</code> i know i have to set up with jndi. i try to google it but all articles are based on developing with jbossas, ejb, tomcat, weblogic and their jndi. But i need jndi of java SE. Correct me please if i am wrong.</p> <p>I am new to Hibernate so I am using NetBeans, SE project with Hibernate 3.2.5 jars. (I am studying Hibernate from book Beginning Hibernate 2nd edition, apress and source code derive on the book...)</p> <p>My <code>hibernate.cfg.xml</code></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt; &lt;hibernate-configuration&gt; &lt;property name="hibernate.connection.driver_class"&gt; com.mysql.jdbc.Driver &lt;/property&gt; &lt;property name="hibernate.connection.url"&gt; jdbc:mysql://127.0.0.1:3306/asd &lt;/property&gt; &lt;property name="hibernate.connection.username"&gt;root&lt;/property&gt; &lt;!-- nastaveni dialektu --&gt; &lt;property name="hibernate.dialect"&gt; org.hibernate.dialect.MySQLInnoDBDialect &lt;/property&gt; &lt;!-- jndi nastaveni --&gt; &lt;property name="hibernate.connection.datasource"&gt; java:hibernate/SessionFactory &lt;/property&gt; &lt;property name="hibernate.connection.username"&gt;root&lt;/property&gt; &lt;property name="cache.provider_class"&gt; org.hibernate.cache.NoCacheProvider &lt;/property&gt; &lt;property name="hibernate.jndi.class"&gt;javax.naming.InitialContext&lt;/property&gt; &lt;/session-factory&gt; &lt;/hibernate-configuration&gt; </code></pre> <p>And I have only one class <code>FirstHibernate</code>:</p> <pre><code>package firsthibernate; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; public class FirstHibernate { private static SessionFactory session = null; private static Session s = null; public static void main(String[] args) { try { session = new AnnotationConfiguration().configure().buildSessionFactory(); s = session.openSession(); s.beginTransaction(); // List l = s.createQuery("from query").list(); s.getTransaction().commit(); } catch (Exception ex) { if (s.getTransaction() != null) { //s.getTransaction().rollback(); } System.out.println(ex.toString()); } finally { s.close(); } } } </code></pre> <p>I obtain this message:</p> <pre><code>SEVERE: Could not obtain initial context javax.naming.NoInitialContextException: Cannot instantiate class: javax.naming.InitialContext [Root exception is java.lang.ClassCastException: javax.naming.InitialContext cannot be cast to javax.naming.spi.InitialContextFactory] </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload