-->
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.  [ 5 posts ] 
Author Message
 Post subject: problem with collection persistence in many-to-many relation
PostPosted: Thu Jul 28, 2005 10:33 am 
Newbie

Joined: Mon May 16, 2005 11:55 am
Posts: 11
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 2.1.6

Mapping documents: Persona.hbm.xml, Privilegio.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
  PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

  <class name="it.crisaune.db.Persona" table="persona">

    <id name="id" type="integer" unsaved-value="null">
      <column name="id_persona" not-null="true"/>
      <generator class="native"/>
      <!--use-in-equals/-->
    </id>
    <property    name="nome"          column="nome"        type="string"/>
    <property    name="cognome"       column="cognome"       type="string"/>
    <property    name="dataRegistraz" column="data_reg_pers" type="date"/>

    <set name="privilegi" table="autorizzazioni" lazy="false"><!-- order-by="priorita asc"-->
      <key column="persona"/>
      <many-to-many column="privilegio" class="it.crisaune.db.Privilegio"/>
    </set>

  </class>

</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
  PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

  <class name="it.crisaune.db.Privilegio" table="privilegio">

    <id name="nome" type="string" unsaved-value="null">
      <column name="nome_priv" not-null="true"/>
      <generator class="assigned"/>
    </id>
    <property    name="priorita"    column="priorita"    type="integer"/>
    <property    name="azione"      column="azione"      type="string"/>

    <set name="persone"    table="autorizzazioni" lazy="false" inverse="true">
      <key column="privilegio"/>
      <many-to-many column="persona" class="it.crisaune.db.Persona"/>
    </set>

  </class>

</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
Code:
            Set elenco = new HashSet();
            Iterator it = privilegi.iterator();
            while (it.hasNext()) {
                Object o = it.next();
                o = ((SelectItem)o).getValue();
                o = session.load(Privilegio.class, (String)o);
                Privilegio p = (Privilegio) o;
                elenco.add(p);
            }
            value = (Persona) session.load(Persona.class, value.getId());
/*
The problem is here!!! because next update don't make persistent the change made by setPrivilegi(...)!!
*/
            value.setPrivilegi(elenco);
            session.update(value);

Full stack trace of any exception that occurs:
no exceptions.

Name and version of the database you are using:
Code:
MySQL 4.1

Tables created by schemaexport ant-task
Code:
CREATE TABLE `persona` (
  `id_persona` int(11) NOT NULL auto_increment,
  `nome` varchar(255) default NULL,
  `cognome` varchar(255) default NULL,
  `data_reg_pers` date default NULL,
  PRIMARY KEY  (`id_persona`),
) TYPE=MyISAM AUTO_INCREMENT=9 ;

-- --------------------------------------------------------

CREATE TABLE `privilegio` (
  `nome_priv` varchar(255) NOT NULL default '',
  `priorita` int(11) default NULL,
  `azione` varchar(255) default NULL,
  PRIMARY KEY  (`nome_priv`)
) TYPE=MyISAM;

-- --------------------------------------------------------

CREATE TABLE `autorizzazioni` (
  `persona` int(11) NOT NULL default '0',
  `privilegio` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`privilegio`,`persona`),
) TYPE=MyISAM;


The problem is that the collection is not made persistent on the database.

thanx....
cloud


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 11:15 am 
Beginner
Beginner

Joined: Wed Feb 23, 2005 2:23 pm
Posts: 21
Location: Pescara, italy
Hi Cloud,

do you commit your transaction?

Is there somehing like..

Code:
Transaction tx = null;

            try {
                tx = s.beginTransaction();

                //YOUR CODE HERE

                tx.commit();

            } catch (HibernateException e) {
               if (tx != null) {
                  try {
                      log.debug("tx.Rollback");
                      tx.rollback();
                        } catch (HibernateException ex) {}
                }
                log.error("Error", e);
            }


..around the code you posted in your question?

Gruß
Kai


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 01, 2005 3:24 pm 
Newbie

Joined: Mon May 16, 2005 11:55 am
Posts: 11
good idea, but I use an HttpFilter that intercept HTTP requests, instantiate a HibernateSessionManager that creates a Session (when occurs, like a Singleton) and after all (if the session is opened) closes it.
...And closing a session means committing the (unique) transaction. correctly? I know that it is correct. ...but probably I'm going wrong somewhere.... ^_^

ps: the Filter works like Thread Local Session design pattern.

thanx for any comment you like to post.
cloud


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 01, 2005 3:28 pm 
Newbie

Joined: Mon May 16, 2005 11:55 am
Posts: 11
as I written in the title, I have this problem only with many-to-many relations.
Tomorrow I try to isolate the problem and if it remains, I'll post simpler code.

bye...
cloud


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 01, 2005 4:06 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
czanon wrote:
as I written in the title, I have this problem only with many-to-many relations.
Tomorrow I try to isolate the problem and if it remains, I'll post simpler code.

bye...
cloud


You don't appear to have specified a cascade="" property for your many-to-many.

This should be set to cascade="all" or cascade="save-update" if you want to cascade persistence of this data.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


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