hibernate configuration file and SessionFactory

Boermt-die-Buse picture Boermt-die-Buse · May 6, 2015 · Viewed 10.8k times · Source

I am struggling at the moment to find out why I get this error messages. I'm using hibernate for the first time so that I could have configured something wrong.

IMO it could be one of this 3 problems.

  • My hibernate.cfg.xml file is at the wrong "place" but I didn't change the classpath and the hibernet file is inside the src folder.
  • I get a warning at the line where I create a new SessionFactory SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); There are 2 types which i can get back from buildSessionFactory()

    1. SessionFactory
    2. Configuration

Of course I took SessionFactory but maybe I overlook something.

package hibernate;
 import java.util.Date;

 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
 import org.hibernate.cfg.Configuration;

 import persistencelayer.*;

 public class HibernateTest {

 public static void main(String[] args) {

    TestUserDetails user2 = new TestUserDetails();
    user2.setUserId(1); 
    user2.setUserName("First User");
    user2.setAddress("First User's address");
    user2.setJoinedDate(new Date());
    user2.setDescription("Description of the user goes here");

    try {
    //SessionFactory wird erzeugt, mit der Konfiguration von Hibernate
    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();

 //     session.save(user);

    session.save(user2);
    session.getTransaction().commit();
    } catch (Exception e) {
        System.out.println("Fehler beim erstellen der SessionFactory");
    }



}

}
`
  • It could also be that i configured my hibernate.cfg.xml file wrong. I am deleting the username, password and host, for safety reasons.

I am trying to find the solution since google but it is simply not working. I am using Oracle as DB btw.

Suggestions would be appreciated. Thank you in advance and sorry for the long post :).

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-configuration PUBLIC 
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>
    <!-- Database connection settings -->
    <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    <property name="hibernate.connection.url">jdbc:oracle:thin:@host:port:ssid:</property>
    <property name="hibernate.connection.username">name</property>
    <property name="hibernate.connection.password">password</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="hibernate.connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>

    <!-- Disable the second-level cache -->
    <property name="hibernate.cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="hibernate.show_sql">true</property>

    <!-- Drop and re-create the database schema on startup -->
    <property name="hibernate.hbm2ddl.auto">create</property>

    <!-- Names the annotated entity class -->
    <mapping class="persistencelayer.Project"/>
    <mapping class="persistencelayer.User"/>
    <!-- <mapping class="persistencelayer.Employer"/>
    <mapping class="persistencelayer.IndividualTest"/>
    <mapping class="persistencelayer.ObjectType"/>
    <mapping class="persistencelayer.TestChamber"/>
    <mapping class="persistencelayer.TestMethod"/>
    <mapping class="persistencelayer.TestUserDetails"/> -->


</session-factory>

Answer

Boermt-die-Buse picture Boermt-die-Buse · May 6, 2015

i have found the problem it is inside the hibernate.cfg.xml file.

i copied it from the offical hibernate 4.3.9 files. This section is wrong.

<property name="hibernate.connection.password" />password</property>

there should not be a "/" on the left side where password is meant to be put in.