I am yet another newbie struggling with an IllegalArgumentException on my getter method of property: id.
I know this problem has been asked about before and I have read the answers, but I'm still stuck.
Please bear with me and excuse any incorrect terminology.
My application is basically the hibernate 3.6.7 EventManager tutorial, project root ./project/hibernate-documentation/quickstart/tutorials/basic,
and, after getting this to work ok (using maven V3 to build), I am now trying to experiment with subclasses.
Specifically, I want to implement two subclasses inside same table (Table per class) in my Event class in Event.hbm.xml
using a discriminator column - Here is my new Event.hbm.xml :
Code:
<hibernate-mapping package="org.hibernate.tutorial.hbm">
<class name="Event" table="EVENTS">
<id name="id" type="long" column="EVENT_ID">
<generator class="sequence"/>
</id>
<discriminator type="string">
<column name="discriminator" />
</discriminator>
<property name="date" type="timestamp" column="EVENT_DATE"/>
<property name="title"/>
<property name="bindata" type="binary" update="true" insert="true" column="BINDATA"/>
[b] <!-- subclasses -->
<subclass name="greenleaf" discriminator-value="GRLF">
<property name="greenlong" type="long" column="leafvalue"/>
</subclass>
<subclass name="redleaf" discriminator-value="RDLF">
<property name="redlong" type="long" column="leafvalue"/>
</subclass>[/b]
</class>
</hibernate-mapping>
Here are my getters and setters in Event.java :
Code:
public class Event {
private long id;
private String title;
private Date date;
private byte[] bindata;
private String discriminator;
public Event() {
// this form used by Hibernate
}
public Event(String title, Date date, byte[] bindata , String discriminator) {
// for application use, to create new events
this.title = title;
this.date = date;
this.bindata = bindata;
this.discriminator = discriminator;
}
/***********************/
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
/************************/
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public byte[] getBindata() {
return bindata;
}
public void setBindata(byte[] bindata) {
this.bindata = bindata;
}
public String getDiscriminator() {
return discriminator;
}
public void setDiscriminator(String discriminator) {
this.discriminator = discriminator;
}
public Event getGreenleaf() {
return this;
}
public void setGreenleaf(Event greenleaf) {
this.title = greenleaf.title;
this.date = greenleaf.date;
this.bindata = greenleaf.bindata;
this.discriminator = new String("GRLF");
}
public Event getRedleaf() {
return this;
}
public void setRedleaf(Event redleaf) {
this.title = redleaf.title;
this.date = redleaf.date;
this.bindata = redleaf.bindata;
this.discriminator = new String("RDLF");
}
And here are relevant snippets from the EventManager.java and localUtil.java (which creates the session)
EventManager.java
Code:
private void createAndStoreEvent(String title, Date theDate, byte[] theData ,String theDiscriminator )/********** throws IOException ***********/ {
byte[] debug_bytes = new byte[40];
String debug_string;
/* public static String toHexString(byte[] bytes) */
char[] hexArray = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
char[] hexChars = new char[80];
int masked_byte;
int bytes_read;
int num_hexchars;
Long redlong;
Long greenlong;
long eventId = 8888;
Session session = localUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Event theEvent = new Event();
/* eventId = new Long(8888); */
/* theEvent.setId(eventId); */
redleaf theredleaf = new redleaf();
greenleaf thegreenleaf = new greenleaf();
redlong = new Long(12);
greenlong = new Long(7);
theEvent.setTitle(title);
theEvent.setDate(theDate);
theEvent.setBindata(theData);
theEvent.setDiscriminator(theDiscriminator);
theredleaf.setRedlong(redlong);
thegreenleaf.setGreenlong(greenlong);
session.save(theEvent);
and localUtil.java :
Code:
public class localUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory();
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
This all compiles ok but at run time I get :
Code:
509 [org.hibernate.tutorial.hbm.EventManager.main()] INFO org.hibernate.impl.SessionFactoryImpl - building session factory
516 [org.hibernate.tutorial.hbm.EventManager.main()] INFO org.hibernate.type.BasicTypeRegistry - Type registration [materialized_blob] overrides previous : org.hibernate.type.MaterializedBlobType@5e76f2e8
558 [org.hibernate.tutorial.hbm.EventManager.main()] ERROR org.hibernate.property.BasicPropertyAccessor - IllegalArgumentException in class: org.hibernate.tutorial.hbm.Event, getter method of property: id
Initial SessionFactory creation failed.org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of org.hibernate.tutorial.hbm.Event.id
[WARNING]
java.lang.reflect.InvocationTargetException
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:597)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.ExceptionInInitializerError
at org.hibernate.tutorial.hbm.localUtil.buildSessionFactory(localUtil.java:21)
at org.hibernate.tutorial.hbm.localUtil.<clinit>(localUtil.java:11)
at org.hibernate.tutorial.hbm.EventManager.createAndStoreEvent(EventManager.java:68)
at org.hibernate.tutorial.hbm.EventManager.main(EventManager.java:43)
... 6 more
Caused by: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of org.hibernate.tutorial.hbm.Event.id
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:198)
at org.hibernate.engine.UnsavedValueFactory.getUnsavedIdentifierValue(UnsavedValueFactory.java:67)
at org.hibernate.tuple.PropertyFactory.buildIdentifierProperty(PropertyFactory.java:67)
at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:135)
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:485)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:133)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:84)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:286)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1872)
at org.hibernate.tutorial.hbm.localUtil.buildSessionFactory(localUtil.java:16)
... 9 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:597)
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:172)
... 18 more
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] An exception occured while executing the Java class. null
I have tried using different types for the id attribute, I've also noticed from other postings
that in different contexts this kind of error is caused by omitting a suitable getter method for the collection,
but whatever I try it's always the same error, and I don't know how to find out what it's really complaining about.
Can anyone please give me a pointer as to what is wrong?
John Lumby