为什么在休眠中使用Set in OneToMany Mapping

时间:2018-06-05 15:09:07

标签: java hibernate collections hibernate-onetomany

我有两个表与一对多的关系。我想通过更改主键来获取这些记录并插入到具有相同表的另一个数据库中。

我的应用实体类

@Entity
@Table(name = "EM_APPLICATION")
public class ApplicationTable  {

    @Id
    private int APPLICATION_ID;
    @Id
    private String CUSTOMER_ID;
    private String LAST_NAME;
    private String FIRST_NAME;

@OneToMany( fetch = FetchType.EAGER,cascade = CascadeType.ALL)
@JoinColumns({ @JoinColumn(name = "CUSTOMER_ID", referencedColumnName = "CUSTOMER_ID"),
        @JoinColumn(name = "APPLICATION_ID", referencedColumnName = "APPLICATION_ID") })
private Set<AddressTable> address;

//Getters and setters
}

地址实体类..

@Entity
@Table(name="EM_APPL_ADDRESS")
public class AddressTable{
    @Id
    private int APPLICATION_ID;
    @Id
    private String CUSTOMER_ID;
    @Id
    private String ADDRESS_TYPE;
    //Getters and setters
}

我必须使用hibernate执行一种从DB获取记录的方法:

public void execute(String applId, String customerId) {

    Session session = HibernateQAUtil.openSession();
    Transaction tx = session.beginTransaction();

    String hql = "FROM  ApplicationTable  WHERE CUSTOMER_ID =:CUSTOMER_ID AND APPLICATION_ID =:APPLICATION_ID";
    Query query = session.createQuery(hql);
    query.setParameter("CUSTOMER_ID", customerId);
    query.setParameter("APPLICATION_ID", Integer.parseInt(applId));

    List<ApplicationTable> list = query.list();
    tx.commit();
    session.close();
    ApplicationTable applVO = list.get(0);

    insertApplication(applVO ); 
}

获取记录后,我正在更改地址表中的APPLICATION_IDCUSTOMER_ID和其他一些列以及插入另一个数据库之后。

private void insertApplication(ApplicationTable emApplVO) {

    applVO.setAPPLICATION_ID(123456);
    applVO.setCUSTOMER_ID("88888888");

    Set<AddressTable> addressSet = emApplVO.getAddress();
    for (AddressTable address : addressSet) {
        address.setAPPLICATION_ID(123456);  
        address.setCUSTOMER_ID("88888888");
        address.setZIP(500032);
    }

    Session session1 = HibernateUtil.openSession();
    Transaction beginTransaction = session1.beginTransaction();

    session1.save(emApplVO);
    beginTransaction.commit();
    session1.close();
}

控制台日志中的Hibernate查询是...(下面提到的查询太大,所以只在某种程度上复制..)

 Hibernate: select em_applica0_.CUSTOMER_ID as CUSTOMER1_0_,em_applica0_.APPLICATION_ID as APPLICAT2_0_,em_applica0_.ARCHIVE_IND as ARCHIVE8_0_ where em_applica0_.CUSTOMER_ID=? and em_applica0_.APPLICATION_ID=?
 Hibernate: select address0_.CUSTOMER_ID as CUSTOMER1_0_1_, address0_.APPLICATION_ID as APPLICAT2_0_1_, address0_.ADDRESS_TYPE as ADDRESS3_1_0_ where em_applica0_.CUSTOMER_ID=? and em_applica0_.APPLICATION_ID=?

 Hibernate: insert into EM_APPLICATION (CUSTOMER_ID, APPLICATION_ID, APPLICATION_NBR, APPLICATION_STATUS, APPLICATION_TYPE) values (?, ?, ?, ?)
 Hibernate: insert into EM_APPL_ADDRESS (CUSTOMER_ID, APPLICATION_ID, ADDRESS_TYPE) values (?, ?, ?)

问题1 :在insert方法中,我已经为addresSet分配了地址并在addresSet中进行了一些更改,在进行这些更改之后,我没有将addressSet分配给applVO(即没有编写applVO。 setAddress(addresSet))但是它将带有更新值的记录插入到Address表中。这里发生了什么?

当我将insertApplication(ApplicationTable emApplVO)方法中的代码更改为

private void insertApplication(ApplicationTable emApplVO) {

    applVO.setAPPLICATION_ID(123456);
    applVO.setCUSTOMER_ID("88888888");

    Set<AddressTable> addressSet = emApplVO.getAddress();
    Set<AddressTable> newAddressSet = new HashSet<AddressTable>();
    for (AddressTable address : newAddressSet) {

        address.setAPPLICATION_ID(emApplVO.getAPPLICATION_ID());
        address.setCUSTOMER_ID(emApplVO.getCUSTOMER_ID());
        address.setZIP(500032);
        newAddressSet.add(address);
    }
    emApplVO.setAddress(null);
    emApplVO.setAddress(newAddressSet);

    Session session1 = HibernateUtil.openSession();
    Transaction beginTransaction = session1.beginTransaction();

    session1.save(emApplVO);
    beginTransaction.commit();
    session1.close();
 }

控制台日志中的Hibernate查询是......它还执行更新...

Hibernate: select em_applica0_.CUSTOMER_ID as CUSTOMER1_0_,em_applica0_.APPLICATION_ID as APPLICAT2_0_,em_applica0_.ARCHIVE_IND as ARCHIVE8_0_ where em_applica0_.CUSTOMER_ID=? and em_applica0_.APPLICATION_ID=?
 Hibernate: select address0_.CUSTOMER_ID as CUSTOMER1_0_1_, address0_.APPLICATION_ID as APPLICAT2_0_1_, address0_.ADDRESS_TYPE as ADDRESS3_1_0_ where em_applica0_.CUSTOMER_ID=? and em_applica0_.APPLICATION_ID=?

 Hibernate: insert into EM_APPLICATION (CUSTOMER_ID, APPLICATION_ID, APPLICATION_NBR, APPLICATION_STATUS, APPLICATION_TYPE) values (?, ?, ?, ?)
 Hibernate: insert into EM_APPL_ADDRESS (CUSTOMER_ID, APPLICATION_ID, ADDRESS_TYPE) values (?, ?, ?)
update EM_APPL_ADDRESS set CUSTOMER_ID=?, APPLICATION_ID=? where CUSTOMER_ID=? and APPLICATION_ID=? and ADDRESS_TYPE=?

问题2 :为什么要执行更新查询?

问题3 :在使用List<AddressTable>代替Set<AddressTable>时,我遇到了一些错误。有什么区别?

0 个答案:

没有答案