从简单CRUD存储库调用中检索到重复的条目

时间:2019-06-17 21:20:48

标签: java spring-boot spring-repositories

我有一个函数应该调用数据库以检索每个具有accountRef = userInput的条目。

要检索的表没有主键。到目前为止,我的代码已发布在下面。 最终发生的情况是,如果有3个具有accountRef ='abc'的条目,它将重复第一个条目3次并返回它。 因此,例如,如果有2个条目的accountRef ='xyz',则返回 [{accountRef:“ xyz”,名称:“第一项”},{accountRef:“ xyz”,名称:“第一项”}]

这是我的代码: BasicAccountAuditRepository.java

package org.fusion.restful.basicaccount.dao;
import org.fusion.restful.basicaccount.model.BasicAccount;
import org.fusion.restful.basicaccount.model.BasicAccountAudit;
import org.fusion.restful.basicaccount.model.BasicAccountAuditPK;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import org.springframework.data.repository.query.Param; 


import java.util.List;
import java.util.Optional;

@Repository
public interface BasicAccountAuditRepository extends CrudRepository<BasicAccountAudit, BasicAccountAuditPK> {

    List<BasicAccountAudit> findAll();

    List<BasicAccountAudit> findByAccountRef(String accountRef);

}

BasicAccountAudit.java

package org.fusion.restful.basicaccount.model;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
import java.util.Date;

@XmlRootElement
@Entity
@Table(name = "tb_Account_History", schema="dbo")
@IdClass(BasicAccountAuditPK.class)
public class BasicAccountAudit implements Serializable{
    @Id
    @Column(columnDefinition = "String")

  private String accountRef;
  private String client;
  .
  .
  .
  .

    @Temporal(TemporalType.TIMESTAMP)
    @Column(insertable = true, columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
    private Date enteredDate;



    @Temporal(TemporalType.TIMESTAMP)
    @Column(insertable = true, columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
    private Date lastUpdatedDate;


//.......getters & setters
}

BasicAccountAuditPK.java

package org.fusion.restful.basicaccount.model;

import java.io.Serializable;

public class BasicAccountAuditPK implements Serializable {

    private String accountRef;


    public String getAccountRef() {
        return accountRef;
    }

    public void setAccountRef(String accountRef) {
        this.accountRef = accountRef;
    }

}

0 个答案:

没有答案