Monday, April 2, 2012

DuplicateMappingException: Duplicate collection role mapping


Caused by: org.hibernate.DuplicateMappingException: Duplicate collection role mapping model.ItemSet.images
at org.hibernate.cfg.Configuration$MappingsImpl.addCollection(Configuration.java:3211)
at org.hibernate.cfg.HbmBinder.createClassProperties(HbmBinder.java:2189)
at org.hibernate.cfg.HbmBinder.createClassProperties(HbmBinder.java:2164)
at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:412)
at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:326)
at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:177)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:4006)
... 6 more

Reason:

Mapping is redundant. Remove the duplicate entries.

org.hibernate.PropertyNotFoundException: Could not find a getter for images in class model.ItemSet


Caused by: org.hibernate.PropertyNotFoundException: Could not find a getter for images in class model.ItemSet
at org.hibernate.property.BasicPropertyAccessor.createGetter(BasicPropertyAccessor.java:326)
at org.hibernate.property.BasicPropertyAccessor.getGetter(BasicPropertyAccessor.java:320)
at org.hibernate.mapping.Property.getGetter(Property.java:304)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildPropertyGetter(PojoEntityTuplizer.java:297)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:155)
at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:77)
... 15 more

Reason:

Hibernate looks for getImages in the ItemSet class, but it could not find such method or the method is not accessible. The method is not available or the property name declared in the hbm.xml file is wrong.


Solution:

1. Declare the getter method
2. Correct the property name in class and mapping xml
3. Declare getter method using protected access specifier

org.hibernate.AnnotationException: A sorted collection has to define @Sort:


org.hibernate.AnnotationException: A sorted collection has to define @Sort: model.ItemSortedMap.images
at org.hibernate.cfg.annotations.CollectionBinder.bind(CollectionBinder.java:446)
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1979)
at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:796)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:707)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:4035)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3989)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1398)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1856)
at util.SessionUtil.<clinit>(SessionUtil.java:10)
... 1 more


Reason: I used SortedMap as the property type. But does not annotate using @Sort annotation:

Solution: Add @Sort annotation or change the property type from SortedMap to Map.

org.hibernate.MappingException: Unknown entity: model.ItemSortedMap


org.hibernate.MappingException: Unknown entity: model.ItemSortedMap
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:693)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1485)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:120)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:713)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:701)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:697)
at main.TestSortedMap.main(TestSortedMap.java:32)

Reason: Annotated Entity class is not declared in hibernate.cfg.xml

Solution: Declare the annotated entity class name in the hibernate configuration file

org.hibernate.InstantiationException: No default constructor for entity: model.Address


org.hibernate.InstantiationException: No default constructor for entity: model.Address
at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:107)
at org.hibernate.tuple.component.AbstractComponentTuplizer.instantiate(AbstractComponentTuplizer.java:102)
at org.hibernate.type.ComponentType.instantiate(ComponentType.java:515)
at org.hibernate.type.ComponentType.deepCopy(ComponentType.java:434)
at org.hibernate.type.TypeHelper.deepCopy(TypeHelper.java:68)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:302)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:713)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:701)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:697)
at main.TestComponents.main(TestComponents.java:32)


Reason: Default constructor is not found in the Address.java or the access specified is private.

Solution: Declare a default no-arg constructor for the class

org.hibernate.MappingNotFoundException: resource: mapping/Address.hbm.xml not found


Caused by: org.hibernate.MappingNotFoundException: resource: mapping/Address.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:799)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2344)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2310)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2290)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2243)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2158)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2137)
at util.SessionUtil.<clinit>(SessionUtil.java:10)
... 1 more


Reason: hibernate.cfg.xml specifies mapping xml. The Address.hbm.xml mapping file is not exist in the path specified.

Solution: Place the Address.hbm.xml mapping file in the path specified or speciy the path of Address.hbm.xml mapping file or remove the <mapping> resource element in the hibernate.cfg.xml


org.hibernate.MappingException: unmapped class

org.hibernate.MappingException: An association from the table USER refers to an unmapped class: Address
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1824)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1756)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1423)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1856)
at util.SessionUtil.<clinit>(SessionUtil.java:10)
... 1 more

Reason: The address class specified in not existing or the FQCN is not correct or the mapping file is not available for the Address class. Correct the issues by placing the class in appropriate package or specify the correct fully qualified class name or create mapping file.