Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Need help with a runtime error using Hibernate in a Java application

Posted on 2009-12-16
11
Medium Priority
?
498 Views
Last Modified: 2013-11-23
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.
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;
	}
	

}
----------------------
full error stacktrace:
----------------------

exception 

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)

Open in new window

0
Comment
Question by:fsyed
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26069202
You haven't specified the annotations for primary key.

See the following link for reference on the same
http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#d0e1700

Let me know if this helps

Thanks
0
 

Author Comment

by:fsyed
ID: 26069235
Thanks for your prompt reply, but unfortunately your answer does not help.  I mentioned in my question that I do not have a primary key in my table, and because of the nature of the exception that I am getting, my question is, can this issue be resolved WITHOUT the use of a primary key in my table, or is having a primary key in my table, that is mapped to a field in my POJO using the annotation @Id the only way to rectify this?

Thanks again for your help.
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26069287
No Primary key in the table?
I guess that you should
https://www.hibernate.org/329.html#A6

If primary key is not possible, then make composite keys and specify the annotation using the previous link
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:fsyed
ID: 26069337
How do I make composite keys using Hibernate?
0
 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 400 total points
ID: 26069357
0
 
LVL 5

Assisted Solution

by:josephtsang
josephtsang earned 400 total points
ID: 26070389
Are there any reason you don't want to define a primary key for it?

I ask this because choosing a single or composite key as primary is not quite a topic on Hibernate, but much on the DB design. So you should ask yourself whether you need to link this Hadith by other objects (tables). Some people would emphasize on normalization, but just bear in mind that composite key can be a disaster if it gets more and more 'composite'... you need to change every table referring it as the foreign key (ripple effect).

In fact, Hibernate relies on primary key very much, because it relies on foreign key to link different objects.
0
 

Assisted Solution

by:vasume2003
vasume2003 earned 400 total points
ID: 26070692
1.It is not mandatory to have a primary key in database table to use hibernate.Hibernate wont check whether that key is actually primary key or not in database.
2.Now we can  annotate  any of the property of mapping or POJO class as @id.It would be better if the property is Unique key.
0
 
LVL 20

Assisted Solution

by:Sathish David Kumar N
Sathish David  Kumar N earned 800 total points
ID: 26078121
As the table has no primary key, it should not be mapped as an entity.

You could use a named query to insert data instead of declaring an entity, and perform the insert from the dao.
0
 

Author Comment

by:fsyed
ID: 26078188
dravidnsr:

What do you mean by using a named query?  Could you give me an example?
0
 
LVL 20

Assisted Solution

by:Sathish David Kumar N
Sathish David  Kumar N earned 800 total points
ID: 26078210


like this !!

@NamedQuery(name="empDetails",query="SELECT st FROM empMaster"),
0
 

Author Closing Comment

by:fsyed
ID: 31667167
Thanks very much for your replies.  I added a primary key to my table, and was able to make my application work!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month15 days, 17 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question