设置实体时获取hibernate lazyinitializationexception

vq8itlhq  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(155)

我们有一个连续运行的线程,它执行一些数据库操作。线程的代码段如下所示。

public class DbWriter implements Runnable {

    @Override
    public void run() {
        while(true) {
            IrnResponse irnResponse = null;
            try {
                irnResponse = irnResponseQueue.take();
                process(irnResponse);

            } catch (Exception e) {

            }
        }
    }

     @Transactional(value="primaryTransactionManager")
     private void process(IrnResponse irnResponse) throws Exception {
           //database get operations 

          //we call a third party API to get some data
          GenerateIrnResponse nicResponse = irnResponse.getResponse();

          nicResponse.getErrorDetails().forEach(e -> 
                               dbmh.addErrorWarning(null,"IRN_ERROR",e.getErrorMessage()));
     }
}

在dbmh.adderrorwarning(..)中,我们得到一个lazyinitializationexception异常。下面给出了相关的刀。

@Entity
@Table(name="tbl_inv_details_history")
public class InvoiceDetailsHistoryDbModel {
    @Id
    @Column(name = "id", unique = true, nullable = false)
    private long id;

    .....
    ......

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "invoice_details_id_fk1") 
    @OrderColumn(name = "idx")
    public List<InvoiceErrorHistoryDbModel> errors;

    public void addErrorWarning(String id, String type, String errorMsg) {
        InvoiceErrorHistoryDbModel invoiceErrorDbModel = new InvoiceErrorHistoryDbModel(id, type, 
                                                                                           errorMsg);
        if(errors == null)
            errors = new ArrayList<InvoiceErrorHistoryDbModel>();
        errors.add(invoiceErrorDbModel);
    }
}

另一个Map实体的片段如下所示。

@Entity
@Table(name="tbl_invoice_error_details_history")
public class InvoiceErrorHistoryDbModel {

    @Id
    @Column(name = "error_id", unique = true, nullable = false)
    private long errorId;

    @Column(name = "field_name")
    private String fieldName;

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

    @Column(name = "description")
    @Lob
    private String description;

    @Column(name = "invoice_details_id_fk1")
    private Long invoiceIdFk;

是否与@joincolumn注解有关?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题