-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: many-to-many collection issues with H3.1
PostPosted: Mon Aug 15, 2005 4:34 pm 
Newbie

Joined: Sun Aug 14, 2005 12:25 am
Posts: 5
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.


Top
 Profile  
 
 Post subject: id?
PostPosted: Mon Aug 15, 2005 8:04 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
According to your mapping property deptId has type Dept, so co de should look like this:
User user = (User) session.load(User.class, 5);
Dept d = (Dept) session.load(Dept.class, 49);
UserDept ud = new UserDept();
ud.setDeptId( d );
user.getUserDepts().add(ud);
session.save(user);


and of course I recommend renaming #setDeptId() to #setDept();

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 15, 2005 9:26 pm 
Newbie

Joined: Sun Aug 14, 2005 12:25 am
Posts: 5
I thank you very much for your response! Thank you for taking the time to look over my post and analyze my problem. You were right on the money. I hope this helps someone else in the future.


Thanks again.


Top
 Profile  
 
 Post subject: Similar mapping different problem
PostPosted: Tue Oct 25, 2005 2:50 pm 
Beginner
Beginner

Joined: Sun Oct 09, 2005 3:21 pm
Posts: 40
Wish the original author had shown the Java POJO representing the relational table between User/Dept.

Get a similar mapping (set instead of bag) but the setter for the property made by the parent element is never found when I run very similar code because the setter is never generated by HibernateToolTask. And of course I get a PropertyNotFoundException thrown at runtime.

Maybe I am missing a greater concept in Hibernate. But how would a set/bag relationship ever work if the parent (Class with the set element) is represented in the relational table and but not the corresponding POJO class?

Thanks / matthew


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.