-->
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.  [ 12 posts ] 
Author Message
 Post subject: Saving a Many to Many relation
PostPosted: Mon Mar 09, 2009 11:36 am 
Newbie

Joined: Mon Mar 09, 2009 11:21 am
Posts: 11
My Problem is as follows. I have a many to many assosiation between environments and servers. It all seems to be fine, I get no error if I want to save some data (see Java Code below).
But my relation table (ecm_server_environment_data) contains no Data! Server and Environment Entities are saved but not the relation between them. Can you help?

Hibernate version:
3.3.2.GA
Mapping documents:
EcmEnvironmentData
Code:
<hibernate-mapping>
    <class name="com.basfits.insite.core.environmentData.EcmEnvironmentData" table="ecm_environment_data" catalog="ecm">
        <id name="persistentId" type="java.lang.Long">
            <column name="environment_id" />
            <generator class="identity" />
        </id>
        <many-to-one name="ecmRuntimeEnvironment" class="com.basfits.insite.core.environmentRuntime.EcmRuntimeEnvironment" fetch="select">
            <column name="runtime_environment_Id" />
        </many-to-one>
        <property name="name" type="java.lang.String">
            <column name="name" length="50" />
        </property>
        <property name="context" type="java.lang.String">
            <column name="context" length="250" />
        </property>
        <property name="version" type="java.lang.Float">
            <column name="version" precision="12" scale="0" />
        </property>
        <bag name="ecmServers" table="ecm_server_environment_data" cascade="all">
            <key column="environment_data_id" />
            <many-to-many class="com.basfits.insite.core.server.EcmServer" column="server_id" />
        </bag>
        <set name="ecmHistories" inverse="true">
            <key>
                <column name="environment_data_id" not-null="true" />
            </key>
            <one-to-many class="com.basfits.insite.core.history.EcmHistory" />
        </set>
        <bag name="ecmClusters" inverse="true">
            <key>
                <column name="environment_data_id" />
            </key>
            <one-to-many class="com.basfits.insite.core.cluster.EcmCluster" />
        </bag>
    </class>
</hibernate-mapping>


EcmServer
Code:
<hibernate-mapping>
    <class name="com.basfits.insite.core.server.EcmServer" table="ecm_server" catalog="ecm">
        <id name="persistentId" type="java.lang.Long">
            <column name="server_id" />
            <generator class="identity" />
        </id>
        <many-to-one name="ecmCluster" class="com.basfits.insite.core.cluster.EcmCluster" fetch="select">
            <column name="cluster_id" />
        </many-to-one>
        <property name="name" type="java.lang.String">
            <column name="name" length="50" not-null="true" />
        </property>
        <property name="description" type="java.lang.String">
            <column name="description" length="65535" />
        </property>
        <property name="url" type="java.lang.String">
            <column name="url" length="250" not-null="true" />
        </property>
        <property name="port" type="java.lang.Integer">
            <column name="port" not-null="true" />
        </property>
        <set name="ecmPortals" table="ecm_portal_server" inverse="true" lazy="true">
            <key>
                <column name="server_id" not-null="true" />
            </key>
            <many-to-many class="com.basfits.insite.core.portal.EcmPortal" />
        </set>
        <bag name="ecmEnvironmentDatas" cascade="all" table="ecm_server_environment_data">
            <key column="server_id" />
            <many-to-many class="com.basfits.insite.core.environmentData.EcmEnvironmentData" column="environment_id"/>
        </bag>
    </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
      
EcmServer server1 = new EcmServer("String", "String", new Integer(9091));
      EcmEnvironmentData data = new EcmEnvironmentData();
      data.setName("NAME");
      data.setContext("/group");
      data.getEcmServers().add(server1);      
      environmentDataDao.save(data);



Log:
16:28:46,218 DEBUG [EcmEnvironmentDataDAO] saving EcmEnvironmentData instance
16:28:46,531 DEBUG [SessionImpl] opened session at timestamp: 12366125263
16:28:46,531 DEBUG [DefaultSaveOrUpdateEventListener] saving transient instance
16:28:46,546 DEBUG [AbstractSaveEventListener] saving [com.basfits.insite.core.environmentData.EcmEnvironmentData#<null>]
16:28:46,546 DEBUG [AbstractSaveEventListener] executing insertions
16:28:46,562 DEBUG [Cascade] processing cascade ACTION_SAVE_UPDATE for: com.basfits.insite.core.environmentData.EcmEnvironmentData
16:28:46,562 DEBUG [Cascade] done processing cascade ACTION_SAVE_UPDATE for: com.basfits.insite.core.environmentData.EcmEnvironmentData
16:28:46,625 DEBUG [WrapVisitor] Wrapped collection in role: com.basfits.insite.core.environmentData.EcmEnvironmentData.ecmServers
16:28:46,640 DEBUG [WrapVisitor] Wrapped collection in role: com.basfits.insite.core.environmentData.EcmEnvironmentData.ecmHistories
16:28:46,640 DEBUG [WrapVisitor] Wrapped collection in role: com.basfits.insite.core.environmentData.EcmEnvironmentData.ecmClusters
16:28:46,687 DEBUG [AbstractSaveEventListener] executing identity-insert immediately
16:28:46,687 DEBUG [AbstractEntityPersister] Inserting entity: com.basfits.insite.core.environmentData.EcmEnvironmentData (native id)
16:28:46,703 DEBUG [AbstractBatcher] about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
16:28:46,703 DEBUG [ConnectionManager] opening JDBC connection
16:28:46,765 DEBUG [SQL]
insert
into
ecm.ecm_environment_data
(runtime_environment_Id, name, context, version)
values
(?, ?, ?, ?)
Hibernate:
insert
into
ecm.ecm_environment_data
(runtime_environment_Id, name, context, version)
values
(?, ?, ?, ?)
16:28:46,765 DEBUG [AbstractBatcher] preparing statement
16:28:46,781 DEBUG [AbstractEntityPersister] Dehydrating entity: [com.basfits.insite.core.environmentData.EcmEnvironmentData#<null>]
16:28:46,796 DEBUG [LongType] binding null to parameter: 1
16:28:46,796 DEBUG [StringType] binding 'BASF.COM' to parameter: 2
16:28:46,796 DEBUG [StringType] binding '/group' to parameter: 3
16:28:46,796 DEBUG [FloatType] binding null to parameter: 4
16:28:46,968 DEBUG [IdentifierGeneratorFactory] Natively generated identity: 33
16:28:46,968 DEBUG [AbstractBatcher] about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
16:28:46,968 DEBUG [AbstractBatcher] closing statement
16:28:46,984 DEBUG [Cascade] processing cascade ACTION_SAVE_UPDATE for: com.basfits.insite.core.environmentData.EcmEnvironmentData
16:28:46,984 DEBUG [Cascade] cascade ACTION_SAVE_UPDATE for collection: com.basfits.insite.core.environmentData.EcmEnvironmentData.ecmServers
16:28:47,000 DEBUG [CascadingAction] cascading to saveOrUpdate: com.basfits.insite.core.server.EcmServer
16:28:47,000 DEBUG [AbstractSaveEventListener] transient instance of: com.basfits.insite.core.server.EcmServer
16:28:47,000 DEBUG [DefaultSaveOrUpdateEventListener] saving transient instance
16:28:47,000 DEBUG [AbstractSaveEventListener] saving [com.basfits.insite.core.server.EcmServer#<null>]
16:28:47,000 DEBUG [AbstractSaveEventListener] executing insertions
16:28:47,000 DEBUG [Cascade] processing cascade ACTION_SAVE_UPDATE for: com.basfits.insite.core.server.EcmServer
16:28:47,000 DEBUG [Cascade] done processing cascade ACTION_SAVE_UPDATE for: com.basfits.insite.core.server.EcmServer
16:28:47,000 DEBUG [WrapVisitor] Wrapped collection in role: com.basfits.insite.core.server.EcmServer.ecmPortals
16:28:47,000 DEBUG [WrapVisitor] Wrapped collection in role: com.basfits.insite.core.server.EcmServer.ecmEnvironmentDatas
16:28:47,000 DEBUG [AbstractSaveEventListener] executing identity-insert immediately
16:28:47,000 DEBUG [AbstractEntityPersister] Inserting entity: com.basfits.insite.core.server.EcmServer (native id)
16:28:47,000 DEBUG [AbstractBatcher] about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
16:28:47,000 DEBUG [SQL]
insert
into
ecm.ecm_server
(cluster_id, name, description, url, port)
values
(?, ?, ?, ?, ?)
Hibernate:
insert
into
ecm.ecm_server
(cluster_id, name, description, url, port)
values
(?, ?, ?, ?, ?)
16:28:47,015 DEBUG [AbstractBatcher] preparing statement
16:28:47,015 DEBUG [AbstractEntityPersister] Dehydrating entity: [com.basfits.insite.core.server.EcmServer#<null>]
16:28:47,015 DEBUG [LongType] binding null to parameter: 1
16:28:47,015 DEBUG [StringType] binding 'string 1' to parameter: 2
16:28:47,015 DEBUG [StringType] binding null to parameter: 3
16:28:47,015 DEBUG [StringType] binding 'String' to parameter: 4
16:28:47,015 DEBUG [IntegerType] binding '9091' to parameter: 5
16:28:47,031 DEBUG [IdentifierGeneratorFactory] Natively generated identity: 42
16:28:47,046 DEBUG [AbstractBatcher] about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
16:28:47,046 DEBUG [AbstractBatcher] closing statement
16:28:47,046 DEBUG [Cascade] processing cascade ACTION_SAVE_UPDATE for: com.basfits.insite.core.server.EcmServer
16:28:47,046 DEBUG [Cascade] cascade ACTION_SAVE_UPDATE for collection: com.basfits.insite.core.server.EcmServer.ecmEnvironmentDatas
16:28:47,046 DEBUG [Cascade] done cascade ACTION_SAVE_UPDATE for collection: com.basfits.insite.core.server.EcmServer.ecmEnvironmentDatas
16:28:47,046 DEBUG [Cascade] done processing cascade ACTION_SAVE_UPDATE for: com.basfits.insite.core.server.EcmServer
16:28:47,046 DEBUG [Cascade] done cascade ACTION_SAVE_UPDATE for collection: com.basfits.insite.core.environmentData.EcmEnvironmentData.ecmServers
16:28:47,046 DEBUG [Cascade] done processing cascade ACTION_SAVE_UPDATE for: com.basfits.insite.core.environmentData.EcmEnvironmentData
16:28:47,046 DEBUG [EcmEnvironmentDataDAO] save successful


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 10, 2009 6:51 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
From the MTM end in the EcmServer class mapping, remove cascade option and set inverse="true".

Now in the code set the association in the EcmServer end also like:
Code:
      EcmServer server1 = new EcmServer("String", "String", new Integer(9091));
      EcmEnvironmentData data = new EcmEnvironmentData();
      data.setName("NAME");
      data.setContext("/group");
      data.getEcmServers().add(server1);
      server1.getEcmEnvironmentDatas().add(data); //*****Note this
      environmentDataDao.save(data);

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 10, 2009 7:43 am 
Newbie

Joined: Mon Mar 09, 2009 11:21 am
Posts: 11
Thank you littypreethkr. But this didn't work :(

Probably the failure is in my Session Factory?

Code:
<bean id="DataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://xxxx/xxx" />
        <property name="username" value="xxxx" />
        <property name="password" value="xxxx" />
    </bean>
   
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="DataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
            </props>
        </property>
        <property name="mappingResources">
           <list>
            [....list of all mapping files...]
           </list>
       </property>
    </bean>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 10, 2009 7:49 am 
Newbie

Joined: Mon Mar 09, 2009 11:21 am
Posts: 11
Thank you littypreethkr. But this didn't work :(

Probably the failure is in my Session Factory?

Code:
<bean id="DataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://xxxx/xxx" />
        <property name="username" value="xxxx" />
        <property name="password" value="xxxx" />
    </bean>
   
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="DataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
            </props>
        </property>
        <property name="mappingResources">
           <list>
            [....list of all mapping files...]
           </list>
       </property>
    </bean>


PS: Sorry for posting this twice!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 10, 2009 8:57 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Well I dont think it could be anythng to do with your SessionFactory... Coz ur other tables are getting updated. Issue is only with the link table rite?

So it could be some issue with the mapping or the code. Is this issue only with the server-env relation?

May be you shud cut short all other mappings and try only with the server-env relation.

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 9:08 am 
Newbie

Joined: Mon Mar 09, 2009 11:21 am
Posts: 11
I have a second m to n relation. There i have the same Problem... The Tables "ecm_server" and "ecm_environment_data" are filled correct, but not the mapping table "ecm_server_environment_data".

I followed your suggestion, but sadly, it didn't work. Here are the create statements of my Tables, probably there is the problem:

ecm_server_environment_data
Code:
DROP TABLE IF EXISTS `ecm`.`ecm_server_environment_data`;
CREATE TABLE  `ecm`.`ecm_server_environment_data` (
  `server_id` int(10) unsigned NOT NULL,
  `environment_data_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`server_id`,`environment_data_id`),
  KEY `env_env` (`environment_data_id`),
  CONSTRAINT `env_env` FOREIGN KEY (`environment_data_id`) REFERENCES `ecm_environment_data` (`environment_id`),
  CONSTRAINT `server_server` FOREIGN KEY (`server_id`) REFERENCES `ecm_server` (`server_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


ecm_server
Code:
DROP TABLE IF EXISTS `ecm`.`ecm_server`;
CREATE TABLE  `ecm`.`ecm_server` (
  `server_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `description` text,
  `url` varchar(250) NOT NULL,
  `port` int(11) NOT NULL,
  `cluster_id` int(10) unsigned DEFAULT NULL,
  PRIMARY KEY (`server_id`),
  KEY `ClusterServer` (`cluster_id`),
  CONSTRAINT `ClusterServer` FOREIGN KEY (`cluster_id`) REFERENCES `ecm_cluster` (`cluster_id`)
) ENGINE=InnoDB AUTO_INCREMENT=55 DEFAULT CHARSET=latin1;


ecm_environment_data
Code:
DROP TABLE IF EXISTS `ecm`.`ecm_environment_data`;
CREATE TABLE  `ecm`.`ecm_environment_data` (
  `environment_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  `context` varchar(250) DEFAULT NULL,
  `version` float DEFAULT NULL,
  `runtime_environment_Id` int(11) unsigned DEFAULT NULL,
  PRIMARY KEY (`environment_id`),
  KEY `env_runtime` (`runtime_environment_Id`),
  CONSTRAINT `env_runtime` FOREIGN KEY (`runtime_environment_Id`) REFERENCES `ecm_runtime_environment` (`runtime_environment_id`)
) ENGINE=InnoDB AUTO_INCREMENT=53 DEFAULT CHARSET=latin1;


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 9:20 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Quote:
DROP TABLE IF EXISTS `ecm`.`ecm_server_environment_data`;
CREATE TABLE `ecm`.`ecm_server_environment_data` (
`server_id` int(10) unsigned NOT NULL,
`environment_data_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`server_id`,`environment_data_id`),
KEY `env_env` (`environment_data_id`),
CONSTRAINT `env_env` FOREIGN KEY (`environment_data_id`) REFERENCES `ecm_environment_data` (`environment_id`),
CONSTRAINT `server_server` FOREIGN KEY (`server_id`) REFERENCES `ecm_server` (`server_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

I dont have MySql expertise, so a doubt....
Does the KEY declaration says that environment_data_id is unique?

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 9:30 am 
Newbie

Joined: Mon Mar 09, 2009 11:21 am
Posts: 11
Update: I added manually some relations to the Database and try to load the data with the relations via Hibernate... it works great. All is correctly mapped if i load the data.... but why not if I try to save them????


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 12, 2009 9:48 am 
Newbie

Joined: Mon Mar 09, 2009 11:21 am
Posts: 11
KEY is normally a synonym for INDEX. The key attribute PRIMARY KEY can also be specified as just KEY when given in a column definition. This was implemented for compatibility with other database systems.

I could create this Table without
"KEY `env_env` (`environment_data_id`)"
but mysql creates it automatically. I don't know why!

But this is just a normal index and it's non unique.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 13, 2009 4:16 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Are you for sure that the collection is having values at the time of commit also? May be they got overwritten. Can you jst do a sysout of the collection values just before the commit or flush?

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 16, 2009 7:45 am 
Newbie

Joined: Mon Mar 09, 2009 11:21 am
Posts: 11
I now solved the problem... Instead of "getSession" I now user "getHibernateTemplate". Now it works fine... but can anyone explain me the difference?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 16, 2009 8:27 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Congratsss on ur problem getting solved....

I dont have any expertise in Spring so I wont be able to ans ur ques. :(

_________________
Regards,
Litty Preeth


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