-->
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: Mongo ManyToMany Mapping - Embedded Object when Expecting Id
PostPosted: Thu Sep 12, 2013 9:31 pm 
Newbie

Joined: Thu Sep 12, 2013 9:17 pm
Posts: 2
Hey,

I'm having a little trouble getting the expected result out of a simple many to many mapping.

First things first - dependencies:
org.hibernate.ogm:hibernate-ogm-mongodb:4.0.0.Beta1
org.mongodb:mongo-java-driver:2.11.2
org.springframework.data:spring-data-mongodb:1.2.3.RELEASE

Code:

import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.ManyToMany;

import org.springframework.data.annotation.Id;

@Entity
public class A {

   @Id
   public String id;
   
   @ManyToMany
   public Set<B> bs;
}


Code:

import javax.persistence.Entity;

import org.springframework.data.annotation.Id;

@Entity
public class B {
   
   @Id
   public String id;
   
   
   public String ts = "ssklskl";
   
   public String shss = "hssla";
}


Code:
                A a = new A();
      template.save(a);
      
      B b = new B();
      template.save(b);
      
      a = template.findById(a.id, A.class);
      b = template.findById(b.id, B.class);
      Set<B> bs = new HashSet<B>();
      bs.add(b);
      a.bs = bs;
      
      template.save(a);


The Result when using the command line:
Code:
{ "_id" : ObjectId("523264e291fddeedcb688d2f"), "_class" : "com.beelite.beans.A", "bs" : [     {     "_id" : ObjectId("523264e291fddeedcb688d30"),     "ts" : "ssklskl",     "shss" : "hssla" } ] }


I haven't changed any of the defaults. I was expecting just the IDs to be embedded and not the whole object. Am I missing something in my mapping?

Thanks in advance,

Brian


Top
 Profile  
 
 Post subject: Re: Mongo ManyToMany Mapping - Embedded Object when Expecting Id
PostPosted: Fri Sep 13, 2013 5:43 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
Hi brianhyder,

In general, this way of mapping collections offers better performance for read operations and the document can be retrieved with a single database operation.

You can change the way collections are stored in MongoDB using the option hibernate.ogm.mongodb.associations.store
More details about it in the documentation: http://docs.jboss.org/hibernate/ogm/4.0/reference/en-US/html/ogm-datastore-providers.html#d0e1875

I hope this has been helpful.

Davide


Top
 Profile  
 
 Post subject: Re: Mongo ManyToMany Mapping - Embedded Object when Expecting Id
PostPosted: Sun Sep 15, 2013 4:55 pm 
Newbie

Joined: Thu Sep 12, 2013 9:17 pm
Posts: 2
Hey,

Thanks for the quick reply. I have a couple of follow up items.

1) The documentation specifies the properties but how/where to set them. I am currently using spring data on top of OGM and have a spring configuration (see below) but cannot figure out how to set the properties:
Code:
   <!-- Default bean name is 'mongo' -->
   <mongo:mongo host="xxx.xxx.xxx.xxx" port="27017" >
      <!-- OPTIONAL: configure <mongo:options /> -->
   </mongo:mongo>

   <mongo:db-factory dbname="dbtest" mongo-ref="mongo" />

   <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
      <constructor-arg ref="mongoDbFactory" />
   </bean>


2) The default "store" is listed as IN_ENTITY according the documentation at the link you provided in section 5.3.2.2. The example provided is what I would expect from the code I posted in the original question but instead I'm seeing the "B" object nested within the "A" object. I'm curious if I'm misinterpreting the documentation, the documentation is wrong, or if there is a bug in the OGM code.

3) I went one step further with my sample code I provided earlier (see below). When I delete the "B" object instance the persisted "A" object is not updated to reflect the deletion of the "B" instance. Is this expected behavior? Does this behavior change if the "store" is changed?
Code:
A a = new A();
      template.save(a);
      
      B b = new B();
      template.save(b);
      
      a = template.findById(a.id, A.class);
      b = template.findById(b.id, B.class);
      Set<B> bs = new HashSet<B>();
      bs.add(b);
      a.bs = bs;
      
      template.save(a);
      
      template.remove(b);
      
      a = template.findById(a.id, A.class);


Sorry for all the followup. I'm really interested in this framework and want to make sure I'm using it correctly.

-Brian


Top
 Profile  
 
 Post subject: Re: Mongo ManyToMany Mapping - Embedded Object when Expecting Id
PostPosted: Thu Oct 10, 2013 3:32 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Hi Brian,

Refer to http://docs.jboss.org/hibernate/ogm/4.0/reference/en-US/html/ogm-configuration.html#_bootstrapping_hibernate_ogm to find out how to configure OGM; basically you specify any properties either in persistence.xml or you set them programmatically on your OgmConfiguration when using the native Hibernate APIs.

I'm wondering about your usage of OGM *and* Spring Data MongoDB at the same time, though. Both provide their own means for accessing MongoDB, so you should be using either of them but not both at the same time. As your example uses Spring Data's MongoTemplate, OGM won't be involved at all.

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


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.