Sunday, January 26, 2014

Hibernate mapping exception: Could not determine type for columns: [org.hibernate.mapping.Column]

Recently when i was implementing Many to Many with Join columns with Additional column, i was getting the following error when i was building my application. Below is the exception from my application stack.

Invocation of init method failed; nested exception is org.hibernate.MappingException: 
Could not determine type for: com.expense.domain.Medicine, at table: Medicine_Shop, for columns: [org.hibernate.mapping.Column(medicine)]

This is because my application doesn't find all of your annotations, because I was annotating both fields and methods. You have to use only one strategy.
Either annotate in fields else methods, here in my case, i was annotating at field level in all classes, except in one class, where i have annotated at the method level (getter/setter) because of that annotation doesn't have any effect. Below is the sample code, which triggers the above exception, where you can see in the MedicineShop class, i have Annotated @EmbeddedId at the method level instead of the field level, Once i move the annotation to the field level, the issue is resolved.

@Entity
@Table(name="Medicine")
public class Medicine{
 
 @Id
 @GeneratedValue
 private int id;
 @Column(name="Name")
 private String name;
 @Column(name="CompanyName")
 private String companyName;
 @OneToMany(fetch=FetchType.LAZY, mappedBy="pk.medicine", cascade=CascadeType.ALL)
 private Set medicineShop= new HashSet();
}

@Entity
@Table(name="Medicine_Shop")
@AssociationOverrides({
 @AssociationOverride(name="pk.medicine", 
   joinColumns= @JoinColumn(name = "medicine_id")),
 @AssociationOverride(name="pk.shop", 
   joinColumns= @JoinColumn(name = "shop_id"))
})
public class MedicineShop implements Serializable 
{
 
 private MedicineShopId pk = new MedicineShopId ();
 @Column(name="capacity", length=10, nullable=false)
 private int capacity;
 
 @EmbeddedId
 public MedicineShopId getPk() {
  return pk;
 }
 public void setPk(MedicineShopId pk) {
  this.pk = pk;
 }
}


Happy Programming...!!!

No comments:

Post a Comment