-->
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.  [ 5 posts ] 
Author Message
 Post subject: display records from two tables using join statement
PostPosted: Wed May 03, 2006 9:52 am 
Newbie

Joined: Thu Apr 27, 2006 6:20 pm
Posts: 10
i have three java files....

first one for employee table(sql)...employee table has the three columns name,id and deptid...

this java program has getter and setter methods for the data fields
import java.io.Serializable;
import java.util.Set;
import java.util.Iterator;

public class Employee implements Serializable
{

private String NAME;

private int ID;

private int DEPTID;

public Employee() {}

public String getNAME() {
return NAME; }

public void setNAME(String NAME)
{
this.NAME = NAME;
}

public int getID() { return ID; }

public void setID(int ID)
{
this.ID = ID;
}

public int getDEPTID() { return DEPTID; }
public void setDEPTID(int DEPTID)
{
this.DEPTID = DEPTID;
}

public void show()
{
System.out.println("===================================");
System.out.println(" Employeeloye Information ");
System.out.println("===================================");
System.out.println("Employeeloyee Name ::>" + NAME);
System.out.println("Employeeloyee Won ::>" + DEPTID);
System.out.println("Employeeloyee Id ::>" + ID);
}
}


second one for department with departname and dept id

import java.io.Serializable;
import java.util.Set;
import java.util.Iterator;

public class Department implements Serializable
{

private String DEPTNAME;

private int DEPTID;


public Department() {}

public String getDEPTNAME() {
return DEPTNAME; }

public void setDEPTNAME(String DEPTNAME)
{
this.DEPTNAME = DEPTNAME;
}

public int getDEPTID() { return DEPTID; }
public void setDEPTID(int DEPTID)
{
this.DEPTID = DEPTID;
}

public void show()
{
System.out.println("===================================");
System.out.println(" dEPARTMENT Information ");
System.out.println("===================================");
System.out.println("dEPARTMENT Name ::>" + DEPTNAME);
System.out.println("dEPARTMENT Won ::>" + DEPTID);

}
}

the third is join java file

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.apache.log4j.*;
import org.hibernate.Query;

import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.HashSet;
import java.util.Set;


public class insert
{
public static void main(String[] args)
{
BasicConfigurator.configure();
Logger logger = Logger.getLogger(insert.class);
Session session = null;
Transaction tx = null;
SessionFactory sessionFactory = null;
try
{
Configuration configuration = new Configuration();
configuration.configure();
sessionFactory = configuration.buildSessionFactory();
session = sessionFactory.openSession();




String query = "from Employee e, Department d where e.DEPTID=d.DEPTID ";
Query qry = session.createQuery(query);

System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
System.out.println(" INFORMATION ");
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
Iterator i = qry.iterate();
Object []pair=(Object[])i.next();
Employee e=(Employee) pair[0];
Department d=(Department)pair[1];
e.show();
d.show();

}
catch (HibernateException e)
{
throw e;
}
finally
{
if (session != null)
{
try
{
session.close();
}
catch (HibernateException ignore)
{// ignore
}
}
if (sessionFactory != null)
{
try
{
sessionFactory.close();
}
catch (HibernateException e)
{// ignore
}
}
}
}
}


i cleary have records in the database which full fills the condition

"from Employee e, Department d where e.DEPTID=d.DEPTID"

but it i cannot c the records........
Pease help me with this at the earliest...
or give me a good link where in get details for joining tables....


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 03, 2006 9:55 am 
Newbie

Joined: Thu Apr 27, 2006 6:20 pm
Posts: 10
this is


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Employee" table="emp_test">
<id name="ID" type="int"/>
<property name="NAME" type="string"/>
<property name="DEPTID" type="int"/>
</class>
<class name="Department" table="dep_test">
<id name="DEPTID" type="int"/>
<property name="DEPTNAME" type="string"/>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 03, 2006 12:03 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Keep in mind that I took lot of effort in arranging the code within the [c o d e] tags Make it as a practice to use available tools v.z. code tags while presenting your problem.


Within you try-catch block, you are catching only HibernateException and ignoring every other exception.
Check to see if this code is also not working.

Code:
public class Insert {

   public static void main(String[] args)
   {
      BasicConfigurator.configure();
      Logger logger = Logger.getLogger(Insert.class);
      Session session = null;
      Transaction tx = null;
      SessionFactory sessionFactory = null;
      try
      {
         // have you specified hibernate_cfg.xml file
         // to use this option below
         // Configuration configuration = new Configuration();
         // configuration.configure();

         // if you havent specified any mapping file or class
         // you could add programmatically as follows
         Configuration configuration = new Configuration();
         configuration.addClass( Employee.class ); // assuming Employee.hbm.xml is available
         configuration.addClass( Department.class ); //// assuming Department.hbm.xml is available
         sessionFactory = configuration.buildSessionFactory();
         session = sessionFactory.openSession();

         String query = "from Employee e, Department d where e.DEPTID=d.DEPTID ";
         Query qry = session.createQuery(query);

         System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
         System.out.println(" INFORMATION ");
         System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
         Iterator i = qry.iterate();
         Object []pair=(Object[])i.next();
         Employee e=(Employee) pair[0];
         Department d=(Department)pair[1];
         e.show();
         d.show();

      }
      //catch (HibernateException e)
      catch( Exception e )
      {
         throw e;
      }
      finally
      {
         if (session != null)
         {
            try
            {
               session.close();
            }
            catch (HibernateException ignore)
            {// ignore
            }
         }
         
         if (sessionFactory != null)
         {
            try
            {
               sessionFactory.close();
            }
            catch (HibernateException e)
            {// ignore
            }
         }
      }
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 03, 2006 2:08 pm 
Newbie

Joined: Thu Apr 27, 2006 6:20 pm
Posts: 10
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 03, 2006 2:23 pm 
Newbie

Joined: Thu Apr 27, 2006 6:20 pm
Posts: 10
Thanks


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