0

I'm starting out in Hibernate and following a tutorial from javabrains. But the buildsessionfactory seems to be deprecated. I searched and found the solution.

However, when i try to get object from the database, I get NullPointerException at sysout at last line and no entry is even saved into the database. But if i comment out the object retrieving section it works fine and saves to database.

What am I doing wrong ?

    public class HibernateTest {
        private static SessionFactory sessionFactory;
        private static ServiceRegistry serviceRegistry;

        private static SessionFactory configureSessionFactory() throws HibernateException {
        Configuration configuration = new Configuration();
        configuration.configure();
        serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();        
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        return sessionFactory;
        }

        public static void main(String[] args) {
        UserDetails user = new UserDetails();
        user.setUserId(1);
        user.setUserName("User's name");
        user.setAddress("Chingeltei");
        user.setJoinedDate(new Date());
        user.setDescription("Description of the user");

        Session session = configureSessionFactory().openSession();
        session.beginTransaction();
        session.save(user);
        session.getTransaction().commit();
        session.close();

  //    user = null;
  //    session = configureSessionFactory().openSession();
  //    session.beginTransaction();
  //    user = (UserDetails) session.get(UserDetails.class, 1);

  //    System.out.println("User name retreived is: "+user.getUserName());

        }

    }

UserDetails.java

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity 
@Table (name="user_details")
public class UserDetails {

    @Id
    @Column (name="userid")
    private int userId;
    @Column (name="username")
    private String userName;
    @Temporal (TemporalType.DATE)
    private Date joinedDate;
    private String address;
    @Transient
    private String description;

    public int getUserId() {
        return userId;
    }
    public String getUserName() {
        return userName;
    }
    public Date getJoinedDate() {
        return joinedDate;
    }
    public String getAddress() {
        return address;
    }
    public String getDescription() {
    return description;
    }
    public void setUserId(int aUserId) {
        userId = aUserId;
    }
    public void setUserName(String aUserName) {
        userName = aUserName;
    }
    public void setJoinedDate(Date aJoinedDate) {
        joinedDate = aJoinedDate;
    }
    public void setAddress(String aAddress) {
        address = aAddress;
    }
    public void setDescription(String aDescription) {
        description = aDescription;
    }
}
3
  • Can we see UserDetails.java? Commented Nov 20, 2013 at 3:20
  • I've added it. anything ? Commented Nov 20, 2013 at 3:31
  • Not sure about this, but try sessionFactory.openSession() rather than second call of configureSessionFactory() (where you commented out at the moment. Commented Nov 20, 2013 at 3:36

1 Answer 1

1

As the tutorial states, you should create sessionFactory once per application. So, instead of second call to configureSessionFactory() you should be using sessionFactory reference which already exists.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.