-->
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: Problem with cascading relationships with delete-orphan
PostPosted: Mon Oct 31, 2005 10:10 am 
Newbie

Joined: Mon Oct 31, 2005 9:49 am
Posts: 2
Hey all,

Background: I am using Hibernate in a web site I am building. Just as forewarning, I am also using Spring to manage the session, etc, etc. Because it is a website and because I have been urged not to include Spring or Hibernate specific details within the presentation logic, I have opted not to use lazy-loading. Because it is generally not desirable to load entire databases into memory, I have also removed most of the relationships between objects.

[Problem:[/b] I have a relationship table between two objects, a User and a Project, that have a many-to-many relationship. The Project and User mappings have a set of relationships they can see. These relationships only hold the primary keys of the Project and User objects. The relationship mapping has no reference to the User or Project objects. Like this: User --> Relationships <-- Project.

My problem arises when I try to update the set of relationships in a User or Project object and then commit. I would expect that if a relationship is removed from the set, Hibernate would remove it from the database. This is not happening. Instead I get an exception that Hibernate cannot set a not-null column in the relationship table to null. It sounds like instead of deleting the relationship, Hibernate is simply trying to set the foreign key attribute to null. :(

I have cascading enabled (all,delete-orphan) but it does not seem to be working. Help please!

Hibernate version: 3.0

Mapping documents:
Project mapping:
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 default-lazy="false">
<!--
        Auto-generated mapping file from
        the hibernate.org cfg2hbm engine
-->
    <class name="com.project.model.hibernate.HibProject" table="PROJECT">
        <id name="projId" type="integer">
            <column name="PROJ_ID" />
            <generator class="increment" />
        </id>
        <property name="projNm" type="string">
            <column name="PROJ_NM" length="10" not-null="true" />
        </property>
        <property name="projLdEmail" type="string">
            <column name="PROJ_LD_EMAIL" length="40" not-null="true" />
        </property>
        <property name="projLdNm" type="string">
            <column name="PROJ_LD_NM" length="40" not-null="true" />
        </property>
        <set name="userProjRels" inverse="true" cascade="all,delete-orphan">
            <key>
                <column name="PROJ_ID" not-null="true" />
            </key>
            <one-to-many class="com.project.model.hibernate.HibUserProjRel" />
        </set>
        <set name="projRsrcRels" inverse="true" cascade="all,delete-orphan">
            <key>
                <column name="PROJ_ID" not-null="true" />
            </key>
            <one-to-many class="com.project.model.hibernate.HibProjRsrcRel" />
        </set>
    </class>
</hibernate-mapping>


User mapping:
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 default-lazy="false">
<!--
        Auto-generated mapping file from
        the hibernate.org cfg2hbm engine
-->
    <class name="com.project.model.hibernate.HibUser" table="USER">
        <id name="userId" type="integer">
            <column name="USER_ID" />
            <generator class="increment" />
        </id>
        <property name="userEmail" type="string">
            <column name="USER_EMAIL" length="40" not-null="true" />
        </property>
        <property name="userPass" type="string">
            <column name="USER_PASS" length="15" not-null="true" />
        </property>
        <property name="secLvl" type="character">
            <column name="SEC_LVL" length="1" not-null="true" />
        </property>
        <property name="defProj" type="integer">
            <column name="DEF_PROJ" />
        </property>
        <set name="userProjRels" cascade="all-delete-orphan">
            <key>
                <column name="USER_ID" length="10" not-null="true" />
            </key>
            <one-to-many class="com.project.model.hibernate.HibUserProjRel" />
        </set>
    </class>
</hibernate-mapping>


Relationship mapping:
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 default-lazy="false">
<!--
        Auto-generated mapping file from
        the hibernate.org cfg2hbm engine
-->
    <class name="com.project.model.hibernate.HibUserProjRel" table="USER_PROJ_REL" schema="DB2ADMIN">
        <composite-id name="id" class="com.project.model.hibernate.HibUserProjRelId">
            <key-property name="userId" type="integer">
                <column name="USER_ID" length="10" />
            </key-property>
            <key-property name="projId" type="integer">
                <column name="PROJ_ID" />
            </key-property>
        </composite-id>
        <property name="userId" type="integer" update="false" insert="false">
            <column name="USER_ID" length="10" not-null="true" />
        </property>
        <property name="projId" type="integer" update="false" insert="false">
            <column name="PROJ_ID" not-null="true" />
        </property>
    </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
public void updateProject(HibProject project) {
      getHibernateTemplate().update(project);
   }

Full stack trace of any exception that occurs:
Code:
TRAS0014I: The following exception was logged org.hibernate.exception.ConstraintViolationException: could not delete collection rows: [com.project.model.hibernate.HibUser.userProjRels#14]
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:63)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.persister.collection.AbstractCollectionPersister.deleteRows(AbstractCollectionPersister.java:953)
   at org.hibernate.action.CollectionUpdateAction.execute(CollectionUpdateAction.java:46)
   at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:669)
   at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:394)
   at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:359)
   at org.springframework.orm.hibernate3.HibernateTemplate.merge(HibernateTemplate.java:722)
   at com.project.model.dao.hibernate.HibernateUserDAOImpl.updateUser(HibernateUserDAOImpl.java:134)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java(Compiled Code))
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java(Compiled Code))
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java(Compiled Code))
   at java.lang.reflect.Method.invoke(Method.java(Compiled Code))
   at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:292)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:163)
   at $Proxy37.updateUser(Unknown Source)
   at com.project.control.ejb.ResourceCatalogBean.editUser(ResourceCatalogBean.java:108)
   at com.project.control.ejb.EJSLocalStatelessResourceCatalogBean_630f4036.editUser(EJSLocalStatelessResourceCatalogBean_630f4036.java:1003)
   at com.project.control.ResourceCatalogDelegate.updateUser(ResourceCatalogDelegate.java:71)
   at com.project.ui.struts.actions.CompleteEditUserAction.execute(CompleteEditUserAction.java:125)
   at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
   at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
   at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
   at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
   at com.ibm.ws.webcontainer.servlet.StrictServletInstance.doService(StrictServletInstance.java:110)
   at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._service(StrictLifecycleServlet.java:174)
   at com.ibm.ws.webcontainer.servlet.IdleServletState.service(StrictLifecycleServlet.java:313)
   at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.service(StrictLifecycleServlet.java:116)
   at com.ibm.ws.webcontainer.servlet.ServletInstance.service(ServletInstance.java:283)
   at com.ibm.ws.webcontainer.servlet.ValidServletReferenceState.dispatch(ValidServletReferenceState.java:42)
   at com.ibm.ws.webcontainer.servlet.ServletInstanceReference.dispatch(ServletInstanceReference.java:40)
   at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java:1019)
   at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java:592)
   at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:204)
   at com.ibm.ws.webcontainer.srt.WebAppInvoker.doForward(WebAppInvoker.java:125)
   at com.ibm.ws.webcontainer.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java:286)
   at com.ibm.ws.webcontainer.cache.invocation.CachedInvocation.handleInvocation(CachedInvocation.java:71)
   at com.ibm.ws.webcontainer.srp.ServletRequestProcessor.dispatchByURI(ServletRequestProcessor.java:182)
   at com.ibm.ws.webcontainer.oselistener.OSEListenerDispatcher.service(OSEListener.java:334)
   at com.ibm.ws.webcontainer.http.HttpConnection.handleRequest(HttpConnection.java:56)
   at com.ibm.ws.http.HttpConnection.readAndHandleRequest(HttpConnection.java(Compiled Code))
   at com.ibm.ws.http.HttpConnection.run(HttpConnection.java(Compiled Code))
   at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:912)
Caused by: COM.ibm.db2.jdbc.DB2Exception: [IBM][CLI Driver][DB2/NT] SQL0407N  Assignment of a NULL value to a NOT NULL column "USER_ID" is not allowed.  SQLSTATE=23502

   at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw_SQLException(SQLExceptionGenerator.java:267)
   at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw_SQLException(SQLExceptionGenerator.java:209)
   at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.check_return_code(SQLExceptionGenerator.java:455)
   at COM.ibm.db2.jdbc.app.DB2PreparedStatement.execute2(DB2PreparedStatement.java:2096)
   at COM.ibm.db2.jdbc.app.DB2PreparedStatement.executeUpdate(DB2PreparedStatement.java:1642)
   at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
   at org.hibernate.persister.collection.AbstractCollectionPersister.deleteRows(AbstractCollectionPersister.java:936)
   ... 48 more
.
                                 org.hibernate.exception.ConstraintViolationException: could not delete collection rows: [com.project.model.hibernate.HibUser.userProjRels#14]
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:63)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.persister.collection.AbstractCollectionPersister.deleteRows(AbstractCollectionPersister.java:953)
   at org.hibernate.action.CollectionUpdateAction.execute(CollectionUpdateAction.java:46)
   at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:669)
   at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:394)
   at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:359)
   at org.springframework.orm.hibernate3.HibernateTemplate.merge(HibernateTemplate.java:722)
   at com.project.model.dao.hibernate.HibernateUserDAOImpl.updateUser(HibernateUserDAOImpl.java:134)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java(Compiled Code))
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java(Compiled Code))
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java(Compiled Code))
   at java.lang.reflect.Method.invoke(Method.java(Compiled Code))
   at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:292)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:163)
   at $Proxy37.updateUser(Unknown Source)
   at com.project.control.ejb.ResourceCatalogBean.editUser(ResourceCatalogBean.java:108)
   at com.project.control.ejb.EJSLocalStatelessResourceCatalogBean_630f4036.editUser(EJSLocalStatelessResourceCatalogBean_630f4036.java:1003)
   at com.project.control.ResourceCatalogDelegate.updateUser(ResourceCatalogDelegate.java:71)
   at com.project.ui.struts.actions.CompleteEditUserAction.execute(CompleteEditUserAction.java:125)
   at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
   at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
   at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
   at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
   at com.ibm.ws.webcontainer.servlet.StrictServletInstance.doService(StrictServletInstance.java:110)
   at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._service(StrictLifecycleServlet.java:174)
   at com.ibm.ws.webcontainer.servlet.IdleServletState.service(StrictLifecycleServlet.java:313)
   at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.service(StrictLifecycleServlet.java:116)
   at com.ibm.ws.webcontainer.servlet.ServletInstance.service(ServletInstance.java:283)
   at com.ibm.ws.webcontainer.servlet.ValidServletReferenceState.dispatch(ValidServletReferenceState.java:42)
   at com.ibm.ws.webcontainer.servlet.ServletInstanceReference.dispatch(ServletInstanceReference.java:40)
   at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.handleWebAppDispatch(WebAppRequestDispatcher.java:1019)
   at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.dispatch(WebAppRequestDispatcher.java:592)
   at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:204)
   at com.ibm.ws.webcontainer.srt.WebAppInvoker.doForward(WebAppInvoker.java:125)
   at com.ibm.ws.webcontainer.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java:286)
   at com.ibm.ws.webcontainer.cache.invocation.CachedInvocation.handleInvocation(CachedInvocation.java:71)
   at com.ibm.ws.webcontainer.srp.ServletRequestProcessor.dispatchByURI(ServletRequestProcessor.java:182)
   at com.ibm.ws.webcontainer.oselistener.OSEListenerDispatcher.service(OSEListener.java:334)
   at com.ibm.ws.webcontainer.http.HttpConnection.handleRequest(HttpConnection.java:56)
   at com.ibm.ws.http.HttpConnection.readAndHandleRequest(HttpConnection.java(Compiled Code))
   at com.ibm.ws.http.HttpConnection.run(HttpConnection.java(Compiled Code))
   at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:912)
Caused by: COM.ibm.db2.jdbc.DB2Exception: [IBM][CLI Driver][DB2/NT] SQL0407N  Assignment of a NULL value to a NOT NULL column "USER_ID" is not allowed.  SQLSTATE=23502

   at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw_SQLException(SQLExceptionGenerator.java:267)
   at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw_SQLException(SQLExceptionGenerator.java:209)
   at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.check_return_code(SQLExceptionGenerator.java:455)
   at COM.ibm.db2.jdbc.app.DB2PreparedStatement.execute2(DB2PreparedStatement.java:2096)
   at COM.ibm.db2.jdbc.app.DB2PreparedStatement.executeUpdate(DB2PreparedStatement.java:1642)
   at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
   at org.hibernate.persister.collection.AbstractCollectionPersister.deleteRows(AbstractCollectionPersister.java:936)
   ... 48 more

Name and version of the database you are using: IBM DB2 v7
Log output (SQL and Hibernate):
Code:
[10/31/05 8:44:14:111 EST] 790af71f XmlBeanDefini I org.springframework.beans.factory.xml.XmlBeanDefinitionReader  Loading XML bean definitions from class path resource [applicationContext.xml]
[10/31/05 8:44:14:127 EST] 790af71f ClassPathXmlA I org.springframework.context.support.ClassPathXmlApplicationContext  Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=1016395582]: org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [RCDataSource,RCSessionFactory,RCTransactionManager,clientDAO,projectDAO,serviceInfoDAO,transmissionProtocolDAO,userDAO,resourceDAO,keywordDAO,clientBeanTarget,projectBeanTarget,serviceInfoBeanTarget,transProtocolBeanTarget,userBeanTarget,resourceBeanTarget,keywordBeanTarget]; root of BeanFactory hierarchy
[10/31/05 8:44:14:127 EST] 790af71f ClassPathXmlA I org.springframework.context.support.ClassPathXmlApplicationContext  17 beans defined in application context [org.springframework.context.support.ClassPathXmlApplicationContext;hashCode=1016395582]
[10/31/05 8:44:14:127 EST] 790af71f ClassPathXmlA I org.springframework.context.support.ClassPathXmlApplicationContext  Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@76503739]
[10/31/05 8:44:14:127 EST] 790af71f ClassPathXmlA I org.springframework.context.support.ClassPathXmlApplicationContext  Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@75d7f739]
[10/31/05 8:44:14:127 EST] 790af71f DefaultListab I org.springframework.beans.factory.support.DefaultListableBeanFactory  Pre-instantiating singletons in factory [org.springframework.beans.factory.support.DefaultListableBeanFactory defining beans [RCDataSource,RCSessionFactory,RCTransactionManager,clientDAO,projectDAO,serviceInfoDAO,transmissionProtocolDAO,userDAO,resourceDAO,keywordDAO,clientBeanTarget,projectBeanTarget,serviceInfoBeanTarget,transProtocolBeanTarget,userBeanTarget,resourceBeanTarget,keywordBeanTarget]; root of BeanFactory hierarchy]
[10/31/05 8:44:14:127 EST] 790af71f DriverManager I org.springframework.jdbc.datasource.DriverManagerDataSource  Loaded JDBC driver: COM.ibm.db2.jdbc.app.DB2Driver
[10/31/05 8:44:14:158 EST] 790af71f HbmBinder     I org.hibernate.cfg.HbmBinder  Mapping class: com.project.model.hibernate.HibClient -> CLIENT
[10/31/05 8:44:14:158 EST] 790af71f HbmBinder     I org.hibernate.cfg.HbmBinder  Mapping class: com.project.model.hibernate.HibKeyword -> KEYWORD
[10/31/05 8:44:14:174 EST] 790af71f HbmBinder     I org.hibernate.cfg.HbmBinder  Mapping class: com.project.model.hibernate.HibKywrdRsrcRel -> KYWRD_RSRC_REL
[10/31/05 8:44:14:174 EST] 790af71f HbmBinder     I org.hibernate.cfg.HbmBinder  Mapping class: com.project.model.hibernate.HibProject -> PROJECT
[10/31/05 8:44:14:189 EST] 790af71f HbmBinder     I org.hibernate.cfg.HbmBinder  Mapping class: com.project.model.hibernate.HibProjRsrcRel -> PROJ_RSRC_REL
[10/31/05 8:44:14:189 EST] 790af71f HbmBinder     I org.hibernate.cfg.HbmBinder  Mapping class: com.project.model.hibernate.HibResource -> RESOURCE
[10/31/05 8:44:14:205 EST] 790af71f HbmBinder     I org.hibernate.cfg.HbmBinder  Mapping class: com.project.model.hibernate.HibServiceInfo -> SERVICE_INFO
[10/31/05 8:44:14:205 EST] 790af71f HbmBinder     I org.hibernate.cfg.HbmBinder  Mapping class: com.project.model.hibernate.HibTransPrtclCs -> TRANS_PRTCL_CS
[10/31/05 8:44:14:221 EST] 790af71f HbmBinder     I org.hibernate.cfg.HbmBinder  Mapping class: com.project.model.hibernate.HibTransPrtclSrvcRel -> TRANS_PRTCL_SRVC_REL
[10/31/05 8:44:14:221 EST] 790af71f HbmBinder     I org.hibernate.cfg.HbmBinder  Mapping class: com.project.model.hibernate.HibUser -> USER
[10/31/05 8:44:14:236 EST] 790af71f HbmBinder     I org.hibernate.cfg.HbmBinder  Mapping class: com.project.model.hibernate.HibUserProjRel -> USER_PROJ_REL
[10/31/05 8:44:14:236 EST] 790af71f LocalSessionF I org.springframework.orm.hibernate3.LocalSessionFactoryBean  Building new Hibernate SessionFactory
[10/31/05 8:44:14:236 EST] 790af71f Configuration I org.hibernate.cfg.Configuration  processing extends queue
[10/31/05 8:44:14:236 EST] 790af71f Configuration I org.hibernate.cfg.Configuration  processing collection mappings
[10/31/05 8:44:14:236 EST] 790af71f HbmBinder     I org.hibernate.cfg.HbmBinder  Mapping collection: com.project.model.hibernate.HibKeyword.kywrdRsrcRels -> KYWRD_RSRC_REL
[10/31/05 8:44:14:236 EST] 790af71f HbmBinder     I org.hibernate.cfg.HbmBinder  Mapping collection: com.project.model.hibernate.HibProject.userProjRels -> USER_PROJ_REL
[10/31/05 8:44:14:236 EST] 790af71f HbmBinder     I org.hibernate.cfg.HbmBinder  Mapping collection: com.project.model.hibernate.HibProject.projRsrcRels -> PROJ_RSRC_REL
[10/31/05 8:44:14:236 EST] 790af71f HbmBinder     I org.hibernate.cfg.HbmBinder  Mapping collection: com.project.model.hibernate.HibResource.projRsrcRels -> PROJ_RSRC_REL
[10/31/05 8:44:14:236 EST] 790af71f HbmBinder     I org.hibernate.cfg.HbmBinder  Mapping collection: com.project.model.hibernate.HibResource.kywrdRsrcRels -> KYWRD_RSRC_REL
[10/31/05 8:44:14:236 EST] 790af71f HbmBinder     I org.hibernate.cfg.HbmBinder  Mapping collection: com.project.model.hibernate.HibServiceInfo.transPrtclSrvcRels -> TRANS_PRTCL_SRVC_REL
[10/31/05 8:44:14:236 EST] 790af71f HbmBinder     I org.hibernate.cfg.HbmBinder  Mapping collection: com.project.model.hibernate.HibTransPrtclCs.transPrtclSrvcRels -> TRANS_PRTCL_SRVC_REL
[10/31/05 8:44:14:236 EST] 790af71f HbmBinder     I org.hibernate.cfg.HbmBinder  Mapping collection: com.project.model.hibernate.HibUser.userProjRels -> USER_PROJ_REL
[10/31/05 8:44:14:236 EST] 790af71f Configuration I org.hibernate.cfg.Configuration  processing association property references
[10/31/05 8:44:14:236 EST] 790af71f Configuration I org.hibernate.cfg.Configuration  processing foreign key constraints
[10/31/05 8:44:14:236 EST] 790af71f Dialect       I org.hibernate.dialect.Dialect  Using dialect: org.hibernate.dialect.DB2Dialect
[10/31/05 8:44:14:236 EST] 790af71f SettingsFacto I org.hibernate.cfg.SettingsFactory  Default batch fetch size: 1
[10/31/05 8:44:14:236 EST] 790af71f SettingsFacto I org.hibernate.cfg.SettingsFactory  Generate SQL with comments: disabled
[10/31/05 8:44:14:236 EST] 790af71f SettingsFacto I org.hibernate.cfg.SettingsFactory  Order SQL updates by primary key: disabled
[10/31/05 8:44:14:236 EST] 790af71f SettingsFacto I org.hibernate.cfg.SettingsFactory  Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
[10/31/05 8:44:14:236 EST] 790af71f ASTQueryTrans I org.hibernate.hql.ast.ASTQueryTranslatorFactory  Using ASTQueryTranslatorFactory
[10/31/05 8:44:14:236 EST] 790af71f SettingsFacto I org.hibernate.cfg.SettingsFactory  Query language substitutions: {}
[10/31/05 8:44:14:252 EST] 790af71f ConnectionPro I org.hibernate.connection.ConnectionProviderFactory  Initializing connection provider: org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider
[10/31/05 8:44:14:314 EST] 790af71f SettingsFacto I org.hibernate.cfg.SettingsFactory  Scrollable result sets: enabled
[10/31/05 8:44:14:314 EST] 790af71f SettingsFacto I org.hibernate.cfg.SettingsFactory  JDBC3 getGeneratedKeys(): disabled
[10/31/05 8:44:14:314 EST] 790af71f TransactionFa I org.hibernate.transaction.TransactionFactoryFactory  Using default transaction strategy (direct JDBC transactions)
[10/31/05 8:44:14:314 EST] 790af71f TransactionMa I org.hibernate.transaction.TransactionManagerLookupFactory  No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
[10/31/05 8:44:14:314 EST] 790af71f SettingsFacto I org.hibernate.cfg.SettingsFactory  Automatic flush during beforeCompletion(): disabled
[10/31/05 8:44:14:314 EST] 790af71f SettingsFacto I org.hibernate.cfg.SettingsFactory  Automatic session close at end of transaction: disabled
[10/31/05 8:44:14:314 EST] 790af71f SettingsFacto I org.hibernate.cfg.SettingsFactory  Cache provider: org.hibernate.cache.EhCacheProvider
[10/31/05 8:44:14:314 EST] 790af71f SettingsFacto I org.hibernate.cfg.SettingsFactory  Second-level cache: enabled
[10/31/05 8:44:14:314 EST] 790af71f SettingsFacto I org.hibernate.cfg.SettingsFactory  Optimize cache for minimal puts: disabled
[10/31/05 8:44:14:314 EST] 790af71f SettingsFacto I org.hibernate.cfg.SettingsFactory  Structured second-level cache entries: enabled
[10/31/05 8:44:14:314 EST] 790af71f SettingsFacto I org.hibernate.cfg.SettingsFactory  Query cache: disabled
[10/31/05 8:44:14:314 EST] 790af71f SettingsFacto I org.hibernate.cfg.SettingsFactory  Echoing all SQL to stdout
[10/31/05 8:44:14:314 EST] 790af71f SettingsFacto I org.hibernate.cfg.SettingsFactory  Statistics: disabled
[10/31/05 8:44:14:314 EST] 790af71f SettingsFacto I org.hibernate.cfg.SettingsFactory  Deleted entity synthetic identifier rollback: disabled
[10/31/05 8:44:14:314 EST] 790af71f SettingsFacto I org.hibernate.cfg.SettingsFactory  Default entity-mode: pojo
[10/31/05 8:44:14:314 EST] 790af71f SessionFactor I org.hibernate.impl.SessionFactoryImpl  building session factory
[10/31/05 8:44:14:346 EST] 790af71f SessionFactor I org.hibernate.impl.SessionFactoryObjectFactory  Not binding factory to JNDI, no JNDI name configured
[10/31/05 8:44:14:346 EST] 790af71f SessionFactor I org.hibernate.impl.SessionFactoryImpl  Checking 0 named queries
[10/31/05 8:44:14:361 EST] 790af71f HibernateTran I org.springframework.orm.hibernate3.HibernateTransactionManager  Using DataSource [org.springframework.jdbc.datasource.DriverManagerDataSource@73b67739] of Hibernate SessionFactory for HibernateTransactionManager
[10/31/05 8:44:14:439 EST] 790af71f SystemOut     O Hibernate: select hibuser0_.USER_ID as USER1_0_, hibuser0_.USER_EMAIL as USER2_75_0_, hibuser0_.USER_PASS as USER3_75_0_, hibuser0_.SEC_LVL as SEC4_75_0_, hibuser0_.DEF_PROJ as DEF5_75_0_ from DB2ADMIN.USER hibuser0_ where hibuser0_.USER_ID=?
[10/31/05 8:44:14:455 EST] 790af71f SystemOut     O Hibernate: select userprojre0_.USER_ID as USER1___, userprojre0_.PROJ_ID as PROJ2___, userprojre0_.USER_ID as USER1_0_, userprojre0_.PROJ_ID as PROJ2_0_, userprojre0_.USER_ID as USER1_76_0_, userprojre0_.PROJ_ID as PROJ2_76_0_ from DB2ADMIN.USER_PROJ_REL userprojre0_ where userprojre0_.USER_ID=?
[10/31/05 8:44:14:517 EST] 790af71f SystemOut     O Hibernate: update DB2ADMIN.USER_PROJ_REL set USER_ID=null where USER_ID=? and PROJ_ID=?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 01, 2005 4:32 am 
Regular
Regular

Joined: Thu Oct 27, 2005 8:06 am
Posts: 55
Location: München, Germany
There are quite a few things that puzzle me:

1. You declared the one-to-many relationship from HibProject to HibUserProjRel as inverse. There is no many-to-one relationship matching this inverse relationship, as you didn't declare your USER_ID inside HibUserProjRel inside a <many-to-one> mapping. As far as I know, this isn't correct in Hibernate, and I'm surprised that Hibernate waits so long to complain. I don't know your intentions of making that userId update=false and insert=false instead -- but this certainly isn't the standard way of doing things, so I'd just use a plain vanilla many-to-one instead.

2. Talking about intentions: Probably, the reason for your trouble is your worry that declaring all of your relationships as part of your mapping might get the entire database loaded into memory. That's definitely not the case. Just declare default-lazy="true" in all your Hibernate mapping files, and no eager fetching will take place at all. You can then enable eager fetching for those associations where you need it by declaring lazy="false".

3. Not the reason for your present problem, but still noteworthy: Eager or lazy loading has nothing to do with exposing Spring or Hibernate details within the presentation logic. You specify eager or lazy loading either in Hibernate configuration files, as sketched in the previous point, or directly in your HQL query. This latter mechanism is quite powerful, as it allows you to choose in which situations to do eager loading (namely in those where you positively know that you need the associated objects, and want to take the performance boost eager loading gives you). HQL queries should be in the Data Access layer, far below any presentation tier.

Marginal note: It's therefore easy and even highly recommendable to leave all Hibernate details out of your presentation logic. If you use Spring for what it's made for, however, both your presentation layer (views) and controllers will be full of Spring details.

-------

After having settled those present problems, you might still meet some errors when you try to delete objects. I just had a multi-day-debugging experience in that area. Therefore:

4. Remember that you are responsible for setting the bidirectional links between objects. While Hibernate will transparently set them when retrieving objects from the database, it's your task to set them in both directions when creating your associations, and to remove them (all of them!) when you want to delete an object. The latter task is specifically error-prone. While it is easy to check which references a given object holds, it's much more difficult to know by which other objects a given object is referenced. Only coding discipline helps here.

5. Take the Hibernate authors' advice about implementing correct equals and hashCode operations seriously when designing classes that are referenced via Set collections. If you don't, in particular if you just omit defining these operations, everything will appear to run well for some time. Suddenly, in the life cycle of such an object, those sets will start containing the same logical element twice without any warning, after which any program logic isn't worth much any more.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 01, 2005 10:58 am 
Newbie

Joined: Mon Oct 31, 2005 9:49 am
Posts: 2
To cover your points (and thanks for the input)
1. The inverse attribute is a hold-over from the generated code before I modified it to kill all the relationships. Thanks for pointing it out.
2. The company I work for is just getting into Hibernate and being that they primarily develop web apps, have decided that using Hibernate mappings for relationships are bad, due to dislike of lazy-loading. As experience in Hibernate grows, that opinion may change. (I kinda like your idea but...)
3. If you use Spring with lazy loading (as it was meant to be used), my understanding was you needed some sort of interceptor or servlet filter wrapped around your presentation layer to keep the session active. Putting those there would be "Putting DA stuff in the presentation layer". Plus we are only using Spring to aid in the data access stuff for now so it's not in the presentation layer at all. It stops at our Business Delegate session bean.

4. See the bottom

5. Already ran into that problem a couple of days ago. :)

I believe Hibernate can see the relationships between the classes. It is trying to update the userId or projectId columns in the relationship when they are removed from the containing Set. I was just wondering if there was a way to get Hibernate to delete the entire relationship object instead of setting a not-null column to null.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 01, 2005 11:54 am 
Regular
Regular

Joined: Thu Oct 27, 2005 8:06 am
Posts: 55
Location: München, Germany
With the restrictions your company imposes on you, you are not to be envied. So a technical argument won't be much help for you.

Therefore, let's talk economy. They let you go special ways that nearly no one else goes:

- Using Hibernate, but not for mapping relationships,

- using Spring, but not for MVC, where it excels.

Even if, technically, this was the most brilliant idea since the discovery of the electron (which I doubt), it will cost your company money. All advice given in books, all example applications will be useless for you. Experienced developers who want to give you advice won't be able to fall back on their concrete experience.

It's no shame if your bosses are wary of a mechanism like lazy loading because they don't understand it yet. The fruitful way to go from there would be to allow understanding it, which is best effected by starting to use it, and solve the problems that occur on the way. Solutions within the intended scope of Hibernate and Spring are much easier to find than solutions that you have to invent yourself. Developing web apps is a reason for using these frameworks the way they are designed, not a reason against.

To get back to the technical stuff: I have no idea how far Hibernate can guess about relationships when they aren't declared. Okay, it sees the object variables in the classes, so maybe it interprets their values. But as this isn't part of the documented functionality, I wouldn't rely on this in a production system.

And the measures to allow lazy loading in a web view (I guess you mean the open session in view mechanism) are arranged in a purely declarative way and aren't visible in any of the views or controllers.


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.