-->
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.  [ 10 posts ] 
Author Message
 Post subject: Unable to Persist Associated Objects
PostPosted: Fri Jan 26, 2007 3:52 pm 
Beginner
Beginner

Joined: Wed Oct 25, 2006 12:10 pm
Posts: 41
I am having problems persisting associated objects to database
Here is the situation

- I have 2 associated objects viz. DeviceType and Role. That means DeviceType is associated with Role .

- Relationship is Uni-directional Many-to-Many

- Here are the POJOs
Code:
package test;

import java.util.Set;

public class DeviceType {
private Long id;
private String deviceType;
private String description;
private Set roles;

public DeviceType(){

}

public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getDeviceType() {
return deviceType;
}
public void setDeviceType(String deviceType) {
this.deviceType = deviceType;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}

public Set getRoles() {
return roles;
}

public void setRoles(Set roles) {
this.roles = roles;
}


}



Code:
package test;

public class Role {
private Long id;
private String name;

public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}


}


- Here is the mapping file
Code:
<?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="test.DeviceType" table="DEVICE_TYPE">
<id name="id" column="ID" unsaved-value="0" >
<generator class="increment"/>
</id>

<property name="deviceType">
<column name="NAME" />
</property>

<property name="description">
<column name="DESC" />
</property>

<set name="roles" table="DEVICETYPE_ROLES" inverse="true" cascade="all">
<key column="DEVICETYPE_ID" />
<many-to-many column="ROLE_ID" class="test.Role"/>
</set>

</class>

<class name="test.Role" table="ROLE">
<id name="id" column="ID">
<generator class="assigned">
</generator>
</id>

<property name="name" column="NAME" />

</class>



</hibernate-mapping>



- Persistence Snippet
Code:
public void persist(DeviceType dt) {
logger.info(" *** In persist method *** ");
System.out.println(" *** In persist method *** ");
// System.out.println("***ID="+dt.getId()+"***DeviceType="+dt.getDeviceType()+"***Desc="+dt.getDescription());
// hibernateTemplate.save(dt);
System.out.println("***ID="+dt.getId()+"***DeviceType="+dt.getDeviceType()+"***Desc="+dt.getDescription()+"***Role Size="+dt.getRoles().size()); // Works fine
hibernateTemplate.save(dt);  // Throws Exception shown below

}


- Stack Trace
Code:
[org.hibernate.property.BasicPropertyAccessor] - <IllegalArgumentException in class: test.Role, getter method of property: id>
<IllegalArgumentException occurred calling getter of test.Role.id; nested exception is org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of test.Role.id>




- An Example on how to persist associated objects in the persist method of XXXDAOHibernateImpl Class will be highly appreciated


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 21, 2007 9:06 pm 
Regular
Regular

Joined: Sun Sep 17, 2006 2:48 am
Posts: 81
Location: California
Is your role objects already saved in DB by the time you are trying to save deviceType object? Can you also print their ids in a loop before you call the save?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 21, 2007 11:39 pm 
Newbie

Joined: Wed Feb 07, 2007 8:26 pm
Posts: 17
try to omit inverse="true" in "DeviceType" class


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 22, 2007 12:18 am 
Beginner
Beginner

Joined: Wed Oct 25, 2006 12:10 pm
Posts: 41
Somu & BISO
Thank you so much for your responses

As suggested by BISO i omitted inverse=true in the mapping file devicetype.hbm.xml file but the same error still persists


Somu as you correctly guessed Role objects are already saved in the DB by the time i am trying to save Device Type object.
Yes i am able to print the ids before calling the save method.

Here is log trace generated using log4j when i click Save button

[code]
Register method called
DeviceRoles size3
In deviceRoles 10
In deviceRoles 20
In deviceRoles 30
roleSet size=3
2007-02-21 20:06:06,187 INFO [com.boeing.nmt.nams.service.impl.DeviceTypeManagerImpl] - < *** In createDeviceType Manager: save method *** >
2007-02-21 20:06:06,187 INFO [com.boeing.nmt.nams.dao.hibernate.DeviceTypeDaoHibernateImpl] - < *** In persist method *** >
*** In persist method ***
***ID=null***Name=ok***Desc=ok***Role Size=3
Hibernate: select max(ID) from DEVICE_TYPE
2007-02-21 20:06:06,250 ERROR [color=red][org.hibernate.property.BasicPropertyAccessor] - <IllegalArgumentException in class: com.boeing.nmt.nams.model.Role, getter method of property: id>
2007-02-21 20:06:06,250 WARN [com.boeing.nmt.nams.view.bean.DeviceTypeBean] - <IllegalArgumentException occurred calling getter of com.boeing.nmt.nams.model.Role.id; nested exception is org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.boeing.nmt.nams.model.Role.id>[/color]2007-02-21 20:06:06,265 INFO [com.boeing.nmt.nams.view.bean.DeviceTypeBean] - < *** In getDeviceTypes Backing Bean*** >
2007-02-21 20:06:06,265 INFO [com.boeing.nmt.nams.service.impl.DeviceTypeManagerImpl] - < *** In getDeviceTypeList Spring*** >
Hibernate: select devicetype0_.ID as col_0_0_, devicetype0_.NAME as col_1_0_ from DEVICE_TYPE devicetype0_
2007-02-21 20:06:06,265 INFO [com.boeing.nmt.nams.view.bean.DeviceTypeBean] - < *** In getSelectedDevice method*** >
2007-02-21 20:06:06,265 WARN [org.apache.myfaces.shared_impl.renderkit.html.HtmlGridRendererBase] - <PanelGrid deviceTypeForm:_id17 has not enough children. Child count should be a multiple of the columns attribute.>


[/code]

The situation is very similar to Many-to-Many Uni-directional relationship between User and Role Objects.
The expected functionality is to persist the data ( i.e. userID, roleID) into the link/intermediate table user_roles when i click the Save button. I am getting the error when i say hibernateTemplate.save(User) i.e. in my case hibernateTemplate.persist(deviceType)

Any pointers/suggestions will be highly appreciated

Regards
Bansi


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 22, 2007 12:29 am 
Beginner
Beginner

Joined: Wed Oct 25, 2006 12:10 pm
Posts: 41
Somu
A little correction to my earlier posting i.e. Role object is not saved as part of the DeviceType object whereas data for Role object already exsist in the database. What i am doing is creating a new DeviceType object which is associated with Roles.

Users create new DeviceType object and select Roles for that device type object on JSF form and then click Save button.

The expected functionality is
1) Insert device type object into device type table
insert into DeviceType(Id, Name, Desc) values(?,?,?)
2) Insert deviceID, roleID into link/intermediate table DeviceType_Roles
insert into DeviceType_Roles(DeviceType_Id,Role_Id) values(?,?)



Regards
Bansi


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 22, 2007 1:42 am 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi bansi,


Try this one

<class name="test.Role" table="ROLE">

<id name="id" column="ID" type="long" >
<generator class="sequence">
<param name="sequence">ID_SEQ</param>
</generator>
</id>



<property name="name" column="NAME" />

</class>

where ID is sequence

or not sure but try type ="long" only will help you in ID

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 22, 2007 10:02 am 
Beginner
Beginner

Joined: Fri Aug 12, 2005 7:05 am
Posts: 25
Location: TamilNadu
There are two ways to persist associated objects.
1. Create bi-direction rleationship in mapping document and then do like below,

role1 // value object
role1.setDeviceType(deviceTypeid);
role2 //value object
role2.setDeviceType(deviceTypeid);
Set rolesSet = new HashSet();
rolesSet.add(role1);
rolesSet.add(role2);
deviceType.setRoles(rolesSet);
save(deviceType);

2. seperatly call session.save(role1) method
but this seem to be static code.

Regards
Shanmugam.N


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 22, 2007 9:09 pm 
Beginner
Beginner

Joined: Wed Oct 25, 2006 12:10 pm
Posts: 41
Hi Shanmugam

As suggested by you i created
bi-direction rleationship in mapping document as shown below

[code]
<class name="com.boeing.nmt.nams.model.Role" table="ROLE">
<id name="id" column="ID" type="long">
<generator class="native">
</generator>
</id>

<property name="name" column="NAME" type="string"/>

<set name="deviceTypes" table="DEVICETYPE_ROLES" >
<key column="ROLE_ID"/>
<many-to-many column="DEVICETYPE_ID" class="com.boeing.nmt.nams.model.DeviceType" />
</set>

</class>
[/code]

I exactly do as suggested by you in my JSF Backing bean i.e.
Set rolesSet = new HashSet();
rolesSet.add(role1);
rolesSet.add(role2);
deviceType.setRoles(rolesSet);
Here is my code

[code]
public void createDeviceType() {
//System.out.println("*** In Backin Bean Save method: deviceType= ***");
try{
roleSet = getRoleList();
System.out.println("roleSet size="+roleSet.size()); // prints successfully
deviceType.setRoles(roleSet);
deviceTypeManager.createDeviceType(deviceType);

}catch(Exception e) {
logger.warn(e.getMessage());
}



}

public Set getRoleList()
{


for (Iterator iter = deviceRoles.iterator(); iter.hasNext();) {
System.out.println("Inside for loop");
SelectItem selectItem = (SelectItem) iter.next();
roleList.add(selectItem.getValue());
}
return new HashSet(roleList);
}
[/code]

If i comment the line deviceType.setRoles(rolesSet); then the error goes away and as you know it will not result in inserting the records either in the DeviceType table or DevicetYpe_roles table
So do you think error is at the line
[color=red]deviceType.setRoles(roleSet);[/color] or somewhere in hibernate mapping files

Any pointers/suggestions will be highly appreciated

Regards
Bansi


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 22, 2007 9:34 pm 
Beginner
Beginner

Joined: Wed Oct 25, 2006 12:10 pm
Posts: 41
I tried playing around with the mapping file and switched the cascade parameter onto Role side without inverse parameter on either side as shown below

Code:

<set name="deviceTypes" table="DEVICETYPE_ROLES" [b]cascade="save-update,persist"> [/b]            <key column="ROLE_ID"/>
            <many-to-many column="DEVICETYPE_ID" class="com.boeing.nmt.nams.model.DeviceType" />
        </set>


The Result is i am happy to see two insert statements in the log file but it shows different error this time. Here is the log trace

Code:

*** In persist method ***
***ID=null***Name=yeh***Desc=yeh***Role Size=1
Hibernate: select max(ID) from DEVICE_TYPE
Hibernate: insert into DEVICE_TYPE (NAME, DESCRIPTION, ID) values (?, ?, ?)
Hibernate: insert into DEVICETYPE_ROLES (DEVICETYPE_ID, ROLE_ID) values (?, ?)
2007-02-22 17:14:30,245 ERROR [org.hibernate.property.BasicPropertyAccessor] - <IllegalArgumentException in class: com.boeing.nmt.nams.model.Role, getter method of property: id>
2007-02-22 17:14:30,245 ERROR [org.hibernate.event.def.AbstractFlushingEventListener] - <Could not synchronize database state with session>
org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.boeing.nmt.nams.model.Role.id
   at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:171)
   at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:183)
   at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3539)
   at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:3255)
   at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:181)
   at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:218)
   at org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
   at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:78)
   at org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:755)
   at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1143)
   at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:26)
   at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:143)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
   at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:394)
   at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:367)
   at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:614)
   at com.boeing.nmt.nams.dao.hibernate.DeviceTypeDaoHibernateImpl.persist(DeviceTypeDaoHibernateImpl.java:36)
   at com.boeing.nmt.nams.service.impl.DeviceTypeManagerImpl.createDeviceType(DeviceTypeManagerImpl.java:33)
   at com.boeing.nmt.nams.view.bean.DeviceTypeBean.createDeviceType(DeviceTypeBean.java:226)
   at com.boeing.nmt.nams.view.bean.DeviceTypeBean.saveOrUpdate(DeviceTypeBean.java:154)
   at com.boeing.nmt.nams.view.bean.DeviceTypeBean.register(DeviceTypeBean.java:114)
   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 com.sun.el.parser.AstValue.invoke(AstValue.java:157)
   at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:283)
   at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
   at com.sun.facelets.el.LegacyMethodBinding.invoke(LegacyMethodBinding.java:69)
   at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:63)
   at javax.faces.component.UICommand.broadcast(UICommand.java:106)
   at org.ajax4jsf.framework.ajax.AjaxViewRoot.processEvents(AjaxViewRoot.java:274)
   at org.ajax4jsf.framework.ajax.AjaxViewRoot.broadcastEvents(AjaxViewRoot.java:250)
   at org.ajax4jsf.framework.ajax.AjaxViewRoot.processApplication(AjaxViewRoot.java:405)
   at org.apache.myfaces.lifecycle.LifecycleImpl.invokeApplication(LifecycleImpl.java:343)
   at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:86)
   at javax.faces.webapp.FacesServlet.service(FacesServlet.java:137)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:144)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at org.ajax4jsf.framework.ajax.xmlfilter.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:75)
   at org.ajax4jsf.framework.ajax.xmlfilter.BaseFilter.doFilter(BaseFilter.java:213)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at com.boeing.nmt.nams.view.util.MessageFilter.doFilter(MessageFilter.java:32)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
   at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
   at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
   at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
   at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
   at java.lang.Thread.run(Thread.java:595)
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)
   ... 64 more
2007-02-22 17:14:30,260 WARN [com.boeing.nmt.nams.view.bean.DeviceTypeBean] - <IllegalArgumentException occurred calling getter of com.boeing.nmt.nams.model.Role.id; nested exception is org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.boeing.nmt.nams.model.Role.id>
2007-02-22 17:14:30,276 INFO [com.boeing.nmt.nams.view.bean.DeviceTypeBean] - < *** In getDeviceTypes Backing Bean*** >
2007-02-22 17:14:30,276 INFO [com.boeing.nmt.nams.service.impl.DeviceTypeManagerImpl] - < *** In getDeviceTypeList Spring*** >
Hibernate: select devicetype0_.ID as col_0_0_, devicetype0_.NAME as col_1_0_ from DEVICE_TYPE devicetype0_
2007-02-22 17:14:30,323 INFO [com.boeing.nmt.nams.view.bean.DeviceTypeBean] - < *** In getSelectedDevice method*** >
2007-02-22 17:14:30,323 WARN [org.apache.myfaces.shared_impl.renderkit.html.HtmlGridRendererBase] - <PanelGrid deviceTypeForm:_id17 has not enough children. Child count should be a multiple of the columns attribute.>



I am not sure what does the error means
[ code]

Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 20, 2007 11:20 pm 
Beginner
Beginner

Joined: Fri May 21, 2004 5:22 am
Posts: 24
hi there,

has this problem being solved? i had the same problem here


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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.