JPA 调用 MSSQL 存储过程 returns 重复记录
JPA call to MSSQL stored procedure returns duplicate records
我使用 JPA 调用的 MS SQL 服务器数据库中有一个存储过程。从 java 代码调用它,它 returns 重复记录:
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 服务器调用存储过程 returns 正确结果:
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);
}
}
我遇到了同样的问题并发现了问题。
Entity
class 中几乎所有属性都提到了身份注释 @Id
。请仅对结果集中唯一的 attribute/attributes 提及身份注释 @Id
。这解决了我的问题。
我使用 JPA 调用的 MS SQL 服务器数据库中有一个存储过程。从 java 代码调用它,它 returns 重复记录:
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 服务器调用存储过程 returns 正确结果:
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);
}
}
我遇到了同样的问题并发现了问题。
Entity
class 中几乎所有属性都提到了身份注释 @Id
。请仅对结果集中唯一的 attribute/attributes 提及身份注释 @Id
。这解决了我的问题。