Wednesday, April 4, 2012

org.hibernate.MappingException: Repeated column in mapping for entity


 org.hibernate.MappingException: Repeated column in mapping for entity: model.User column: ID (should be mapped with insert="false" update="false")
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:676)
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:698)
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:720)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:474)
at org.hibernate.mapping.RootClass.validate(RootClass.java:235)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1362)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1865)
at util.SessionUtil.<clinit>(SessionUtil.java:10)
... 1 more

Reason:

The annotation @Column name property is given the same value for two fields.


@Column(name = "ID")
private String id;

@Column(name = "ID")
private String name;

Solution:

Provide unique name for the column name.

@Column(name = "ID")
private String id;

@Column(name = "NAME")
private String name;

org.hibernate.MappingException: Association references unmapped class


org.hibernate.MappingException: Association references unmapped class: Bid
at org.hibernate.cfg.HbmBinder.bindCollectionSecondPass(HbmBinder.java:2503)
at org.hibernate.cfg.HbmBinder$CollectionSecondPass.secondPass(HbmBinder.java:2782)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:65)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1716)
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 mapping xml does not specify the class name correctly.


<bag name="bids" cascade="all">
<key column="ITEM_ID" />
<one-to-many class="Bid" />
</bag>

In the above collection mapping, the Bid class does not declared using full path.

Solution:

Enter the fully qualified class name of the collection target class (Bid).

<bag name="bids" cascade="all">
<key column="ITEM_ID" />
<one-to-many class="model.Bid" />
</bag>




org.hibernate.HibernateException: /hibernate.cfg.xml not found


org.hibernate.HibernateException: /hibernate.cfg.xml not found
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:170)
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:2176)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2157)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2137)
at util.SessionUtil.<clinit>(SessionUtil.java:10)

Reason: hibernate.cfg.xml is not found in the default location or the location specified to the configuration. Place the hibernate.cfg.xml file in root folder of the source code or specify the correct file path to configuration object.

Tuesday, April 3, 2012

org.hibernate.MappingException: An association from the table refers to an unmapped class:


org.hibernate.MappingException: An association from the table ADDRESS refers to an unmapped class: model.User
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:

Mapping for a non existent field is there in the mapping xml. I have an Address class. It has one-to-one relationship between User class. Later I delete the association from Address class, but forgot to delete the mapping from xml.

Solution:

1. Remove the unwanted property mapping element
2. Add the correct property back to the class.

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.