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.

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

   joinColumns= @JoinColumn(name = "medicine_id")),
   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;
 public MedicineShopId getPk() {
  return pk;
 public void setPk(MedicineShopId pk) { = pk;

Happy Programming...!!!