7

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

等待回复....

4

4 回答 4

3

我可能错了,但我认为这是正常的,当您进行插入时,Hibernate 首先会从关联表中删除所有记录。

这就是原因:当使用 x 对多关联(基本上,通过集合映射的关联)时,Hibernate 的持久性上下文将根据集合的标识符执行脏检查。

让我们从 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 并在映射数据中配置你想要级联的操作。

于 2012-04-26T21:48:20.667 回答
0

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

Collection<Student> moreStudents = ...

course = em.find(CourseEntity.class,courseId);
course.getStudentList().addAll(moreStudents);
于 2012-04-26T22:01:57.430 回答
0

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

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

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

于 2017-12-11T10:58:48.110 回答
0

我面临着类似的挑战。我提供了类似的场景,希望它可能对某些人有所帮助。

我正在使用产品和订单来解释问题并使用单向多对多映射

@Entity
@Table(name="Product")
public class Product {

    @Id
    @Column(name = "id")
    private Long id;

    
    @Column(name = "product")
    private String product;

    @ManyToMany(cascade = CascadeType.PERSIST,fetch= FetchType.EAGER)
    @JoinTable(name = "product_order_mapping", joinColumns = { @JoinColumn(name = "product_id") }, inverseJoinColumns = {
            @JoinColumn(name = "order_id") })
    @JsonProperty("orders")
    private Set<Order> orders =new HashSet<Order>();

    //setters and getters


}

@Table(name="Order")
public class Order {

    @Id
    @Column(name = "id")
    private Long id;
        
    @Column(name = "order")
    private String order;

    //setters and getters
    //equals and hashcode

}

实现逻辑

productList 和 OrderFunctions 是一些包含产品列表和订单详细信息的列表

for (Product pEntity : productList) {

OrderFunctions = this.getAllLineFunctions(product.getId(), cmEntity.getId())
                        .getBody();
Set<Order> oList = new HashSet<>();

for (OrderFunction orderFn : OrderFunctions) {
Order orderEntity = new Order();
orderEntity.setId(lfs.getOrderFunction_id());
orderEntity.setStageGroup(lfs.getOrderFunctionOrder());
oList.add(sgEntity);
}

orderRepository.saveAll(oList);
pEntity.setOrders(oList);
productRepository.save(pEntity);
}
}

为了理解重复映射让我们举个例子

假设产品在公司下,现在公司有多个产品,产品有多个订单

现在让我们举一个在表中存储数据的实际示例

set1--> ID 为 c1 的公司有 ID 为 p1,p2,p3 的产品 p1 有订单 o1,o2,o3 产品 p2 有订单 o1,o2 产品 p3 有订单 o2,o3,o4

set2--> ID 为 c2 的公司有 ID 为 p1 的产品,p3 产品 p1 有订单 o1,o2 产品 p3 有订单 o2

现在当保存 set1 表 product_order_mapping 看起来像

product_id  order_id
---------------------
   p1         o1
   p1         o2
   p1         o3
   p2         o1
   p2         o2
   p3         o2
   p3         o3
   p3         o4

但是当在 set1 表 product_order_mapping 之后保存 set2 时会看起来像

product_id  order_id
---------------------
   p1         o1
   p1         o2
   p2         o1
   p2         o2
   p3         o2

我们可以看到保存 set2 之前和保存 set2 之后的区别 p1->o3 、 p3->o3 和 p3->o4 丢失了

丢失映射的原因

当我们尝试存储 set2 时,我们尝试覆盖重复条目,例如 p1->o1,p1->o2,它们在映射表中已经可用所以在添加这些重复条目之前,所有与 p1 相关的映射都被删除,然后 p1 ->o1,p1->o2 已添加

所以我们丢失了一些以前的条目

解决方案

避免从 set2 添加重复条目,例如 p1->o1,p1->o2,因为它们已经存在于映射表中

for (Company cEntity : companyList) {
for (Product pEntity : productList) {

OrderFunctions = this.getAllOrderFunctions(cEntity.getId(), pEntity.getId());
                        
Set<Order> oList = new HashSet<>();

for (OrderFunction orderFn : OrderFunctions) {
Order orderEntity = new Order();
orderEntity.setId(lfs.getOrderFunction_id());
orderEntity.setStageGroup(lfs.getOrderFunctionOrder());
oList.add(sgEntity);
}

Set<Order> collectSG = pEntity.getOrders().stream().filter(o->oList.contains(o)).collect(Collectors.toSet());
oList.removeAll(collectSG);
if(oList.size()>0) {
orderRepository.saveAll(oList);
                
pEntity.setOrders(oList);
productRepository.save(pEntity);
}
}
}
}
于 2021-09-09T15:53:41.867 回答