hibernate 3.5.6 annotation 双向一对多的配法。
今天用hibernate 3.5.6 annotation 双向一对多时候出现了异常,Invocation of init method failed; nested exception is java.lang.NoSuchMethodError:javax.persistence.OneToMany.orphanRemova()Z
上网一查发现是ejb3-persistence.jar的问题,解决方法是去掉ejb3-persistence.jar,使用hibernate-jpa-2.0-api-1.0.0.Final.jar
现贴出我自己的配法
一方(课程库):Course.class
Java代码
1.
2.import java.util.Date;
3.import java.util.HashSet;
4.import java.util.Set;
5.
6.import javax.persistence.Column;
7.import javax.persistence.Entity;
8.import javax.persistence.FetchType;
9.import javax.persistence.GeneratedValue;
10.import javax.persistence.Id;
11.import javax.persistence.OneToMany;
12.import javax.persistence.Table;
13.
14.import org.hibernate.annotations.Cache;
15.import org.hibernate.annotations.CacheConcurrencyStrategy;
16.import org.hibernate.annotations.GenericGenerator;
17.
18.import com.zhjy.core.domain.StringIdObject;
19.
20.@SuppressWarnings(“serial”)
21.@Entity
22.@Table(name = “COURSE”)
23.@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
24.public class Course {
25.
26. @Id
27. @GeneratedValue(generator = “system-uuid”)
28. @GenericGenerator(name = “system-uuid”, strategy = “uuid”)
29. @Column(length = 32)
30. protected String id; // hibernate的uuid机制,生成32为字符串
31.
32.
33. public String getId() {
34. return id;
35. }
36.
37. public void setId(String id) {
38. this.id = id;
39. }
40.
41. /**
42. * 课程编码
43. */
44. @Column(name = “COURSE_CODE”, length = 50)
45. private String courseCode;
46.
47. /**
48. * 课程主题
49. */
50. @Column(name = “COURSE_TOPIC”, length = 500)
51. private String courseTopic;
52. /**
53. * 创建时间
54. */
55. @Column(name = “CREATED_DATE”, length = 500)
56. private Date createdDate;
57. /**
58. * 创建部门
59. */
60. @Column(name = “CREATED_DEPT”, length = 500)
61. private String createdDept;
62.
63. /**
64. * 课程简介
65. */
66. @Column(name = “COURSE_DESCRIPTION”, length = 500)
67. private String courseDescription;
68. /**
69. * fetch = FetchType.EAGER 相当于lazy=”false”
70. */
71. @OneToMany(mappedBy=”course”, targetEntity=CourseManager.class,fetch = FetchType.EAGER)
72.
73. private Set
74.
75. public String getCourseCode() {
76. return courseCode;
77. }
78.
79. public void setCourseCode(String courseCode) {
80. this.courseCode = courseCode;
81. }
82.
83. public String getCourseTopic() {
84. return courseTopic;
85. }
86.
87. public void setCourseTopic(String courseTopic) {
88. this.courseTopic = courseTopic;
89. }
90.
91. public Date getCreatedDate() {
92. return createdDate;
93. }
94.
95. public void setCreatedDate(Date createdDate) {
96. this.createdDate = createdDate;
97. }
98.
99. public String getCreatedDept() {
100. return createdDept;
101. }
102.
103. public void setCreatedDept(String createdDept) {
104. this.createdDept = createdDept;
105. }
106.
107. public String getCreatedDescription() {
108. return courseDescription;
109. }
110.
111. public void setCreatedDescription(String courseDescription) {
112. this.courseDescription = courseDescription;
113. }
114.
115. public String getCourseDescription() {
116. return courseDescription;
117. }
118.
119. public void setCourseDescription(String courseDescription) {
120. this.courseDescription = courseDescription;
121. }
122.
123. public Set
124. return courseManagers;
125. }
126.
127. public void setCourseManagers(Set
128. this.courseManagers = courseManagers;
129. }
130.
131.}
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;
import com.zhjy.core.domain.StringIdObject;
@SuppressWarnings(“serial”)
@Entity
@Table(name = “COURSE”)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Course {
@Id
@GeneratedValue(generator = “system-uuid”)
@GenericGenerator(name = “system-uuid”, strategy = “uuid”)
@Column(length = 32)
protected String id; // hibernate的uuid机制,生成32为字符串
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
/**
* 课程编码
*/
@Column(name = “COURSE_CODE”, length = 50)
private String courseCode;
/**
* 课程主题
*/
@Column(name = “COURSE_TOPIC”, length = 500)
private String courseTopic;
/**
* 创建时间
*/
@Column(name = “CREATED_DATE”, length = 500)
private Date createdDate;
/**
* 创建部门
*/
@Column(name = “CREATED_DEPT”, length = 500)
private String createdDept;
/**
* 课程简介
*/
@Column(name = “COURSE_DESCRIPTION”, length = 500)
private String courseDescription;
/**
* fetch = FetchType.EAGER 相当于lazy=”false”
*/
@OneToMany(mappedBy=”course”, targetEntity=CourseManager.class,fetch = FetchType.EAGER)
private Set
public String getCourseCode() {
return courseCode;
}
public void setCourseCode(String courseCode) {
this.courseCode = courseCode;
}
public String getCourseTopic() {
return courseTopic;
}
public void setCourseTopic(String courseTopic) {
this.courseTopic = courseTopic;
}
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
public String getCreatedDept() {
return createdDept;
}
public void setCreatedDept(String createdDept) {
this.createdDept = createdDept;
}
public String getCreatedDescription() {
return courseDescription;
}
public void setCreatedDescription(String courseDescription) {
this.courseDescription = courseDescription;
}
public String getCourseDescription() {
return courseDescription;
}
public void setCourseDescription(String courseDescription) {
this.courseDescription = courseDescription;
}
public Set
return courseManagers;
}
public void setCourseManagers(Set
this.courseManagers = courseManagers;
}
}
多方(课程安排):CourseManager.class
Java代码
1.
2.
3.import java.sql.Date;
4.
5.import javax.persistence.Column;
6.import javax.persistence.Entity;
7.import javax.persistence.FetchType;
8.import javax.persistence.GeneratedValue;
9.import javax.persistence.Id;
10.import javax.persistence.ManyToOne;
11.import javax.persistence.Table;
12.
13.import org.hibernate.annotations.Cache;
14.import org.hibernate.annotations.CacheConcurrencyStrategy;
15.import org.hibernate.annotations.GenericGenerator;
16.
17.import com.zhjy.core.domain.StringIdObject;
18.
19.@SuppressWarnings(“serial”)
20.@Entity
21.@Table(name = “COURSE_MANAGER”)
22.@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
23.public class CourseManager {
24.
25. @Id
26. @GeneratedValue(generator = “system-uuid”)
27. @GenericGenerator(name = “system-uuid”, strategy = “uuid”)
28. @Column(length = 32)
29. protected String id; // hibernate的uuid机制,生成32为字符串
30.
31.
32. public String getId() {
33. return id;
34. }
35.
36. public void setId(String id) {
37. this.id = id;
38. }
39.
40. /**
41. * 课程编号
42. */
43. @ManyToOne(fetch = FetchType.EAGER)
44. private Course course;
45.
46.
47. /**
48. * 开课时间
49. */
50. @Column(name = “START_DATE”, length = 50)
51. private Date startDate;
52.
53. /**
54. * 报名人数
55. */
56.
57. @Column(name = “REGISTRATION_NUMBER”, length = 50)
58. private String registrationNumber;
59.
60. /**
61. * 实到人数
62. */
63. @Column(name = “ACTUAL_NUMBER”, length = 50)
64. private String actualNumber;
65.
66. /**
67. * 发布时间
68. */
69. @Column(name = “PUBLISH_DATE”, length = 50)
70. private Date publishDate;
71. /**
72. * 填报部门
73. */
74. @Column(name = “EDIT_DEPT”, length = 50)
75. private String editdept;
76.
77. public Course getCourse() {
78. return course;
79. }
80.
81. public void setCourse(Course course) {
82. this.course = course;
83. }
84.
85.
86.
87. public String getRegistrationNumber() {
88. return registrationNumber;
89. }
90.
91. public void setRegistrationNumber(String registrationNumber) {
92. this.registrationNumber = registrationNumber;
93. }
94.
95. public String getActualNumber() {
96. return actualNumber;
97. }
98.
99. public void setActualNumber(String actualNumber) {
100. this.actualNumber = actualNumber;
101. }
102.
103. public Date getPublishDate() {
104. return publishDate;
105. }
106.
107. public void setPublishDate(Date publishDate) {
108. this.publishDate = publishDate;
109. }
110.
111. public Date getStartDate() {
112. return startDate;
113. }
114.
115. public void setStartDate(Date startDate) {
116. this.startDate = startDate;
117. }
118.
119. public String getEditdept() {
120. return editdept;
121. }
122.
123. public void setEditdept(String editdept) {
124. this.editdept = editdept;
125. }
126.
127.}
import java.sql.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;
import com.zhjy.core.domain.StringIdObject;
@SuppressWarnings(“serial”)
@Entity
@Table(name = “COURSE_MANAGER”)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class CourseManager {
@Id
@GeneratedValue(generator = “system-uuid”)
@GenericGenerator(name = “system-uuid”, strategy = “uuid”)
@Column(length = 32)
protected String id; // hibernate的uuid机制,生成32为字符串
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
/**
* 课程编号
*/
@ManyToOne(fetch = FetchType.EAGER)
private Course course;
/**
* 开课时间
*/
@Column(name = “START_DATE”, length = 50)
private Date startDate;
/**
* 报名人数
*/
@Column(name = “REGISTRATION_NUMBER”, length = 50)
private String registrationNumber;
/**
* 实到人数
*/
@Column(name = “ACTUAL_NUMBER”, length = 50)
private String actualNumber;
/**
* 发布时间
*/
@Column(name = “PUBLISH_DATE”, length = 50)
private Date publishDate;
/**
* 填报部门
*/
@Column(name = “EDIT_DEPT”, length = 50)
private String editdept;
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
public String getRegistrationNumber() {
return registrationNumber;
}
public void setRegistrationNumber(String registrationNumber) {
this.registrationNumber = registrationNumber;
}
public String getActualNumber() {
return actualNumber;
}
public void setActualNumber(String actualNumber) {
this.actualNumber = actualNumber;
}
public Date getPublishDate() {
return publishDate;
}
public void setPublishDate(Date publishDate) {
this.publishDate = publishDate;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public String getEditdept() {
return editdept;
}
public void setEditdept(String editdept) {
this.editdept = editdept;
}
}
hibernate-jpa-2.0-api-1.0.0.Final.jar (98.5 KB)
下载次数: 5