0

我对弹簧数据很陌生,并试图分析一段代码。就这样吧。

BaseRepository.java

import java.io.Serializable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.NoRepositoryBean;

/**
 * Base Repository is a generic repository
 * 
 * */
@NoRepositoryBean
public interface BaseRepository<T, ID extends Serializable> extends JpaRepository<T, ID>, JpaSpecificationExecutor<T> {}

SectionChainView.java

@Entity
@Table(name = "SECTION_CHAIN_VIEW", schema = "RPM")
public class SectionChainView implements BaseEntity {


public SectionChainView() {}

public SectionChainView(SectionChainViewId id) {
    this.id = id;
}

public SectionChainViewId getId() {
    return id;
}

public void setId(SectionChainViewId id) {
    this.id = id;
}


@EmbeddedId
   @AttributeOverrides( {
       @AttributeOverride(name="contractId", column=@Column(name="CONTRACT_ID",nullable=false, length=5) ), 
       @AttributeOverride(name="sectionId", column=@Column(name="SECTION_ID", nullable=false, length=1) ),
       @AttributeOverride(name="chainId", column=@Column(name="CHAIN_ID", nullable=false, length=4) ),
       @AttributeOverride(name="name", column=@Column(name="NAME") )
       } )
private SectionChainViewId id;
}

SectionChainViewId.java

@Embeddable
public class SectionChainViewId implements BaseEntity {
private static final long serialVersionUID = 1L;

public SectionChainViewId() {}

public String getContractId() {
    return contractId;
}
public void setContractId(String contractId) {
    this.contractId = contractId;
}
public String getSectionId() {
    return sectionId;
}
public void setSectionId(String sectionId) {
    this.sectionId = sectionId;
}
public String getChainId() {
    return chainId;
}
public void setChainId(String chainId) {
    this.chainId = chainId;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@Column(name="CONTRACT_ID", nullable=false, length=5)
private String contractId;
@Column(name="SECTION_ID", nullable=false, length=1)
private String sectionId;
@Column(name="CHAIN_ID", nullable=false, length=4)
private String chainId;
@Column(name = "NAME")
private String name;

public boolean equals(Object other) {
    if ( (this == other ) ) return true;
     if ( (other == null ) ) return false;
     if ( !(other instanceof SectionChainViewId) ) return false;
     SectionChainViewId castOther = ( SectionChainViewId ) other; 

     return ( (this.getContractId()==castOther.getContractId()) || (  this.getContractId()!=null && castOther.getContractId()!=null &&  this.getContractId().equals(castOther.getContractId()) ) )
&& ( (this.getSectionId()==castOther.getSectionId()) || ( this.getSectionId()!=null &&  castOther.getSectionId()!=null && this.getSectionId().equals(castOther.getSectionId()) ) 
&& ( (this.getChainId()==castOther.getChainId()) || ( this.getChainId()!=null && castOther.getChainId()!=null && this.getChainId().equals(castOther.getChainId())))
&& ( (this.getName()==castOther.getName()) || ( this.getName()!=null &&  castOther.getName()!=null && this.getName().equals(castOther.getName())))) ;
}

public int hashCode() {
    int result = 17;

    result = 37 * result + ( getContractId() == null ? 0 : this.getContractId().hashCode() );
    result = 37 * result + ( getSectionId() == null ? 0 : this.getSectionId().hashCode() );
    result = 37 * result + ( getChainId() == null ? 0 : this.getChainId().hashCode() );
    result = 37 * result + ( getName() == null ? 0 : this.getName().hashCode() );
    return result;
} 
}

SectionChainViewRepository.java

public interface SectionChainViewRepository extends BaseRepository<SectionChainView, SectionChainViewId> {


@Query("select s from SectionChainView s where s.id.contractId = ?1 and s.id.sectionId = ?2")
public List<SectionChainView> findByContractIdAndSectionId(String contractId, String sectionId);

}

SECTION_CHAIN_VIEW 是来自 oracle 数据库的视图,看起来像这样。

|-----------------------------------------------------|
|CONTRACT_ID        SECTION_ID      CHAIN_ID    NAME  |
|-----------------------------------------------------|
|   001             A               ABC         Andy  |
|   002             B               XYZ         Mike  |
|   003             C               PQR         (null)|
-------------------------------------------------------

并且当 NAME 列中没有值或 null 时。在我的情况下,我的 NAME 列具有 null 值的合同 003。SectionChainViewRepository 的 findByContractIdAndSectionId 方法返回的 List 仅包含 2 个 SectionChain 对象而不是 3 个对象。我注意到当有 NULL 值时,它不会返回 SectionChain 对象。真正感谢解决获得 3 个 SectionChain 对象而不是 2 个的任何帮助。

4

0 回答 0