MS SQL Server数据库中有一个存储过程,我使用JPA进行调用。从java代码调用它,它返回重复的记录:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
@NamedNativeQueries({
@NamedNativeQuery(
name = "ClassRP.getPs",
query = "{call Load_Ps_Data("
+ ":param1,"
+ ":param2,"
+ ":param3,"
+ ":param4)}",
resultClass = ClassRP.class),
@NamedNativeQuery(
name = "ClassRP.getRSPs",
query = "{call Load_RSPs_Data("
+ ":param1,"
+ ":param2)}",
resultClass = ClassSP.class)
})
@Entity
@IdClass(RPKey.class)
public class ClassRP {
@Id
@Column(name = "column1")
private long column1;
@Id
@Column(name = "column2")
private int column2;
@Id
@Column(name = "column3")
private int column3;
@Id
@Column(name = "column4")
private int column4;
public long getColumn1() {
return column1;
}
public int getColumn2() {
return column2;
}
public Date getColumn3() {
return column3;
}
public String getColumn4() {
return column4;
}
}
import java.io.Serializable;
public class RPKey implements Serializable {
private static final long serialVersionUID = 41627806516297462433L;
public Long column1;
public Integer column2;
}
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
@Entity
@IdClass(RPKey.class)
public class ClassSP {
@Id
@Column(name = "column1")
private Long column1;
@Id
@Column(name = "column2")
private Integer column2;
@Id
@Column(name = "column3")
private String column3;
@Id
@Column(name = "column4")
private String column4;
@Column(name = "column5")
private Date column5;
@Column(name = "column6")
private String column6;
@Column(name = "column7")
private Double column7;
public Long getColumn1() {
return column1;
}
public Integer getColumn2() {
return column2;
}
public Date getColumn3() {
return column3;
}
public String getColumn4() {
return column4;
}
public Double getColumn5() {
return column5;
}
public Double getColumn6() {
return column6;
}
public Double getColumn7() {
return column7;
}
}
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
public interface RPsRepository extends JpaRepository<ClassRP, String> {
@Query(nativeQuery = true)
List<ClassRP> getPs(
@Param("param1") Integer param1,
@Param("param2") Integer param2,
@Param("param3") Date param3,
@Param("param4") Integer param4);
@Query(nativeQuery = true)
List<ClassSP> getRSPs(
@Param("param1") String param1,
@Param("param2") Integer param2);
}
从MS SQL服务器调用存储过程会返回正确的结果:
exec Load_RSPs_Data param1, param2;
为什么JPA调用行为不正确的任何建议?
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("rService")
public class RService {
@Autowired
private RPsRepository rpsRepository;
public List<ClassSP> getSPsData(String param1, Integer param2) {
return rpsRepository.getRSPs(param1, param2);
}
}