findBy ...方法在spring数据jpa中返回null值

时间:2013-10-08 21:18:55

标签: spring spring-data-jpa

我很擅长弹出数据并试图分析一段代码。在这里。

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时。在我的情况下,合同003我的NAME列具有空值。 SectionChainViewRepository的findByContractIdAndSectionId方法返回List,只有2个SectionChain对象而不是3个对象。我注意到当有NULL值时它不返回SectionChain对象。通过解决获得3个SectionChain对象而非2来获得任何帮助都非常感激。

0 个答案:

没有答案