-->
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.  [ 6 posts ] 
Author Message
 Post subject: many-to-many (JBoss - Hibernate - SessionBean - Client)
PostPosted: Mon Mar 15, 2004 6:26 pm 
Newbie

Joined: Mon Mar 15, 2004 5:39 pm
Posts: 5
Hello, iam new to hibernate and checked the manual since 3 days but can't find a solution. I run hibernate on JBoss as an MBean and a SessionBean provides methods for Clients from other vm. Everything works fine. Except the Session#saveOrUpdate() method in association with updates on the many-to-many sets (.hbm.xml at http://kleinmantara.de/hibernate/). Only the inverse="false" side is updated.

My add and remove methods looks like this:
Code:
public void addUser(User user)
{
    users.add(user);
    user.getUserGroups().add(this);
}


My SessionBean running on JBoss, called from a other vm.
Code:
    public void ejbCreate() throws CreateException
    {
        Context context = null;
        try
        {
            context = new InitialContext();
            factory = (SessionFactory) context.lookup(jndiName);
        }
        finally
        {
            if (context != null) try { context.close(); } catch (Exception ignore){}
        }
    }
    public List find(String query) throws Exception
    {
        Session session = factory.openSession();
        try
        {
            return = session.find(query);
        }
        finally
        {
            if (session != null) try { session.close(); } catch (Exception ignore) { }
        }
    }
[...]
    public void save(ISecModel secModel) throws Exception
    {
        Session session         = session = factory.openSession();
        Transaction transaction = null;
        try
        {
            transaction = session.beginTransaction();
            session.saveOrUpdate(secModel);
            session.flush();
            transaction.commit();
        }
        catch (Exception ex)
        {
            if (transaction != null)  transaction.rollback();
        }
        finally
        {
            if (session != null) try { session.close(); } catch (Exception ignore) {}
        }
    }

Is this the "right way" to do things like that? Or is this the wrong way?

Versions:
JBoss 3.2.3
Hibernate version 2.1.2, 4 February 2003
MySQL Ver 3.23.55 for linux on i686
SUN jdk 1.4.2_04


Thanks for your help.
Thomas


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 17, 2004 10:11 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Quote:
Only the inverse="false" side is updated.

incerse=false means this side is responsible for the association. So it is expected.

Please read http://www.hibernate.org/155.html

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 17, 2004 1:22 pm 
Newbie

Joined: Mon Mar 15, 2004 5:39 pm
Posts: 5
hi Emmanuel,
of course your right. But if i run my app local on my client without JBoss only hibernate, it works. Local i do all the work in the same Hibernate Session and the update/inserts/removes works.
Code:
public class LocalOpenBrige implements IBridge
{
    Session session;
    SessionFactory factory;

    public LocalOpenBrige() throws HibernateException
    {
        Configuration configuration = new Configuration();
        configuration.addClass(User.class);
        configuration.addClass(UserGroup.class);

        factory = configuration.buildSessionFactory();
        session = factory.openSession();
    }

    public List find(String query) throws Exception
    {
        return session.find(query);
    }

    public void save(ISecModel secModel) throws Exception
    {
        Transaction transaction = session.beginTransaction();
        try
        {
            session.saveOrUpdate(secModel);
            session.flush();
            transaction.commit();
        }
        catch (Exception ex) { transaction.rollback(); }
    }
}

If i run local i get follow log messages: http://www.kleinmantara.de/hibernate/LogLocal.txt
The same remote with JBoss and SLSB: http://www.kleinmantara.de/hibernate/LogSession.txt

I remove a User from UserGroup and call Session#saveOrUpdate(UserGroup).

Is this correct that in the same Hibernate Session it works and in two different Sessions not? If this mentioned in your link, i'm sorry but i don't see it.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 17, 2004 1:26 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
It depends on how you linked your obejct graph in yout client app.
Do you map it both side ?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 17, 2004 1:30 pm 
Newbie

Joined: Mon Mar 15, 2004 5:39 pm
Posts: 5
yes, you can see it here (http://kleinmantara.de/hibernate)
Code:
User.java
    public void addUserGroup(UserGroup group)
    {
        userGroups.add(group);
        group.getUsers().add(this);
    }

UserGroup.java
    public void addUser(User user)
    {
        users.add(user);
        user.getUserGroups().add(this);
    }


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 17, 2004 2:38 pm 
Pro
Pro

Joined: Mon Sep 08, 2003 4:30 pm
Posts: 203
I have a very similar problem with many-to-many and JBoss.

Mappings:

Code:
    <class name="com.db.device.NetPortalSubnetwork" table="subnet" mutable="true" polymorphism="implicit" dynamic-update="false" dynamic-insert="false" proxy="com.db.device.NetPortalSubnetwork">
      <id name="id" type="long" unsaved-value="0">
         <generator class="native">
         </generator>
      </id>
      
      <set name="mgmtInfo" table="ne_subnet" inverse="true" lazy="false" cascade="save-update">
         <key column="subnet_id"/>
         <many-to-many class="com.db.device.NEMgmtInfo" column="ne_subnet"/>
      </set>
    </class>



Code:
    <class name="com.diatem.db.device.NEMgmtInfo" table="nemgmtinfo" mutable="true" polymorphism="implicit" dynamic-update="false" dynamic-insert="false">
      <!--cache usage="read-write" /-->
      <id name="id" type="long" unsaved-value="0">
         <generator class="native">
         </generator>
      </id>
      <set name="subnets" table="ne_subnet" lazy="true">
         <key>
            <column name="ne_subnet" not-null="true"/>
         </key>
         <many-to-many class="com.diatem.db.device.NetPortalSubnetwork">
            <column name="subnet_id" not-null="true"/>
         </many-to-many>
      </set>
    </class>


Code:
    <class name="com.db.device.NetPortalNetwork" table="network" mutable="true" polymorphism="implicit" dynamic-update="false" dynamic-insert="false" proxy="com.db.device.NetPortalNetwork">
      <id name="id" type="long" unsaved-value="0">
         <generator class="native">
         </generator>
      </id>

       <set name="subnet" cascade="all-delete-orphan" inverse="true" lazy="true">
         <key column="net_subnet_id"/>
         <one-to-many class="com.db.device.NetPortalSubnetwork"/>
       </set>

</class>


Then I try to update the network:

Code:
      NetPortalNetwork network = helper.getNetworkByPK(2);
      NEMgmtInfo device = helper.getDeviceByPK(46);

      Set subnets = helper.getSubnetsForNe(device);
      device.setSubnets(subnets);
      System.out.println(device);

      Set nes = new HashSet();
      nes.add(device);

      NetPortalSubnetwork subnet =
         new NetPortalSubnetwork("test_"+new Date(), "", network, nes, null);
         network.getSubnet().add(subnet);
         Iterator iter = nes.iterator();
         while(iter.hasNext()){
            NEMgmtInfo ne = (NEMgmtInfo)iter.next();
            ne.getSubnets().add(subnet);
            ne.setNetwork(network);
         }
         network = helper.updateNetwork(network);


Now, the funny thing is that

sometimes a new record is inserted into the join table (ne_subnet)
sometimes not.

Note:
The
Code:
helper
class calls a JBoss session bean which in turn opens and closes a Hibernate session per request.


Please help.


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