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
|