双向OneToMany @LazyCollection(LazyCollectionOption.EXTRA)

时间:2015-07-14 13:10:27

标签: java hibernate lazy-loading

我在使用LazyCollection时遇到了问题。

此网站上的家伙lazy collection tutorial

  

在访问集合然后插入集合时,Hibernate不会实现集合元素(至少在我的Winery和Wine OneToMany关系映射示例中)

但它并不适合我。

以下是我的实体:

import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Entity
@Table(name="DOCUMENT_GENERATED")
public class DocumentDomainToGenerateImpl extends DomainObjectImpl<DocumentDomainToGenerate, Integer> implements DocumentDomainToGenerate{

/**
 * 
 */
private static final long serialVersionUID = -1297116626347796196L;
private String identificationKey;
private Identification identification;
private String documentGroup;
private String documentType;
private Server server;
private int statusOrder = 2;
private int numberOfAttempt;
private AuditData auditData;
private Date documentIssuanceDate;
private Date creationDate;
private DocumentStatus documentCurrentState;
private List<DocumentGeneratedHistoryStatus> documentGeneratedHistoryStatusList = new ArrayList<DocumentGeneratedHistoryStatus>(4);



public String getIdentificationKey() {
    return identificationKey;
}

public void setIdentificationKey(String identificationKey) {
    this.identificationKey = identificationKey;
}

@Embedded
@Target(AuditDataImpl.class)
public AuditData getAuditData() {
    return auditData;
}

public void setAuditData(AuditData auditData) {
    this.auditData = auditData;
}

@Embedded
@Target(IdentificationImpl.class)
@AttributeOverrides({
    @AttributeOverride(name = "businessId", column = @Column(name = "business_id_doc")),
    @AttributeOverride(name = "businessIdContextName", column = @Column(name = "business_id_ctx_doc")),
    @AttributeOverride(name = "identificationId", column = @Column(name = "id_doc")),
    @AttributeOverride(name = "identificationIdContextName", column = @Column(name = "id_ctx_name_doc")) })
public Identification getIdentification() {
    return identification;
}
public void setIdentification(Identification identification) {
    this.identification = identification;
}
public int getStatusOrder() {
    return statusOrder;
}
public void setStatusOrder(int statusOrder) {
    this.statusOrder = statusOrder;
}

public String getDocumentGroup() {
    return documentGroup;
}
public void setDocumentGroup(String documentGroup) {
    this.documentGroup = documentGroup;
}
public String getDocumentType() {
    return documentType;
}
public void setDocumentType(String documentType) {
    this.documentType = documentType;
}
public int getNumberOfAttempt() {
    return numberOfAttempt;
}
public void setNumberOfAttempt(int numberOfAttempt) {
    this.numberOfAttempt = numberOfAttempt;
}
@Embedded
@Target(ServerImpl.class)
public Server getServer() {
    return server;
}
public void setServer(Server server) {
    this.server = server;
}
@Embedded
@Target(DocumentStatusImpl.class)
public DocumentStatus getDocumentCurrentState() {
    return documentCurrentState;
}

public void setDocumentCurrentState(DocumentStatus documentCurrentState) {
    this.documentCurrentState = documentCurrentState;
}
@Temporal(TemporalType.TIMESTAMP)
public Date getDocumentIssuanceDate() {
    return documentIssuanceDate;
}

public void setDocumentIssuanceDate(Date documentIssuanceDate) {
    this.documentIssuanceDate = documentIssuanceDate;
}
@Temporal(TemporalType.TIMESTAMP)
public Date getCreationDate() {
    return creationDate;
}

public void setCreationDate(Date creationDate) {
    this.creationDate = creationDate;
}

@OneToMany(fetch = FetchType.LAZY, targetEntity = DocumentGeneratedHistoryStatusImpl.class)
@Cascade(CascadeType.ALL)
@JoinTable(name = "doc_gen_status", joinColumns = {@JoinColumn(name="doc_gen_id")}, inverseJoinColumns = {@JoinColumn(name = "doc_gen_stat_id")})
@IndexColumn(name="doc_gen_id_index", base = 1)
@LazyCollection(LazyCollectionOption.EXTRA)
public List<DocumentGeneratedHistoryStatus> getDocumentGeneratedHistoryStatusList() {
    return documentGeneratedHistoryStatusList;
}

public void setDocumentGeneratedHistoryStatusList(
        List<DocumentGeneratedHistoryStatus> documentGeneratedHistoryStatusList) {
    this.documentGeneratedHistoryStatusList = documentGeneratedHistoryStatusList;
}


@Override
public boolean equals(Object o) {
    if (this == o) return true;

    if (!(o instanceof DocumentDomainToGenerateImpl)) return false;

    DocumentDomainToGenerateImpl that = (DocumentDomainToGenerateImpl) o;

    return new EqualsBuilder()
            .appendSuper(super.equals(o))
            .append(getStatusOrder(), that.getStatusOrder())
            .append(getNumberOfAttempt(), that.getNumberOfAttempt())
            .append(getIdentificationKey(), that.getIdentificationKey())
            .append(getIdentification(), that.getIdentification())
            .append(getDocumentGroup(), that.getDocumentGroup())
            .append(getDocumentType(), that.getDocumentType())
            .append(getServer(), that.getServer())
            .append(getAuditData(), that.getAuditData())
            .append(getDocumentIssuanceDate(), that.getDocumentIssuanceDate())
            .append(getCreationDate(), that.getCreationDate())
            .append(getDocumentCurrentState(), that.getDocumentCurrentState())
            .isEquals();
}

@Override
public int hashCode() {
    return new HashCodeBuilder(17, 37)
            .appendSuper(super.hashCode())
            .append(getIdentificationKey())
            .append(getIdentification())
            .append(getDocumentGroup())
            .append(getDocumentType())
            .append(getServer())
            .append(getStatusOrder())
            .append(getNumberOfAttempt())
            .append(getAuditData())
            .append(getDocumentIssuanceDate())
            .append(getCreationDate())
            .append(getDocumentCurrentState())
            .toHashCode();
}

第二个实体:

import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="DOC_GEN_HIST_STATUS")
public class DocumentGeneratedHistoryStatusImpl extends DomainObjectImpl<DocumentGeneratedHistoryStatus, Integer> implements DocumentGeneratedHistoryStatus{

/**
 * 
 */
private static final long serialVersionUID = -8542541552897795870L;
private DocumentStatus documentStatus;
private DocumentDomainToGenerate documentDomainToGenerate;
@Embedded
@Target(DocumentStatusImpl.class)
public DocumentStatus getDocumentStatus() {
    return documentStatus;
}

public void setDocumentStatus(DocumentStatus documentStatus) {
    this.documentStatus = documentStatus;
}

@Override
@ManyToOne(targetEntity = DocumentDomainToGenerateImpl.class)
public DocumentDomainToGenerate getDocumentDomainToGenerate() {
    return documentDomainToGenerate;
}
@Override
public void setDocumentDomainToGenerate(DocumentDomainToGenerate documentDomainToGenerate) {
    this.documentDomainToGenerate = documentDomainToGenerate;
}

@Override
public boolean equals(Object o) {
    if (this == o) return true;

    if (!(o instanceof DocumentGeneratedHistoryStatusImpl)) return false;

    DocumentGeneratedHistoryStatusImpl that = (DocumentGeneratedHistoryStatusImpl) o;

    return new EqualsBuilder()
            .appendSuper(super.equals(o))
            .append(getDocumentStatus(), that.getDocumentStatus())
            .isEquals();
}

@Override
public int hashCode() {
    return new HashCodeBuilder(11, 61)
            .appendSuper(super.hashCode())
            .append(getDocumentStatus())
            .toHashCode();
}

}

当我正在加载DocumentDomainToGenerate时,执行以下sql:

select
    this_.id as id25_0_,
    this_.business_id_app as business2_25_0_,
    this_.business_id_ctx_app as business3_25_0_,
    this_.id_app as id4_25_0_,
    this_.id_ctx_name_app as id5_25_0_,
    this_.moduleName as moduleName25_0_,
    this_.business_id_app_role as business7_25_0_,
    this_.business_id_ctx_app_role as business8_25_0_,
    this_.id_app_role as id9_25_0_,
    this_.id_ctx_name_app_role as id10_25_0_,
    this_.business_id_app_user as business11_25_0_,
    this_.business_id_ctx_app_user as business12_25_0_,
    this_.id_app_user as id13_25_0_,
    this_.id_ctx_name_app_user as id14_25_0_,
    this_.creationDate as creatio15_25_0_,
    this_.doc_stat_app_b_id as doc16_25_0_,
    this_.doc_stat_app_b_id_ctx as doc17_25_0_,
    this_.doc_stat_app_id as doc18_25_0_,
    this_.doc_stat_app_id_ctx as doc19_25_0_,
    this_.doc_stat_app_module_name as doc20_25_0_,
    this_.doc_stat_role_b_id as doc21_25_0_,
    this_.doc_stat_role_b_id_ctx as doc22_25_0_,
    this_.doc_stat_role_id as doc23_25_0_,
    this_.doc_stat_role_id_ctx as doc24_25_0_,
    this_.doc_stat_user_b_id as doc25_25_0_,
    this_.doc_stat_user_b_id_ctx as doc26_25_0_,
    this_.doc_stat_user_id as doc27_25_0_,
    this_.doc_stat_user_id_ctx as doc28_25_0_,
    this_.description as descrip29_25_0_,
    this_.effectiveDateTime as effecti30_25_0_,
    this_.orderToSend as orderTo31_25_0_,
    this_.sended as sended25_0_,
    this_.statusCode_id as statusCode45_25_0_,
    this_.documentGroup as documen33_25_0_,
    this_.documentIssuanceDate as documen34_25_0_,
    this_.documentType as documen35_25_0_,
    this_.business_id_doc as business36_25_0_,
    this_.business_id_ctx_doc as business37_25_0_,
    this_.id_doc as id38_25_0_,
    this_.id_ctx_name_doc as id39_25_0_,
    this_.identificationKey as identif40_25_0_,
    this_.numberOfAttempt as numberO41_25_0_,
    this_.ip as ip25_0_,
    this_.nodePid as nodePid25_0_,
    this_.statusOrder as statusO44_25_0_ 
from
    DOCUMENT_GENERATED this_ 
where
    this_.id = ?

接下来我需要将新的DocumentGeneratedHistoryStatus添加到DocumentDomaintToGenerate的列表中,当我执行此

documentGeneratedHistoryStatus.setDocumentDomainToGenerate(documentDomainToGenerate);
documentDomainToGenerate.getDocumentGeneratedHistoryStatusList().add(documentGeneratedHistoryStatus);

执行此sql

select
    documentge0_.doc_gen_id as doc1_25_2_,
    documentge0_.doc_gen_stat_id as doc2_2_,
    documentge0_.doc_gen_id_index as doc3_2_,
    documentge1_.id as id10_0_,
    documentge1_.documentDomainToGenerate_id as documen19_10_0_,
    documentge1_.doc_stat_app_b_id as doc2_10_0_,
    documentge1_.doc_stat_app_b_id_ctx as doc3_10_0_,
    documentge1_.doc_stat_app_id as doc4_10_0_,
    documentge1_.doc_stat_app_id_ctx as doc5_10_0_,
    documentge1_.doc_stat_app_module_name as doc6_10_0_,
    documentge1_.doc_stat_role_b_id as doc7_10_0_,
    documentge1_.doc_stat_role_b_id_ctx as doc8_10_0_,
    documentge1_.doc_stat_role_id as doc9_10_0_,
    documentge1_.doc_stat_role_id_ctx as doc10_10_0_,
    documentge1_.doc_stat_user_b_id as doc11_10_0_,
    documentge1_.doc_stat_user_b_id_ctx as doc12_10_0_,
    documentge1_.doc_stat_user_id as doc13_10_0_,
    documentge1_.doc_stat_user_id_ctx as doc14_10_0_,
    documentge1_.description as descrip15_10_0_,
    documentge1_.effectiveDateTime as effecti16_10_0_,
    documentge1_.orderToSend as orderTo17_10_0_,
    documentge1_.sended as sended10_0_,
    documentge1_.statusCode_id as statusCode20_10_0_,
    documentdo2_.id as id25_1_,
    documentdo2_.business_id_app as business2_25_1_,
    documentdo2_.business_id_ctx_app as business3_25_1_,
    documentdo2_.id_app as id4_25_1_,
    documentdo2_.id_ctx_name_app as id5_25_1_,
    documentdo2_.moduleName as moduleName25_1_,
    documentdo2_.business_id_app_role as business7_25_1_,
    documentdo2_.business_id_ctx_app_role as business8_25_1_,
    documentdo2_.id_app_role as id9_25_1_,
    documentdo2_.id_ctx_name_app_role as id10_25_1_,
    documentdo2_.business_id_app_user as business11_25_1_,
    documentdo2_.business_id_ctx_app_user as business12_25_1_,
    documentdo2_.id_app_user as id13_25_1_,
    documentdo2_.id_ctx_name_app_user as id14_25_1_,
    documentdo2_.creationDate as creatio15_25_1_,
    documentdo2_.doc_stat_app_b_id as doc16_25_1_,
    documentdo2_.doc_stat_app_b_id_ctx as doc17_25_1_,
    documentdo2_.doc_stat_app_id as doc18_25_1_,
    documentdo2_.doc_stat_app_id_ctx as doc19_25_1_,
    documentdo2_.doc_stat_app_module_name as doc20_25_1_,
    documentdo2_.doc_stat_role_b_id as doc21_25_1_,
    documentdo2_.doc_stat_role_b_id_ctx as doc22_25_1_,
    documentdo2_.doc_stat_role_id as doc23_25_1_,
    documentdo2_.doc_stat_role_id_ctx as doc24_25_1_,
    documentdo2_.doc_stat_user_b_id as doc25_25_1_,
    documentdo2_.doc_stat_user_b_id_ctx as doc26_25_1_,
    documentdo2_.doc_stat_user_id as doc27_25_1_,
    documentdo2_.doc_stat_user_id_ctx as doc28_25_1_,
    documentdo2_.description as descrip29_25_1_,
    documentdo2_.effectiveDateTime as effecti30_25_1_,
    documentdo2_.orderToSend as orderTo31_25_1_,
    documentdo2_.sended as sended25_1_,
    documentdo2_.statusCode_id as statusCode45_25_1_,
    documentdo2_.documentGroup as documen33_25_1_,
    documentdo2_.documentIssuanceDate as documen34_25_1_,
    documentdo2_.documentType as documen35_25_1_,
    documentdo2_.business_id_doc as business36_25_1_,
    documentdo2_.business_id_ctx_doc as business37_25_1_,
    documentdo2_.id_doc as id38_25_1_,
    documentdo2_.id_ctx_name_doc as id39_25_1_,
    documentdo2_.identificationKey as identif40_25_1_,
    documentdo2_.numberOfAttempt as numberO41_25_1_,
    documentdo2_.ip as ip25_1_,
    documentdo2_.nodePid as nodePid25_1_,
    documentdo2_.statusOrder as statusO44_25_1_ 
from
    doc_gen_status documentge0_ 
inner join
    DOC_GEN_HIST_STATUS documentge1_ 
        on documentge0_.doc_gen_stat_id=documentge1_.id 
left outer join
    DOCUMENT_GENERATED documentdo2_ 
        on documentge1_.documentDomainToGenerate_id=documentdo2_.id 
where
    documentge0_.doc_gen_id=?

当我尝试将新项目添加到收藏中时,是否有任何选项可以取消整个收藏?我做错了什么?

0 个答案:

没有答案