-5

I know this question has been asked thousand times, but i cant solve my problem... I searched all over the internet and cant find a solution... This is the error!

Hibernate 5.1

Configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"/>
<property name="hibernate.default_schema">protein_tracker</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="com/twentyone/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>

Mapping file:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jul 18, 2016 9:29:14 PM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.twentyone.User" table="USERS">
    <id name="id" type="int">
        <column name="ID" />
        <generator class="assigned" />
    </id>
    <property name="name" type="java.lang.String">
        <column name="NAME" />
    </property>
    <property name="goal" type="int">
        <column name="GOAL" />
    </property>
    <property name="total" type="int">
        <column name="TOTAL" />
    </property>
</class>
</hibernate-mapping>

HibernateUtilities class

package com.twentyone;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtilities {

private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

static {
    try {
        Configuration configuration = new Configuration().configure();

        serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);

    } catch (HibernateException e1) {
        e1.printStackTrace();
        System.out.println("Problem");
    }
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}

}

Main

 package com.twentyone;

import org.hibernate.Session;

public class Program {
public static void main(String[] args) {

    System.out.println("Hayden");

    Session session = HibernateUtilities.getSessionFactory().openSession();
    session.beginTransaction();

    User user = new User();
    user.setName("Hayden");
    user.setGoal(250);
    session.save(user);

    session.getTransaction().commit();
    session.close();
    HibernateUtilities.getSessionFactory().close(); 

}

}

Hayden
  • 61
  • 5

1 Answers1

0

It is already a classical problem: you can't use Hibernate 4 configuration code to configure Hibernate 5.

https://stackoverflow.com/a/32711654/3405171

Just use:

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

Above code works, if you have properties in hibernate.properties. You have it in hibernate.cfg.xml. I can't remember, probably, it will work for this situation too.

Any way, you can use the new Hibernate 5 Bootstrap API.

Community
  • 1
  • 1
v.ladynev
  • 19,275
  • 8
  • 46
  • 67
  • yeah... its fine i guess.. Now it says " No database selected" – Hayden Jul 18 '16 at 20:04
  • @Hayden Why you don't specify a password? Try to see in the log or console, has Hibernate loaded properties? – v.ladynev Jul 18 '16 at 20:07
  • im using phpMyAdmin, my db has no password – Hayden Jul 18 '16 at 20:08
  • @Hayden Do you see `org.hibernate.dialect.MySQLDialect` in the Hibernate log? – v.ladynev Jul 18 '16 at 20:09
  • Now there are those errors... 1-"Error parsing JNDI name []" 2-"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" 3-"could not execute statement" 4-"No database selected"... Sorry, i am new/noob in Hibernate – Hayden Jul 18 '16 at 20:13
  • i cant thank you enough... Thanks to you it is solved and now i cant continue my "Introduction To Hibernate" course! Thanks a lot – Hayden Jul 18 '16 at 21:05
  • @Hayden You are welcome :) – v.ladynev Jul 18 '16 at 21:10