I'm trying to create a collection using an existing link table. It is similar to the one used in HIA page 230.
I have three tables
tbl_user
lnk_user_dept
tbl_dept
The relationship between user and dept is many-to-many
My User and Dept POJO work fine until I add the relationship and attempt to use it.
Below is the definition of the relationship in User.hbm.xml
<bag name="userDepts"
table="lnk_user_dept"
cascade="save-update">
<key column="col_user_id"/>
<composite-element class="edu.cmu.ua.dbo.UserDept">
<parent name="userId"/>
<many-to-one name="deptId" class="edu.cmu.ua.dbo.Dept" column="col_dept_id"/>
<property name="primary_relationship" type="boolean" column="col_primary_relationship" not-null="true"/>
</composite-element>
</bag>
I use schemaexport to verify the generated table structure to the tables I have to work with. Schema export correctly exports the structure of the table I'm trying to use in the relationship.
[schemaexport] create table lnk_user_dept (
[schemaexport] col_user_id int4 not null,
[schemaexport] col_dept_id int4,
[schemaexport] col_primary_relationship bool not null
[schemaexport] );
[schemaexport] alter table lnk_user_dept
[schemaexport] add constraint FKE4A8F0A36D54E2DF
[schemaexport] foreign key (col_dept_id)
[schemaexport] references public.tbl_dept;
[schemaexport] alter table lnk_user_dept
[schemaexport] add constraint FKE4A8F0A37EEAD1F
[schemaexport] foreign key (col_user_id)
[schemaexport] references public.tbl_user;
The reason I used a bag instead of a set as they did in the HIA example was because the third colum of lnk_user_dept.col_primary_relationship is not part of the composite key and therefore can be null.
When I attempt to establish a relationship using the below code I receive and error.
Usage:
User user = (User) session.load(User.class, 5);
Dept d = (Dept) session.load(Dept.class, 49);
UserDept ud = new UserDept();
ud.setDeptId(d.getId());
user.getUserDepts().add(ud);
session.save(user);
Error:
org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of edu.cmu.ua.dbo.Dept.Id
Stack Trace:
Aug 15, 2005 4:31:09 PM org.hibernate.property.BasicPropertyAccessor$BasicGetter get
SEVERE: IllegalArgumentException in class: edu.cmu.ua.dbo.Dept, getter method of property: Id
Aug 15, 2005 4:31:09 PM org.hibernate.event.def.AbstractFlushingEventListener performExecutions
SEVERE: Could not synchronize database state with session
org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of edu.cmu.ua.dbo.Dept.Id
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:171)
at org.hibernate.tuple.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:176)
at org.hibernate.persister.entity.BasicEntityPersister.getIdentifier(BasicEntityPersister.java:3044)
at org.hibernate.persister.entity.BasicEntityPersister.isTransient(BasicEntityPersister.java:2794)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:181)
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:215)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:107)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:72)
at org.hibernate.type.ComponentType.nullSafeSet(ComponentType.java:211)
at org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:664)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:937)
at org.hibernate.action.CollectionUpdateAction.execute(CollectionUpdateAction.java:51)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:243)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:227)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:143)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:284)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:748)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:329)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at edu.cmu.ua.dbo.HibernateUtil.commitTransaction(Unknown Source)
at edu.cmu.ua.servlets.HelloWorld.doGet(Unknown Source)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:596)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:427)
at org.mortbay.jetty.servlet.WebApplicationHandler$CachedChain.doFilter(WebApplicationHandler.java:832)
at org.mortbay.jetty.servlet.jsr77.Jsr77Filter.doFilter(Jsr77Filter.java:77)
at org.mortbay.jetty.servlet.WebApplicationHandler$CachedChain.doFilter(WebApplicationHandler.java:823)
at org.mortbay.jetty.servlet.WebApplicationHandler.dispatch(WebApplicationHandler.java:473)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:567)
at org.mortbay.http.HttpContext.handle(HttpContext.java:1565)
at org.mortbay.jetty.servlet.WebApplicationContext.handle(WebApplicationContext.java:635)
at org.mortbay.http.HttpContext.handle(HttpContext.java:1517)
at org.mortbay.jetty.plus.PlusWebAppContext.handle(PlusWebAppContext.java:158)
at org.mortbay.http.HttpServer.service(HttpServer.java:954)
at org.mortbay.http.HttpConnection.service(HttpConnection.java:814)
at org.mortbay.http.HttpConnection.handleNext(HttpConnection.java:981)
at org.mortbay.http.HttpConnection.handle(HttpConnection.java:831)
at org.mortbay.http.SocketListener.handleConnection(SocketListener.java:244)
at org.mortbay.util.ThreadedServer.handle(ThreadedServer.java:357)
at org.mortbay.util.ThreadPool$PoolThread.run(ThreadPool.java:534)
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:585)
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:145)
... 40 more
Aug 15, 2005 4:31:09 PM org.mortbay.jetty.servlet.ServletHandler handle
WARNING: /servlets/HelloWorld:
edu.cmu.ua.exceptions.InfrastructureException: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of edu.cmu.ua.dbo.Dept.Id
at edu.cmu.ua.dbo.HibernateUtil.commitTransaction(Unknown Source)
at edu.cmu.ua.servlets.HelloWorld.doGet(Unknown Source)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:596)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:427)
at org.mortbay.jetty.servlet.WebApplicationHandler$CachedChain.doFilter(WebApplicationHandler.java:832)
at org.mortbay.jetty.servlet.jsr77.Jsr77Filter.doFilter(Jsr77Filter.java:77)
at org.mortbay.jetty.servlet.WebApplicationHandler$CachedChain.doFilter(WebApplicationHandler.java:823)
at org.mortbay.jetty.servlet.WebApplicationHandler.dispatch(WebApplicationHandler.java:473)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:567)
at org.mortbay.http.HttpContext.handle(HttpContext.java:1565)
at org.mortbay.jetty.servlet.WebApplicationContext.handle(WebApplicationContext.java:635)
at org.mortbay.http.HttpContext.handle(HttpContext.java:1517)
at org.mortbay.jetty.plus.PlusWebAppContext.handle(PlusWebAppContext.java:158)
at org.mortbay.http.HttpServer.service(HttpServer.java:954)
at org.mortbay.http.HttpConnection.service(HttpConnection.java:814)
at org.mortbay.http.HttpConnection.handleNext(HttpConnection.java:981)
at org.mortbay.http.HttpConnection.handle(HttpConnection.java:831)
at org.mortbay.http.SocketListener.handleConnection(SocketListener.java:244)
at org.mortbay.util.ThreadedServer.handle(ThreadedServer.java:357)
at org.mortbay.util.ThreadPool$PoolThread.run(ThreadPool.java:534)
Caused by: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of edu.cmu.ua.dbo.Dept.Id
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:171)
at org.hibernate.tuple.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:176)
at org.hibernate.persister.entity.BasicEntityPersister.getIdentifier(BasicEntityPersister.java:3044)
at org.hibernate.persister.entity.BasicEntityPersister.isTransient(BasicEntityPersister.java:2794)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:181)
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:215)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:107)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:72)
at org.hibernate.type.ComponentType.nullSafeSet(ComponentType.java:211)
at org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:664)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:937)
at org.hibernate.action.CollectionUpdateAction.execute(CollectionUpdateAction.java:51)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:243)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:227)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:143)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:284)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:748)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:329)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
... 21 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:585)
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:145)
Hibernate version:
3.1
Name and version of the database you are using:
PostgreSQL 7.4
I'd appreciate any help or suggestions.
Thank you.
|