-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Database error
PostPosted: Wed Apr 17, 2013 2:30 am 
Newbie

Joined: Fri Apr 12, 2013 1:53 am
Posts: 18
I have two pojo classes Student and Address. The respective tables for those classes are 'student' and 'address'. There is one-to-one mapping from address to student. I'm getting the following error during the execution.

The Address.java code:::::
package onetoone;;
public class Address {

private int addressId;
private String city;
private String state;
private Student s;

public Student getS() {
return s;
}
public void setS(Student s) {
this.s = s;
}
public int getAddressId() {
return addressId;
}
public void setAddressId(int addressId) {
this.addressId = addressId;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}

}

The Student.java code is as follows:::::
package onetoone;

public class Student {

private int studentId;
private String studentName;

public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}

}

I inserted the values in the database but while retrieving the data using the following code I'm getting an error at the line a.getCity()

The Application class code is as follows:::::
package onetoone;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.util.*;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class OneToOne{


public static void main(String args[])
{
SessionFactory factory=null;
Session session=null;
try
{
factory=new Configuration().configure().buildSessionFactory();
session=null;
session=factory.openSession();
Object o = session.get(Address.class, new Integer(100));
Address a = (Address)o;
System.out.println(a.getCity());
Student s=a.getS();
System.out.println(s.getStudentName());
}
catch (HibernateException he)
{
System.out.println(he.getMessage());
}
finally
{
//SessionFactory close
factory.close();
//Session close
session.close();
}
}
}

The error is as follows::::::::
Apr 17, 2013 2:28:26 PM org.hibernate.impl.SessionFactoryImpl close
Hibernate: select address0_.address_id as address1_0_1_, address0_.city as city0_1_, address0_.state as state0_1_, student1_.student_id as student1_1_0_, student1_.student_name as student2_1_0_ from address address0_ left outer join student student1_ on address0_.address_id=student1_.student_id where address0_.address_id=?
INFO: closing
Apr 17, 2013 2:28:26 PM org.hibernate.connection.DriverManagerConnectionProvider close
INFO: cleaning up connection pool: jdbc:mysql://localhost:3306/database1?zeroDateTimeBehavior=convertToNull
Exception in thread "main" java.lang.NullPointerException
at onetoone.OneToOne.main(OneToOne.java:24)
Java Result: 1
BUILD SUCCESSFUL (total time: 40 seconds)


Top
 Profile  
 
 Post subject: Re: Database error
PostPosted: Thu Apr 18, 2013 8:03 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Code:
java.lang.NullPointerException at onetoone.OneToOne.main(OneToOne.java:24)


tells us clearly that variable

o == null

which means that the there is no persistent instance on database with id set to 100.
I suggest you to log the jdbc-activities with a JDBCLogger like p6spy,
so you will see the exact sql which is executed against your database and so you will hopeful also understand why it returns 0 records.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.