Hibernate version: 3.1
CONTENT:
1. THE PROBLEM AND QUESTION
2. SOURCE CODE FOR .java FILES
2.1 The Persistent classes:
2.1.1 MainReport.java implements Serializable
2.1.2 MainReportId.java implements Serializable, equals(), hashcode()
2.1.3 Project.java implements Serializable
2.2 The DAO files (I call them Manager files)
2.2.1 MainReportManager.java
2.2.2 ProjectManager.java
2.3 The Driver class that runs all this
3. THE SOURCE CODE FOR THE MAPPING hbm.xml FILES
3.1 mainreport.hbm.xml
3.2 project.hbm.xml
4. THE ERROR MESSAGE
1. THE PROBLEM AND QUESTION
problem:
1. I have two simple database tables "report" and "project", one report may belong to one project,
and one project may have many reports. Thus, project and report have many-to-one associations.
2. When I run the main.java file without storing any information it goes well and creates the tables without any problems (the associations too).
When I run the main.java and stores information on the project table, it all goes well again, and the informaiton is stored.
When I run the main.java after I have stored information on the project table, and I'm trying to store information on the report table (by doing so I want to get the primary key from project table which is "NO-DEF" to be the foreign key in the report table), it all crashes with the error message i've written in section 4.
question:
My mapping files seems to be correct, I don't have any "wrong" getter or setter methods in my persistent objects (the java classes), the storeInformation() methods in my Manager classes works.
I REALLY don't know what I'm doing wrong.
Can anyone please help me with this, is my mappings wrong? If not, what may be wrong?
The error message says:
IllegalArgumentException occurred calling getter of reportsystem.Project.project_id
What?!? I do have getter and setters in Project.java, and they're public.
I've been trying to solve this for hours now, anyone PLEASE help !
2. SOURCE CODE FOR .java FILES
2.1.1 MainReport.java implements Serializable
Code:
package reportsystem;
import java.util.*;
import java.io.*;
import reportsystem.*;
public class MainReport implements Serializable {
// The first variable of the primary key
private String projectid;
// The second variable of the primary key
private Long reportnr;
// The last date of revision
private Date last_date;
// Other string variables that's necessary for the table report
private String clientid;
private String dept;
private String type_of_report;
private String summary;
private String groupe;
private String title_of_report;
private String index_term_1;
private String index_term_2;
private String index_term_3;
private String index_term_4;
private String report_type;
//other integer variables that's necessary for the table report
private int idreport;
private int distribution_statement;
private int rev_nr;
/* Empty constructor so that Hibernate may understand that this is
an POJO (Plain Old Java Object), to further us as an persistant class. */
public MainReport() {}
//Gets an MainReportId object to store the id of a mainreport
public void setReportid(MainReportId repid_) {
String repid_id = repid_.getProjectid();
Long repid_nr = repid_.getReportnr();
setProjectid(repid_id);
setReportnr(repid_nr);
}
public String getProjectid() {
return projectid;
}
public void setProjectid(String projectid) {
this.projectid = projectid;
}
public Long getReportnr() {
return reportnr;
}
public void setReportnr(Long reportnr) {
this.reportnr = reportnr;
}
public int getIdreport() {
return idreport;
}
public void setIdreport(int idreport) {
this.idreport = idreport;
}
public String getClientid() {
return clientid;
}
public void setClientid(String clientid) {
this.clientid = clientid;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public String getType_of_report() {
return type_of_report;
}
public void setType_of_report(String type_of_report) {
this.type_of_report = type_of_report;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getGroupe() {
return groupe;
}
public void setGroupe(String groupe) {
this.groupe = groupe;
}
public String getTitle_of_report() {
return title_of_report;
}
public void setTitle_of_report(String title_of_report) {
this.title_of_report = title_of_report;
}
public String getIndex_term_1() {
return index_term_1;
}
public void setIndex_term_1(String index_term_1) {
this.index_term_1 = index_term_1;
}
public String getIndex_term_2() {
return index_term_2;
}
public void setIndex_term_2(String index_term_2) {
this.index_term_2 = index_term_2;
}
public String getIndex_term_3() {
return index_term_3;
}
public void setIndex_term_3(String index_term_3) {
this.index_term_3 = index_term_3;
}
public String getIndex_term_4() {
return index_term_4;
}
public void setIndex_term_4(String index_term_4) {
this.index_term_4 = index_term_4;
}
public int getDistribution_statement() {
return distribution_statement;
}
public void setDistribution_statement(int distribution_statement) {
this.distribution_statement = distribution_statement;
}
public Date getLast_date() {
return last_date;
}
public void setLast_date(Date last_date) {
this.last_date = last_date;
}
public int getRev_nr() {
return rev_nr;
}
public void setRev_nr(int rev_nr) {
this.rev_nr = rev_nr;
}
public String getReport_type() {
return report_type;
}
public void setReport_type(String report_type) {
this.report_type = report_type;
}
}
2.1.2 MainReportId.java implements Serializable, equals(), hashcode().
This is the composite-id classCode:
package reportsystem;
import java.io.*;
public class MainReportId implements Serializable {
private Long reportnr;
private String projectid;
public MainReportId(){}
public MainReportId(Long reportnr, String projectid) {
this.reportnr = reportnr;
this.projectid = projectid;
}
public Long getReportnr() {
return reportnr;
}
public void setReportnr(Long reportnr) {
this.reportnr = reportnr;
}
public String getProjectid() {
return projectid;
}
public void setProjectid(String projectid) {
this.projectid = projectid;
}
public boolean equals(Object o) {
if (this == o) return true;
if(o == null) return false;
if(!(o instanceof MainReportId)) return false;
final MainReportId mainreportid = (MainReportId) o;
if (!projectid.equals(mainreportid.getProjectid()))
return false;
if(!reportnr.equals(mainreportid.getReportnr()))
return false;
return true;
}
public int hashCode() {
return reportnr.hashCode();
}
}
2.1.3 Project.java implements SerializableCode:
package reportsystem;
import java.io.*;
public class Project implements Serializable {
// The key value for a project
private String project_id;
private String cust_info;
public Project() {}
public String getProject_id() {
return project_id;
}
public void setProject_id(String project_id) {
this.project_id = project_id;
}
public String getCust_info() {
return cust_info;
}
public void setCust_info(String cust_info) {
this.cust_info = cust_info;
}
}
2.2 The DAO files (I call them Manager files)
2.2.1 MainReportManager.javaCode:
package reportsystem;
import util.*;
import exeptions.*;
import java.util.*;
import org.hibernate.*;
import org.hibernate.exception.*;
import org.hibernate.criterion.*;
public class MainReportManager {
public MainReportManager() {
HibernateUtil.beginTransaction();
}
public void storeInformation(Long nr, String projectid,
Date d, String clientid, String dept,
String type_of_report,
String summary, String groupe,
String title_of_report,
String index_term1, String index_term2,
String index_term3,
String index_term4, String report_type,
int idreport,
int distribution_statement, int rev_nr) {
try {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
MainReportId reportid = new MainReportId(nr,projectid);
MainReport mainRep = new MainReport();
mainRep.setReportid(reportid);
mainRep.setLast_date(d);
mainRep.setClientid(clientid);
mainRep.setDept(dept);
mainRep.setType_of_report(type_of_report);
mainRep.setSummary(summary);
mainRep.setGroupe(groupe);
mainRep.setTitle_of_report(title_of_report);
mainRep.setIndex_term_1(index_term1);
mainRep.setIndex_term_2(index_term2);
mainRep.setIndex_term_3(index_term3);
mainRep.setIndex_term_4(index_term4);
mainRep.setReport_type(report_type);
mainRep.setIdreport(idreport);
mainRep.setDistribution_statement(distribution_statement);
mainRep.setRev_nr(rev_nr);
session.save(mainRep);
tx.commit();
session.flush();
} catch (HibernateException ex) {
throw new InfrastructureException(ex);
}
}
}// end class
2.2.2 ProjectManager.javaCode:
package reportsystem;
import java.io.*;
import util.*;
import exeptions.*;
import org.hibernate.*;
import org.hibernate.exception.*;
public class ProjectManager {
public ProjectManager() {
HibernateUtil.beginTransaction();
}
public void storeInformation(String projectid, String custinfo) {
try {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
Project project = new Project();
project.setProject_id(projectid);
project.setCust_info(custinfo);
session.save(project);
tx.commit();
session.flush();
} catch (HibernateException ex) {
throw new InfrastructureException(ex);
}
}// end storeInformation()
}
2.3 The Driver class that runs all thisCode:
package util;
import java.util.*;
import reportsystem.*;
public class Driver {
static ProjectManager pm = new ProjectManager();
static MainReportManager rm = new MainReportManager();
public Driver() {}
public static void main(String args[]) {
Date date = new Date();
//pm.storeInformation("NO-DEF","Statoil"); //This works without any problems
//------------------- This is where it all crashes -------------------------
rm.storeInformation(new Long(1),"NO-DEF",date,"clientid","dept","type of r",
"This is the summary","tha groupe","title of rep",
"term1","term2","term3","term4","Tha type",
2,3,7);
}
}
3. THE SOURCE CODE FOR THE MAPPING hbm.xml FILES
3.1 mainreport.hbm.xmlCode:
<?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="reportsystem.MainReport" table="report">
<composite-id class="reportsystem.MainReportId" mapped="true" >
<key-property name="reportnr" column="reportnr" type="long"/>
<key-many-to-one name="projectid" class="reportsystem.Project"
column="projectid" />
</composite-id>
<property name="last_date" type="date" >
<column name="last_date" />
</property>
<property name="clientid" >
<column name="clientid" />
</property>
<property name="dept" >
<column name="dept" />
</property>
<property name="type_of_report" >
<column name="type_of_report" />
</property>
<property name="summary" type="text" >
<column name="summary" />
</property>
<property name="groupe" >
<column name="groupe" />
</property>
<property name="title_of_report" type="text" >
<column name="title_of_report" />
</property>
<property name="index_term_1" >
<column name="index_term_1" />
</property>
<property name="index_term_2" >
<column name="index_term_2" />
</property>
<property name="index_term_3" >
<column name="index_term_3" />
</property>
<property name="index_term_4" >
<column name="index_term_4" />
</property>
<property name="report_type" >
<column name="report_type" />
</property>
<property name="idreport" type="java.lang.Integer" >
<column name="idreport" />
</property>
<property name="distribution_statement" type="java.lang.Integer" >
<column name="distribution_statement" />
</property>
<property name="rev_nr" type="java.lang.Integer" >
<column name="rev_nr" />
</property>
</class>
</hibernate-mapping>
3.2 project.hbm.xmlCode:
<?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="reportsystem.Project" table="project">
<id name="project_id" column="project_id" type="string">
</id>
<property name="cust_info" type="text">
<column name="cust_info" />
</property>
</class>
</hibernate-mapping>
4. THE ERROR MESSAGECode:
exeptions.InfrastructureException: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of reportsystem.Project.project_id
at reportsystem.MainReportManager.storeInformation(MainReportManager.java:70)
at util.Driver.main(Driver.java:40)
Caused by: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of reportsystem.Project.project_id
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:171)
at org.hibernate.tuple.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:176)6500 [main] ERROR org.hibernate.property.BasicPropertyAccessor - IllegalArgumentException in class: reportsystem.Project, getter method of property: project_id
at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3257)
at org.hibernate.type.EntityType.getHashCode(EntityType.java:400)
at org.hibernate.type.ComponentType.getHashCode(ComponentType.java:162)
at org.hibernate.engine.EntityKey.getHashCode(EntityKey.java:69)
at org.hibernate.engine.EntityKey.<init>(EntityKey.java:42)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:147)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:114)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:559)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:547)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:543)
at reportsystem.MainReportManager.storeInformation(MainReportManager.java:65)
... 1 more
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:145)
... 18 more
Exception in thread "main"