今天用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
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<CourseManager> courseManagers=new HashSet<CourseManager>();
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<CourseManager> getCourseManagers() {
return courseManagers;
}
public void setCourseManagers(Set<CourseManager> courseManagers) {
this.courseManagers = courseManagers;
}
}
多方(课程安排):CourseManager.class
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;
}
}