JPA,多对多关系,删除所有先前的关系并进入新的关系

时间:2012-04-25 14:51:41

标签: java java-ee jpa ejb-3.0 jpa-2.0

这里我在尝试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>

等待回复......

3 个答案:

答案 0 :(得分:1)

我可能错了,但我认为这是正常的,当你进行插入时,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,并在映射数据中配置你想要级联的操作。

答案 1 :(得分:0)

将新课程添加到现有列表中:

Collection<Student> moreStudents = ...

course = em.find(CourseEntity.class,courseId);
course.getStudentList().addAll(moreStudents);

答案 2 :(得分:0)

我没有覆盖整个关系列表,而是将新实体添加到原始列表中。但是,Hibernate仍然会删除我以前的所有关系。

根据这篇文章: https://vladmihalcea.com/the-best-way-to-use-the-manytomany-annotation-with-jpa-and-hibernate/

这似乎是Hibernate的当前行为,如果我们不想这样做,我们必须首先正确实现我们实体的hashCode()equals()方法,以及使用Set来建模ManyToMany关系。