Hibernate session.get() shows null

625 Views Asked by At

Although I read session.get null problems on stackoverflow and tried to do what they suggested, my problem seems to continue.

SessionFactory mysessionfactory = new Configuration().configure().buildSessionFactory();
Session mysession = mysessionfactory.openSession();
mysession.beginTransaction();

Myperson person3 = (Myperson) mysession.get(Myperson.class, 3);
System.out.println("there you go : "+person3.getName());

As simple as it seems, i am just trying to retrieve data from database. Let me show you the database.

As you can see above , the data i like to retrieve has a name !

This is what i get, NOTHING ! And finally , i run the code with debug mode and i hope you'll understand the problem and suggest a solution.

enter image description here

i am in a conflict here, my database shows that data which has UserId of 3 has a name but why can't i see it in my output ?

EDIT : Here is my hibernate xml file as you requested.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

 <hibernate-configuration>
    <session-factory>

      <property name="hibernate.dialect">
        org.hibernate.dialect.MySQLDialect
      </property>

      <property name="hibernate.connection.driver_class">
        com.mysql.jdbc.Driver
      </property>

      <property name="connection.pool.size">1</property>

      <property name="hibernate.connection.url">
        jdbc:mysql://localhost:3306/myhibernatedb
      </property>

      <property name="hibernate.connection.username">
       root
      </property>

      <property name="hibernate.connection.password">
       000003
      </property>

      <property    name="hibernate.current_session_context_class">thread</property>

      <property name="show_sql">true</property>

      <property name="hbm2ddl.auto">update</property>

      <!-- List of XML mapping files -->

      <mapping class="org.emreverim.com.Myperson"/>
      <mapping class="org.emreverim.com.Vehicle" />
      <mapping class="org.emreverim.com.FourWheeler" />
      <mapping class="org.emreverim.com.TwoWheeler" />

    </session-factory>
  </hibernate-configuration>

EDIT 2 : here is myperson class as you requested.

@Entity
public class Myperson {

    @Id @GeneratedValue(strategy=GenerationType.AUTO)   
    private int userID;     
    private String name;    


    @OneToMany(cascade=CascadeType.PERSIST)
    private Collection<Vehicle> vehicle = new ArrayList<Vehicle>();


    public Collection<Vehicle> getVehicle() {
        return vehicle;
    }
    public void setVehicle(Collection<Vehicle> vehicle) {
        this.vehicle = vehicle;
    }

    @Lob
    private String description;
    @Temporal (TemporalType.DATE)
    private Date joinedDate;


    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public Date getJoinedDate() {
        return joinedDate;
    }
    public void setJoinedDate(Date joinedDate) {
        this.joinedDate = joinedDate;
    }
    public int getUserID() {
        return userID;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public void setUserID(int userID){

        this.userID = userID;
    }

}
3

There are 3 best solutions below

0
On

In your entity map you forgot to add the annotations @column attributes to the field and @table to link it to the table.

Example:

@Column(name="description")
private String description;

Check this example for the annotations http://archive.oreilly.com/pub/a/onjava/2007/02/08/an-introduction-to-hibernate-3-annotations.html?page=2

0
On

I think the problem here is caused by the fact that Hibernate Session.get() method expects a Seriliazable id object, which is not provided in your case here mysession.get(Myperson.class, 3) .

Because the given value 3 here is of primitive type int which contains only the value and isn't serialized so you have to use new Integer() method in order to pass an Integer serializable object or new Long() to pass a Long serializable object to the get() method, so just change:

Myperson person3 = (Myperson) mysession.get(Myperson.class, 3);

To:

Myperson person3 = (Myperson) mysession.get(Myperson.class, new Integer(3));

Or:

Myperson person3 = (Myperson) mysession.get(Myperson.class, new Long(3));
0
On

We happened to see the solution, my database has been configured as utf-16 while it should have been utf-8. so its just as simple as that. i gave thumps up for all answers, thanks for all of your concerns.