Dmytro
Dmytro

Reputation: 159

Error with Hibernate:Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

Here, I create Session:

public class HibernateSessionFactoryUtil {

    private static SessionFactory sessionFactory;

    public HibernateSessionFactoryUtil() { }

    public static SessionFactory getSessionFactory() {
        if (sessionFactory == null) {
            try {
                Configuration configuration = new Configuration().configure();
                configuration.addAnnotatedClass(User.class);

                StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
                sessionFactory = configuration.buildSessionFactory(builder.build());
            } catch (Exception e) {
                System.out.println("Error!" + e);
            }
        }

        return sessionFactory;
    }
}

MyUser:

@Entity
@Table(appliesTo = "users")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String lastName;
    private String firstName;
    private String position;
    private int department;

    public User(){}

    public User(String lastName, String firstName, String position, int department) {
        this.lastName = lastName;
        this.firstName = firstName;`
        this.position = position;
        this.department = department;
    }

}

In DAOImpl, at the line where I initialize the session, I got an error:

public void save(final Object o) {
    if (o == null) {
        logger.error("**DAO - save* EMPTY OBJ");
        return;
    }

    Session session = HibernateSessionFactoryUtil.getSessionFactory().openSession();
    Transaction tx1 = session.beginTransaction();
    session.save(o);
    tx1.commit();
    session.close();
}

Exception:

Exception in thread "main" java.lang.NullPointerException
    at codebox.dao.DAOImpl.save(DAOImpl.java:50)
    at codebox.services.UserService.saveUser(UserService.java:19)
    at codebox.Application.main(Application.java:16)

Upvotes: 0

Views: 10089

Answers (2)

Dmytro
Dmytro

Reputation: 159

The error was that I did not initialize the context: For an example in main:

ConfigurableApplicationContext context = SpringApplication.run (Application.class, args);
MyService myService = (MyService) context.getBean ("myService");

In this service, the main beans were pulled up

Upvotes: 1

Level_Up
Level_Up

Reputation: 824

The problem that you have is most probably caused because you did not configure the configuration object. Please add hibernate.connection.username, hibernate.connection.password, hibernate.dialect, hibernate.hbm2ddl.auto and try again. If you do not set these properties then every time your session will be null. You also can check how to do this with hibernate.cfg.xml file.

For example:

Configuration configuration = new Configuration().configure();
configuration.addAnnotatedClass(User.class);
configuration.setProperty("hibernate.connection.username", "sa");
configuration.setProperty("hibernate.connection.password", "");
configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
configuration.setProperty("hibernate.hbm2ddl.auto", "create-drop");

Good luck!

Upvotes: 1

Related Questions