我正在为我的项目多次使用Hibernate。当我浏览“http://localhost:8080/subject/list”显示列表主题,但后来我看到了这个错误:
信息:HHH000327:执行加载命令时出错: org.hibernate.ObjectNotFoundExc eption:没有给定的行 标识符存在:[edu.java.spring.model.Student#2101]
此处存档Student.java
@XmlRootElement(name="item")
@Entity
@Table(name = "student",uniqueConstraints={@UniqueConstraint(columnNames="id")})
public class Student {
// @NotBlank
// @Size(min=2,max=100,message ="Age value is invalid")
private String name;
private int id;
// @Range(min=1,max=150)
private int age;
@XmlAttribute
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement
@Column(name = "name", nullable = false, length = 200)
public String getName() {
return name;
}
此处提交Subject.java文件
public class Subject {
// @Id
// @GeneratedValue(strategy = GenerationType.IDENTITY)
// @Column(name = "id", unique = true, nullable = false)
public int id;
public String title;
public Student student;
public int score;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
// @Column(name = "title", nullable = false, length = 200)
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
// @Column(name = "student", nullable = false)
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
// @Column(name = "score", nullable = false)
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
此处提交Subject.hbm.xml
<hibernate-mapping>
<class name="edu.java.spring.model.Subject" table="subject">
<meta attribute="class-description">
This class contains the subject detail
</meta>
<id name = "id" type = "int" column="id">
<generator class="native"/>
</id>
<property name="title" column="title" type="string"/>
<many-to-one name="student" column="student"
class="edu.java.spring.model.Student" not-null="true" lazy="false"/>
<property name="score" column="score" type="int"/>
</class>
</hibernate-mapping>
此处提交SubjectController.java
@RequestMapping(value="subject/list",method= RequestMethod.GET)
public ModelAndView listSubjects(){
ModelAndView model = new ModelAndView("SubjectList");
model.addObject("subjects", subjectDao.listSubject());
// System.out.println("anh yeu em");
return model;