Reputation: 35
I've developed a simple web application which connects to MySQL through hibernate. At a point, I was even successful in creating a connection and inserting the java object in the DB. However, I did some changes thereafter to meet my project needs and there I messed up the things. I am receiving this error "No identifier specified for entity" no matter how much I try to debug it. I even created a new project from scratch but in vain. Please help me out here in identifying what I am doing wrong.
package com.proj.beandb;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
@ManagedBean
@SessionScoped
@Entity
@Table(name="dbstat")
public class Hibernate
{
@Id
@Column(name="first_name")
private static String fname;
@Column(name="last_name")
private static String lname;
public Hibernate(){}
public Hibernate(String fname, String lname)
{
Hibernate.fname = fname;
Hibernate.lname = lname;
}
public static void main(String[] args) {
// create session factory
SessionFactory factory = new Configuration().configure("hibernate.cfg.xml")
.addAnnotatedClass(Student.class)
.buildSessionFactory();
// create session
Session session = factory.getCurrentSession();
try {
// create a student object
System.out.println("Creating new object");
Hibernate tempRec = new Hibernate("bah", "blah");
// start a transaction
session.beginTransaction();
// save the object
session.save(tempRec);
// commit transaction
session.getTransaction().commit();
System.out.println("Done!");
}
finally {
factory.close();
}
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
Hibernate.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
Hibernate.lname = lname;
}
}
`
Upvotes: 1
Views: 3793
Reputation: 19956
It is a really a big mistake to use static
fields as properties of a persistent class.
@Id
@Column(name="first_name")
private static String fname;
@Column(name="last_name")
private static String lname;
should be
@Id
@Column(name="first_name")
private String fname;
@Column(name="last_name")
private String lname;
You don't need it, because of it is not a Hibernate related stuff
@ManagedBean
@SessionScoped
You add Student
class with addAnnotatedClass(Student.class)
, maybe you need to add Hibernate
class instead.
And please don't put main()
in the persistent class. You need to have Hibernate
class and, for an example, HibernateTest
class with main()
.
Upvotes: 1