Dear fellow Java developers:
I am getting a runtime error when I try to run my java application that uses hibernate. The error message I'm getting is:
org.hibernate.AnnotationException: No identifier specified for entity: com.searchqs.beans.Hadith
From my research on the web, I've found out that you get this error when you have not set @Id for one of your properties or fields. The thing is, I don't have a primary key in my table, so I'm wondering if it's still necessary to set this value as one of my annotations? If this is so, is there a workaround to this, or must I absolutely have a primary key, which I must map to a field using the @Id annotation? Just wondering. I've attached the class that maps to my table, along with the full stack trace below.
Thanks in advance to all who reply.
Code:
package com.searchqs.beans;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name="hadith")
public class Hadith {
@Column(name="collection")
String collection;
@Column(name="volume")
int volume;
@Column(name="booknumber")
int bookNumber;
@Column(name="booktitle")
String bookTitle;
@Column(name="chapternumber")
int chapterNumber;
@Column(name="chaptertitle")
String chapterTitle;
@Column(name="hadithnumber")
int hadithNumber;
@Column(name="hadithgrade")
String hadithGrade;
@Column(name="narrator")
String narrator;
@Column(name="matntext")
String matnText;
public String getCollection() {
return collection;
}
public void setCollection(String collection) {
this.collection = collection;
}
public int getVolume() {
return volume;
}
public void setVolume(int volume) {
this.volume = volume;
}
public int getBookNumber() {
return bookNumber;
}
public void setBookNumber(int bookNumber) {
this.bookNumber = bookNumber;
}
public String getBookTitle() {
return bookTitle;
}
public void setBookTitle(String bookTitle) {
this.bookTitle = bookTitle;
}
public int getChapterNumber() {
return chapterNumber;
}
public void setChapterNumber(int chapterNumber) {
this.chapterNumber = chapterNumber;
}
public String getChapterTitle() {
return chapterTitle;
}
public void setChapterTitle(String chapterTitle) {
this.chapterTitle = chapterTitle;
}
public int getHadithNumber() {
return hadithNumber;
}
public void setHadithNumber(int hadithNumber) {
this.hadithNumber = hadithNumber;
}
public String getHadithGrade() {
return hadithGrade;
}
public void setHadithGrade(String hadithGrade) {
this.hadithGrade = hadithGrade;
}
public String getNarrator() {
return narrator;
}
public void setNarrator(String narrator) {
this.narrator = narrator;
}
public String getMatnText() {
return matnText;
}
public void setMatnText(String matnText) {
this.matnText = matnText;
}
}
Code:
javax.servlet.ServletException: Servlet execution threw an exception
root cause
java.lang.ExceptionInInitializerError
com.searchqs.utils.HibernateUtil.<clinit>(HibernateUtil.java:20)
com.searchqs.DAO.HadithDAO.saveOrUpdate(HadithDAO.java:18)
com.searchqs.controller.DataServlet.doPost(DataServlet.java:77)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
org.hibernate.AnnotationException: No identifier specified for entity: com.searchqs.beans.Hadith
org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:672)
org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:546)
org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:291)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)
org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
com.searchqs.utils.HibernateUtil.<clinit>(HibernateUtil.java:15)
com.searchqs.DAO.HadithDAO.saveOrUpdate(HadithDAO.java:18)
com.searchqs.controller.DataServlet.doPost(DataServlet.java:77)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)