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.