0
votes
// This is my pojo class

package com.kittu.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class User {
    @Id
    @GeneratedValue
    private int Rollno;
    private String Name;

    public int getRollno() {
        return Rollno;
    }

    public void setRollno(int Rollno) {
        this.Rollno = Rollno;
    }

    public String getName() {
        return Name;
    }

    public void setName(String Name) {
        this.Name = Name;
    }       
}

//**Main class**

package com.kittu.hibernate;

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

public class Main {
   public static void main (String args[])
   {
       User u1=new User();
       u1.setName("gontu");

       Configuration cfg = new Configuration();
       cfg = cfg.configure("hibernate.cfg.xml");
       SessionFactory sessionFactory = cfg.buildSessionFactory();
       Session session=sessionFactory.openSession();
       session.beginTransaction();

       u1 = (User)session.get(User.class,1);

       System.out.println("student object having name as" +u1.getName());

       session.getTransaction().commit();
       session.close();
       sessionFactory.close();
   }
}

// I have created database name user in mysql db

Error

run:
Aug 12, 2017 10:42:18 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.1.0.Final}
Aug 12, 2017 10:42:18 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Aug 12, 2017 10:42:18 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Aug 12, 2017 10:42:18 AM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver

resolveEntity WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead. Support for obsolete DTD/XSD namespaces may be removed at any time. Aug 12, 2017 10:42:19 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final} Aug 12, 2017 10:42:19 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!) Aug 12, 2017 10:42:19 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3360/hibernate_tutorials] Aug 12, 2017 10:42:20 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001001: Connection properties: {user=root, password=****} Aug 12, 2017 10:42:20 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001003: Autocommit mode: false Aug 12, 2017 10:42:20 AM org.hibernate.engine.jdbc.connections.internal.PooledConnections INFO: HHH000115: Hibernate connection pool size: 1 (min=1) Aug 12, 2017 10:42:20 AM org.hibernate.dialect.Dialect INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect Aug 12, 2017 10:42:21 AM org.hibernate.envers.boot.internal.EnversServiceImpl configure INFO: Envers integration enabled? : true Hibernate: drop table if exists hibernate_sequence Hibernate: drop table if exists User Hibernate: create table hibernate_sequence (next_val bigint) Hibernate: insert into hibernate_sequence values ( 1 ) Hibernate: create table User (Rollno integer not null, Name varchar(255), primary key (Rollno)) Aug 12, 2017 10:42:24 AM org.hibernate.tool.schema.internal.SchemaCreatorImpl applyImportSources INFO: HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@623e088f' Hibernate: select user0_.Rollno as Rollno1_0_0_, user0_.Name as Name2_0_0_ from User user0_ where user0_.Rollno=? Exception in thread "main" java.lang.NullPointerException at com.kittu.hibernate.Main.main(Main.java:24) BUILD STOPPED (total time: 1 minute 14 seconds)

1

1 Answers

1
votes

Session.get() always returns null if the value doesn't exists in the database..in your case if the user with id=1 doesn't exists it will return null..also if the user exists with id=1 and name as null in db, then also you will get null pointer exception while doing System.out.println("student object having name as" +u1.getName());