I am trying to display values from two simple tables with no primary keys and no foriegn keys but there is a common column.
Table EMPLOYEE - Columns ->ID,NAME,DEPTID
Table DEPARTMENT - Columns ->DEPTID,DEPTNAME
I tried like this:
Department.hbm.xml:
********
<?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="dep_test">
<id name="deptid" type="int"/>
<property name="deptname" type="string"/>
</class>
</hibernate-mapping>
********
Employee.hbm.xml:
*********
<?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"/>
<one-to-one name="department" class="Department" />
</class>
</hibernate-mapping>
*******************
Employee.java
public class Employee implements Serializable
{
private String name;
private int deptid;
private int id;
public Employee() {}
public String getName() { return name; }
public void setName(String Name)
{
this.name = Name;
}
public int getDeptid() { return deptid; }
public void setDeptid(int Deptid)
{
this.deptid = Deptid;
}
public int getId() { return id; }
public void setId(int ID)
{
this.id = ID;
}
}
Deaprtment.java
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 getDetid() { return deptid; }
public void setDeptid(int Deptid)
{
this.deptid = Deptid;
}
}
join.java
public class join
{
public static void main(String[] args)
{
BasicConfigurator.configure();
Logger logger = Logger.getLogger(insert.class);
Session session = null;
Transaction tx = null;
SessionFactory sessionFactory = null;
Configuration configuration = new Configuration();
configuration.configure();
sessionFactory = configuration.buildSessionFactory();
session = sessionFactory.openSession();
String query = "from Employee as emp left join emp.department as dept where emp.deptid=dept.deptid";
Query qry = session.createQuery(query);
Iterator prjItr = qry.iterate();
Employee currEmp = (Employee) prjItr.next();
System.out.println("++++++++++++"+currEmp.getDeptid());
System.out.println("+++++++++++"+currEmp.getId());
}}
got error as
"
BUILD FAILED
C:\working\build.xml:28: org.hibernate.PropertyNotFoundException: Could not find
a getter for department in class Employee"
Please help me in this
|