
I want to know how to use hibernate annotations in case of composite primary key in one table. I tried like this, I have created a table without primary key. By default mssql has created a composite primary key with all the columns of the table.
table: create table student(sid number(5),sname varchar2(20),age number(3));
And i have created 2 pojo classes like this,
Student.java:
@Entity @Table(name="student_tbl") @IdClass(StudentID.class) public class Student implements Serializable{ /** * */ private static final long serialVersionUID = -7007299156855950071L; private StudentID id;
public StudentID getId() {
return id;
}
public void setId(StudentID id) {
this.id = id;
}
}
import javax.persistence.Column; import javax.persistence.Embeddable; import javax.persistence.OneToMany; @Embeddable public class StudentID {
long sid;
String name;
double age;
@OneToMany
public long getSid() {
return sid;
}
public void setSid(long sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getAge() {
return age;
}
public void setAge(double age) {
this.age = age;
}
}
And i have created a Store.java to store a record inside the mssql database.
import org.hibernate.Session; import org.hibernate.Transaction;
public class Store { public static void main(String[] args) { Session session=HibernateSessionFactory.getSession(); Transaction tx=session.beginTransaction(); Student studentObject=new Student(); StudentID studentIDObject=new StudentID(); studentIDObject.setSid(123456l); studentIDObject.setName("suresh"); studentIDObject.setAge(24d); studentObject.setId(studentIDObject); session.save(studentObject); tx.commit(); session.close(); } }
When i ran the above java file, i got following exception,
%%%% Error Creating SessionFactory %%%%
org.hibernate.AnnotationException: No identifier specified for entity: com.tbss.Student
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:672)
at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:546)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:291)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1333)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
at com.tbss.HibernateSessionFactory.
Please help me in resolving this problem. And please send the answer to following mail id