Hibernate needs mandatory @Id column in mapping class

It looks very simple and silly, actually i faced this issue while working in our project and frighted with this for an hour.

My requirement was to create mapping class for an oracle view. As we know there is no primary key for the view, so what can i give as an Id for the view. So i created a persistence class for that view and tried to get data from that view.

Example :-

@Entity
@Table(name="VIEW_STUDENT_REPORT")
public class ViewStudentReport implements Serializable {
private static final long serialVersionUID = 1L;

@Column(name="STUDENT_ID")
private String studentId;

       @Column(name="STUDENT_NAME")
private String studentName;

        public String getStudentId() {
return studentId;
}

public void setStudentId(String studentId) {
this.studentId= studentId;
}

       public String getStudentName() {
return studentName;
}

public void setStudentName(String studentName) {
this.studentName= studentName;
}
}

In the above example, we will not find any @Id annotation. Because of that, it was not binding this class.
After that I came to know that, relational data model should have primary key. If no primary key, then it will be considered as broken data model.

Solution:-
        @Id//This solves the problem.
@Column(name="STUDENT_ID")
private String studentId;


Thanks,
Harish HB


Comments