I have an Animal that visits many Locations.
So I have a one-to-many relationship.
The hitch is that the LocationTable's unique id is a combination of 3 fields.
I'm using the LocationPK class to model the composite id.
I have looked for a solution online and have tried many things, but to no avail.
I'm new to Hibernate and would very much appreciate some help with this problem.
Thanks,
David
Here is the basic setup:
Animal Table
animalID (primary key)
animalName
Locations Table
animalID
dateMovedIn
LocationId
dateMovedOut
Code:
public class Animal {
private int animalID;
private String animalName;
private Set<location> locations;
public Animal(){}
plus getter and setters
}
public class LocationPK implements Serializable {
private int animalID;
private int locationID;
private Date dateMovedIn;
public LocationPK(){}
plus getter and setters
}
public class Location {
private LocationPK locationPK;
private Date dateMovedOut;
private Animal animal;
public class Location(){}
plus getter and setters
}
Animal.hbm.xml
<hibernate-mapping>
<class name="Animal" table="AnimalTable">
<id name="animalID">
<generator class="native"/>
</id>
<property name="animalName"/>
<set name="locations" lazy="false" cascade="all" inverse="true">
<key column="animalID"/>
<one-to-many class="Location"/>
</set>
</hibernate-mapping>
Location.hbm.xml
<hibernate-mapping>
<class name="Location" table="LocationTable">
<composite-id name="locationPK" class="LocationPK">
<key-property name="animalID/>
<key-property name="locationID"/>
<key-property name="dateMovedIn" type="timestamp"/>
<key-many-to-one name="animalID" class="Animal"/>
</composite-id>
<property name="dateMovedOut" type="timestamp"/>
</class>
</hibernate-mapping>
I'm getting the following error:
Code:
Messages:
1. object is not an instance of declaring class
2. IllegalArgumentException occurred calling getter of Animal.animalID
File: org/hibernate/property/BasicPropertyAccessor.java
Line number: 195
With logging set to DEBUG I get this error:
Code:
Key 'location' was not found on instance of org.hibernate.PropertyAccessException etc.......
StacktracesCode:
org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of Animal.animalID
org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:195)
org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:206)
org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3619)
org.hibernate.type.EntityType.getHashCode(EntityType.java:310)
org.hibernate.type.ComponentType.getHashCode(ComponentType.java:212)
org.hibernate.engine.EntityKey.generateHashCode(EntityKey.java:126)
org.hibernate.engine.EntityKey.(EntityKey.java:70)
org.hibernate.engine.StatefulPersistenceContext.getDatabaseSnapshot(StatefulPersistenceContext.java:263)
org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:212)
org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:535)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:103)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:534)
org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:526)
org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:241)
org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:291)
org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:239)
org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:192)
org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:319)
org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:265)
org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:242)
org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:192)
org.hibernate.engine.Cascade.cascade(Cascade.java:153)
org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:479)
org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:357)
org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:562)
org.hibernate.impl.SessionImpl.save(SessionImpl.java:550)
org.hibernate.impl.SessionImpl.save(SessionImpl.java:546)
com.fair.struts2.persistence.animal.AnimalHibernateDao.insert(AnimalHibernateDao.java:53)
com.fair.struts2.service.file.AnimalDaoService.insert(AnimalDaoService.java:41)
com.fair.struts2.actions.test.AnimalTest.execute(AnimalTest.java:152)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:440)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:279)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:163)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:249)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:148)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:93)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:128)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:104)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:148)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:128)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:468)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
com.caucho.server.dispatch.FilterFilterChain.doFilter(Unknown Source)
org.apache.struts2.dispatcher.ActionContextCleanUp.doFilter(ActionContextCleanUp.java:102)
com.caucho.server.dispatch.FilterFilterChain.doFilter(Unknown Source)
com.caucho.server.webapp.WebAppFilterChain.doFilter(Unknown Source)
com.caucho.server.dispatch.ServletInvocation.service(Unknown Source)
com.caucho.server.hmux.HmuxRequest.handleRequest(Unknown Source)
com.caucho.server.port.TcpConnection.run(Unknown Source)
com.caucho.util.ThreadPool.runTasks(Unknown Source)
com.caucho.util.ThreadPool.run(Unknown Source)
java.lang.Thread.run(Thread.java:619)