-->
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.  [ 9 posts ] 
Author Message
 Post subject: re:null pointer exception
PostPosted: Mon Mar 08, 2010 5:25 am 
Beginner
Beginner

Joined: Fri Feb 26, 2010 8:23 am
Posts: 29
this is my sql query see what wrong in this and reply to me it is very urgent.i have to complete this task i am struggling in this exception for the past two days.

i have try the outer join query.

String sql_query = " select empdet.firstname,empdet.city,empmail.firstname,empmail.email FROM sample.EmpDetail empdet left outer join fetch sample.Employee empmail where empdet.Empid = empmail.Empid";


advance thanks those who reply to me.

my main class
package sample;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import java.util.*;
/*import java.util.List;
import java.util.ListIterator;*/
import sample.Employee;
import sample.EmpDetail;
public class Inner {
public static void main(String[] args) {
// TODO Auto-generated method stub
Session sess = null;
try{
SessionFactory fact = new
Configuration().configure().buildSessionFactory();
sess = fact.openSession();
String sql_query = " select empdet.firstname,empdet.city,empmail.firstname,empmail.email FROM sample.EmpDetail empdet left outer join fetch sample.Employee empmail where empdet.Empid = empmail.Empid";
Query query = sess.createQuery(sql_query);
Iterator ite = query.list().iterator();

System.out.println("FirstName\t"+"Email\t"+"FirstName\t"+"City");
while ( ite.hasNext() ) {
Object[] pair = (Object[]) ite.next();
Employee mail = (Employee) pair[0];
EmpDetail det = (EmpDetail) pair[1];
System.out.print(mail.getfirstname());
System.out.print(mail.getemail());
System.out.print("\t"+det.getfirstname());
System.out.print("\t\t"+det.getcity());
System.out.println();
}
sess.close();
}
catch(Exception e ){
System.out.println(e.getMessage());
e.printStackTrace();

}

}

}

this is my pojo class:Employee.java
package sample;

public class Employee {
private String firstname;

private String email;
private int empid;

/**
* @return Email
*/


/**
* @return First Name
*/
public String getfirstname() {
return firstname;
}

public void setfirstname(String string) {
firstname = string;
}

public String getemail() {
return email;
}


/**
* @param string Sets the Email
*/
public void setemail(String string) {
email = string;
}

/**
* @param string Sets the First Name
*/


/**
* @param string sets the Last Name
*/

public int getempid() {
return empid;
}

/**
* @param l Sets the ID
*/
public void setempid(int i) {
empid = i;
}

}

EmpDetail.java

package sample;

public class EmpDetail{

private String firstname;
private String city;
private int empid;


public String getfirstname() {
return firstname;
}

public void setfirstname(String firstname) {
this.firstname =firstname;
}

public String getcity() {
return city;
}

public void setcity(String city) {
this.city = city;
}


public int getempid() {
return empid;
}

public void setempid(int Empid) {
this.empid =empid;
}
}
contact.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="sample.Employee" table="empmail">
<id name="empid" type="int" column="Empid" >
<generator class="increment"/>
</id>

<property name="firstname">
<column name="firstname" />
</property>

<property name="email">
<column name="email"/>
</property>

</class>
</hibernate-mapping>

insurance.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="sample.EmpDetail" table="empdet">
<id name="empid" column ="Empid" type="int">
<generator class="increment"/>
</id>

<property name="firstname">
<column name="firstname" />
</property>

<property name="city">
<column name="city"/>
</property>

</class>
hibernate.cfg.xml

</hibernate-mapping>
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">

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

jdbc:mysql://localhost/sakthi</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="hibernate.jdbc.batch_size">50</property>
<property name="show_sql">true</property>
<property name="connection.autocommit">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">value="update"</property>
<!-- Mapping files -->
<mapping resource="contact.hbm.xml"/>
<mapping resource="insurance.hbm.xml"/>
</session-factory>
</hibernate-configuration>


Top
 Profile  
 
 Post subject: Re: re:null pointer exception
PostPosted: Mon Mar 08, 2010 5:47 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Did you already try without specifying package names?

Code:
String sql_query = "select empdet.firstname,empdet.city,empmail.firstname,empmail.email FROM .EmpDetail empdet left outer join fetch Employee empmail where empdet.Empid = empmail.Empid";


I also would try without the fetch option.


Top
 Profile  
 
 Post subject: Re: re:null pointer exception
PostPosted: Mon Mar 08, 2010 5:48 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Code:
String sql_query = "select empdet.firstname,empdet.city,empmail.firstname,empmail.email FROM EmpDetail empdet left outer join fetch Employee empmail where empdet.Empid = empmail.Empid";


Top
 Profile  
 
 Post subject: Re: re:null pointer exception
PostPosted: Mon Mar 08, 2010 5:53 am 
Beginner
Beginner

Joined: Fri Feb 26, 2010 8:23 am
Posts: 29
i have try without specifying package name but it is throwing unexpected ast node so only i specify package name.

please help me.


Top
 Profile  
 
 Post subject: Re: re:null pointer exception
PostPosted: Mon Mar 08, 2010 6:04 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Oh, if I remember right then with this join syntax you must use the keyword ON instead to WHERE.
Try with

Code:
String sql_query = "select ... FROM EmpDetail empdet left outer join fetch Employee empmail ON empdet.Empid = empmail.Empid";


Top
 Profile  
 
 Post subject: Re: re:null pointer exception
PostPosted: Mon Mar 08, 2010 6:05 am 
Beginner
Beginner

Joined: Fri Feb 26, 2010 8:23 am
Posts: 29
i try ON instead of where but it is showing unexpected token ON so only i changed like that.

i am using hibernate 3 and mysql version 5.1.


Top
 Profile  
 
 Post subject: Re: re:null pointer exception
PostPosted: Mon Mar 08, 2010 6:18 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
It is not possible to use "join" unless you have mapped an association between the classes. In the mapping files you have provided I can't see any relation between the EmpDetail and Employee classes. It seems like the intention is that there is a one-to-one relation on the primary key between those two classes. If so, check the documentation at http://docs.jboss.org/hibernate/stable/ ... n-onetoone for some example of how to map the association. Then, you should be able to use "join" in the query.


Top
 Profile  
 
 Post subject: Re: re:null pointer exception
PostPosted: Wed Mar 10, 2010 7:21 am 
Beginner
Beginner

Joined: Fri Feb 26, 2010 8:23 am
Posts: 29
thanks alot for ur reply but i dont know which association can u suggest the association and tell where i can give it.


Top
 Profile  
 
 Post subject: Re: re:null pointer exception
PostPosted: Wed Mar 10, 2010 8:00 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
but i dont know which association


Only you can know what kind of relation is between the EmpDetail and Employee classes. There is no relation between those two classes in the mapping documents and there is no relation in the java code either, so maybe there is no connection. Only you can know. But if there is no relation then you can't join those two classes in a query either.

You should not mix HQL (which is what you use with Hibernate) and SQL. In SQL you can join on just everything, but in HQL only on what has been mapped in the mapping documents which need to match both the java code and the underlying database.

The link I provided in the previous post gives you several example of how to map an association between two classes.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.