1

This is my first time using Hiberante.

I am trying to create a Hibernate session within my application using the following:

Session session = HiberanteUtil.getSessionFactory().openSession();

It gives me this error:

org.hibernate.HibernateException: /hibernate.cfg.xml not found

However I do not have a hibernate.cfg.xml file within my project.

How can I create a session without having this file?

alexbt
  • 16,415
  • 6
  • 78
  • 87
java123999
  • 6,974
  • 36
  • 77
  • 121
  • I want to set hibernate up using just annotations, i.e. in this guide: http://www.concretepage.com/hibernate/configure_hibernate_without_hibernate_cfg_xml – java123999 Feb 17 '16 at 12:52

2 Answers2

4
import java.util.Properties;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import com.concretepage.persistence.User;

public class HibernateUtil {
    private static final SessionFactory concreteSessionFactory;
    static {
        try {
            Properties prop= new Properties();
            prop.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/hibernate");
            prop.setProperty("hibernate.connection.username", "root");
            prop.setProperty("hibernate.connection.password", "");
            prop.setProperty("dialect", "org.hibernate.dialect.MySQLDialect");

            concreteSessionFactory = new AnnotationConfiguration()
           .addPackage("com.concretepage.persistence")
                   .addProperties(prop)
                   .addAnnotatedClass(User.class)
                   .buildSessionFactory();
        } catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }
    }
    public static Session getSession()
            throws HibernateException {
        return concreteSessionFactory.openSession();
    }

    public static void main(String... args){
        Session session=getSession();
        session.beginTransaction();
        User user=(User)session.get(User.class, new Integer(1));
        System.out.println(user.getName());
        session.close();
    }
    }
P S M
  • 1,121
  • 12
  • 29
  • Thanks, do you recommend this way over the other hibernate.cfg.xml? – java123999 Feb 17 '16 at 12:58
  • I am confused as to what this means: import com.concretepage.persistence.User; ? – java123999 Feb 17 '16 at 14:34
  • @java123999 It is your persistent class. You can use any class for this with `@Entity` and `@Table` annotations. – v.ladynev Feb 17 '16 at 15:19
  • Hi,java123999 import com.concretepage.persistence.User is for my POJO class.YOU can import your POJO. This way you can avoid xml file. – P S M Feb 18 '16 at 05:20
  • `AnnotationConfiguration` removed from Hibernate 5 – gavenkoa Mar 02 '17 at 14:31
  • “org.hibernate.cfg.AnnotationConfiguration” is deprecated, and all its functionality has been moved to “org.hibernate.cfg.Configuration“. So, you can safely replace your “AnnotationConfiguration” with “Configuration” class. – P S M Mar 07 '17 at 13:03
  • But why do we mix all these stuff in java class let it be separate xml file file itself – srinivas gowda Sep 06 '17 at 11:28
2

The simples way to configure Hibernate 4 or Hibernate 5

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

Hibernate reads a configuration from hibernate.cfg.xml and hibernate.properties.

You shouldn't call configure(), if you don't want to read hibernate.cfg.xml. With adding an annotated class

SessionFactory sessionFactory = new Configuration()
    .addAnnotatedClass(User.class).buildSessionFactory();
v.ladynev
  • 19,275
  • 8
  • 46
  • 67