-->
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.  [ 3 posts ] 
Author Message
 Post subject: How to insert Entity Which has BasicDBObject field?
PostPosted: Wed Aug 12, 2015 11:44 am 
Newbie

Joined: Wed Aug 12, 2015 10:54 am
Posts: 1
Hi,

I have Entity which has BasicDBObject fields. I need to store that Entity into the Mongo DB using Hibernate-OGM Framework.


I have used Map using @ElementCollection annotation but the OGM framework itself generate key(<fieldname>_key). kindly refer following output

"mytree" : [
{
"mytree_KEY" : "ch_zn",
"mytree" : "chinese language"
},
{
"mytree_KEY" : "en",
"mytree" : "english"
}
]

But we need to store data into below format

{
"_id":ObjectId("5582b9eec00fd6913b730dc3"),
"mytree":[
{"ch_zn":"chinese language","en":"english"}

]
}


My Entity Class
--------------------
package com.test;

package com.test;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.bson.types.ObjectId;

import com.mongodb.BasicDBObject;

@Entity
@Table(name="metadata")
public class MetaData implements Serializable{

/**
*
*/
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="_id")
private ObjectId id;



@Column(name = "mytree")
private List<BasicDBObject> basicDBObject;

public MetaData(){}

public ObjectId getId() {
return id;
}

public void setId(ObjectId id) {
this.id = id;
}


public List<BasicDBObject> getBasicDBObject() {
return basicDBObject;
}
public void setBasicDBObject(List<BasicDBObject> basicDBObject) {
this.basicDBObject = basicDBObject;
}

}



When I tried to Store this Entity into Mongo Database It Gives the following Exceptions:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoEntityManagerFactory' defined in class path resource [Beans.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: primary] Unable to build Hibernate SessionFactory
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1482)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1117)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:922)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.test.OGMTestFinal.addObject(OGMTestFinal.java:40)
at com.test.OGMTestFinal.main(OGMTestFinal.java:28)
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: primary] Unable to build Hibernate SessionFactory
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1249)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.access$600(EntityManagerFactoryBuilderImpl.java:120)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:860)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:425)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:849)
at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:152)
at org.hibernate.ogm.jpa.HibernateOgmPersistence.createContainerEntityManagerFactory(HibernateOgmPersistence.java:105)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:288)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1541)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1479)
... 13 more
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.List, at table: metadata, for columns: [org.hibernate.mapping.Column(mytree)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:349)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:322)
at org.hibernate.mapping.Property.isValid(Property.java:241)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:496)
at org.hibernate.mapping.RootClass.validate(RootClass.java:270)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1360)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1851)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:857)
... 22 more

Kindly suggest the solutions

Waiting for your reply

Regards,
Gunavant


Top
 Profile  
 
 Post subject: Re: How to insert Entity Which has BasicDBObject field?
PostPosted: Mon Aug 17, 2015 4:18 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
Hi Gunavant,
I don't think there is away at the moment to obtain this behaviour without changing the dialect.

On the bright side, we fixed this issue and it will be available on the next release: https://hibernate.atlassian.net/browse/OGM-555

Cheers,
Davide


Top
 Profile  
 
 Post subject: Re: How to insert Entity Which has BasicDBObject field?
PostPosted: Thu Aug 20, 2015 2:49 am 
Hibernate Team
Hibernate Team

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

As Davide is saying, improved Map support for MongoDB will be part of the upcoming 5.0 Alpha1 release. However you will not be able to use DBObjects directly within your entities as suggested in your example listing. Instead, you'd model it like so:

@ElementCollection
private Map<String, String> myTree;

This will give you the more natural map representation, although with a minor diference to what you've pasted:

...
"mytree": {
"ch_zn":"chinese language",
"en":"english"
}
...

I.e. there will be no array in this case.

Cheers,

--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.  [ 3 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.