[英]JPA , many-to-many relation, deleting all previous relations and entering the new relations
在這里,我正在嘗試JPA中的多對多關系,我創建了表“tblcourse”和“tblStudent”,學生可以注冊許多課程,
create table tblcourse(
id integer primary key,
name varchar(100),
duration integer
);
create table tblcourseStudent(
studentid integer references tblstudent(studentId),
courseId integer references tblcourse(id),
constraint pk_composit_cs primary key(studentid,courseId)
)
Create table tblStudent(
studentId integer primary key,
……..
….
);
上述關系的JPA表示如下,這是StudentEntity.java的代碼,
@Entity
@Table(name="TBLSTUDENT")
public class StudentEntity implements Serializable{
private static final long serialVersionUID = 100034222342L;
@Id
@Column(name="STUDENTID")
private Integer studentId;
@Column(name="STUDENTNAME")
private String studentName;
@Column(name="CONTACTNO")
private String contactNumber;
@Embedded
private StudentAddress address;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="DEPTID")
private DeptEntity deptEntity;
@ManyToMany(fetch=FetchType.LAZY)
@JoinTable(name="tblcourseStudent",
joinColumns=@JoinColumn(name="studentid"),
inverseJoinColumns=@JoinColumn(name="courseId"))
private List<CourseEntity> courseList;
....
.....
.....
}
這是CourseEntity.java的代碼,
@Entity
@Table(name="TBLCOURSE")
public class CourseEntity implements Serializable{
public CourseEntity(){
}
public CourseEntity(Integer courseId,String courseName,Integer courseDuration){
this.courseId = courseId;
this.courseName = courseName;
this.courseDuration = courseDuration;
}
/**
*
*/
private static final long serialVersionUID = -2192479237310864341L;
@Id
@Column(name="ID")
private Integer courseId;
@Column(name="NAME")
private String courseName;
@Column(name="DURATION")
private Integer courseDuration;
@ManyToMany(fetch=FetchType.LAZY)
@JoinTable(name="tblcourseStudent",
joinColumns=@JoinColumn(name="courseId"),
inverseJoinColumns=@JoinColumn(name="studentid"))
private List<StudentEntity> studentList;
.........
}
現在,當我嘗試通過StudentEntity.java插入課程時,在后端觸發的SQL查詢是
delete
from
tblcourseStudent
where
studentid=?
insert
into
tblcourseStudent
(studentid, courseId)
values
(?, ?)
insert
into
tblcourseStudent
(studentid, courseId)
values
(?, ?)
而且,當我嘗試通過CourseEntity.java插入學生時,觸發的SQL查詢如下,
delete
from
tblcourseStudent
where
courseId=?
insert
into
tblcourseStudent
(courseId, studentid)
values
(?, ?)
在我的兩種情況下,記錄都被刪除,並且插入了新的映射。 因此,如果我為學生插入課程,首先將從第三個表中刪除學生的所有前期課程,並輸入新課程,
所以,我的問題是,如果我不想刪除舊課程並為學生添加新課程我該如何實現,即我想保留舊關系,
天氣我必須以編程方式實現這一點,或者我已經更改了注釋,等待回復
當我們將一個學生映射到多個課程時,會調用StudentServiceBean.java中編寫的代碼和“mapStudentToCourses”方法
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class StudentServiceBean implements StudentService{
@PersistenceContext(unitName="forPractise")
private EntityManager entityMgr;
@Resource
private SessionContext sessionContext;
@EJB
private DeptService deptService;
..........
......
...
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void mapStudentToCourses(Integer studentId,String courseIdList) throws Exception{
List<CourseEntity> courseList = null;
StudentEntity studentEntity = null;
TypedQuery<CourseEntity> courseQuery = null;
String query = "select c from CourseEntity c where c.courseId in ("+courseIdList+")";
try{
courseQuery = entityMgr.createQuery(query,CourseEntity.class);
courseList = courseQuery.getResultList();
studentEntity = entityMgr.find(StudentEntity.class, studentId);
studentEntity.setCourseList(courseList);
entityMgr.merge(studentEntity);
}catch(Exception e){
sessionContext.setRollbackOnly();
throw e;
}
}
這是將一個課程映射到多個學生及其CourseServiceBean.java時的代碼
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class CourseServiceBean implements CourseService{
@PersistenceContext(name="forPractise")
private EntityManager em;
@Resource
private SessionContext sessionCtx;
private Map<Integer, String> durationCode = null;
@EJB
private StudentService studentService;
........
......
...
@Override
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void mapCourseToStudents(Integer courseId,String studentIdList) throws Exception{
List<StudentEntity> studentEntityList = null;
TypedQuery<StudentEntity> studentQuery = null;
String query = "select s from StudentEntity s where s.studentId IN ("+studentIdList+")";
CourseEntity courseEntity = null;
try{
studentQuery = em.createQuery(query, StudentEntity.class);
studentEntityList = studentQuery.getResultList();
courseEntity = em.find(CourseEntity.class,courseId);
courseEntity.setStudentList(studentEntityList);
em.merge(courseEntity);
}catch(Exception e){
sessionCtx.setRollbackOnly();
throw e;
}
}
}
這是我的persistence.xml文件,
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="forPractise" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/app</jta-data-source>
<class>com.entity.StudentEntity</class>
<class>com.entity.DeptEntity</class>
<class>com.entity.CourseEntity</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
等待回復....
我可能錯了,但我認為這是正常的,當你進行插入時,Hibernate第一次刪除關聯表中的所有記錄。
這就是原因:當使用x-to-many關聯(基本上是通過Collection映射的關聯)時,Hibernate的持久化上下文將基於Collection的標識符執行臟檢查。
我們從CourseServiceBean類中獲取mapCourseToStudents()方法:
...
studentQuery = em.createQuery(query, StudentEntity.class);
studentEntityList = studentQuery.getResultList();
courseEntity = em.find(CourseEntity.class,courseId);
courseEntity.setStudentList(studentEntityList); // replacing the previous Collection by the one you retrieved by querying the DB !!!
em.merge(courseEntity);
...
如果您確實希望避免Hibernate首先執行delete語句,則應該向Collection添加/刪除項目,而不是分配新的Collection,並在映射數據中配置要級聯的操作。
在現有列表中添加新課程:
Collection<Student> moreStudents = ...
course = em.find(CourseEntity.class,courseId);
course.getStudentList().addAll(moreStudents);
我沒有覆蓋整個關系列表,而是將新實體添加到原始列表中。 但是,Hibernate仍然會刪除我以前的所有關系。
根據這篇文章: https : //vladmihalcea.com/the-best-way-to-use-the-manytomany-annotation-with-jpa-and-hibernate/
這似乎是Hibernate的當前行為,如果我們不希望這樣,我們必須首先正確實現我們的實體的hashCode()
和equals()
方法,並使用Set來模擬ManyToMany關系。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.