gpt4 book ai didi

java - Spring Jpa @Procedure 无法正常工作

转载 作者:行者123 更新时间:2023-12-02 09:15:22 25 4
gpt4 key购买 nike

尝试调用我的存储过程...

仓库看起来像这样:

@Repository
public interface ItemRepository extends JpaRepository<Item, Integer> {
@Procedure(name="getTopItems")
public List<Item> getTopItems(int top);
}

实体看起来像这样:

@Entity
@Table(name="Items")
@NamedStoredProcedureQuery(name="getTopItems", procedureName="pr_getTopItems", resultClasses={ Item.class },
parameters={ @StoredProcedureParameter(name="top", type=Integer.class, mode=ParameterMode.IN) })
@JsonPropertyOrder({ "itemId", "description" })
public class Item {

@ApiModelProperty(notes="Id of the item.", required=true, value="100000")
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@JsonProperty(access=Access.READ_ONLY)
private int itemId = 0;
@ApiModelProperty(notes="Item description.", required=true, value="Item1")
@NotNull
@Size(min=1, max=256)
private String description;
private int viewed;

public int getItemId() {
return this.itemId;
}

public String getDescription() {
return this.description;
}

public void setDescription(String description) {
this.description = description;
}

public int getViewed() {
return this.viewed;
}
}

当我尝试运行时,出现异常:

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.util.HashMap] to type [@org.springframework.data.jpa.repository.query.Procedure org.xxx.yyy.items.models.Item]

SP 返回名为 ItemIdDescriptionViewed 的列。

映射不是应该是自动魔法的吗?这是新代码,所以我正在寻找 2019 年 11 月最新、最好的实现方式:)。

我也使用标准的 findAll() 并且它映射正确。

最佳答案

几个月前就已经面临这个问题了。尝试在您的存储库中使用注释 @Query :

@Repository
public interface ItemRepository extends JpaRepository<Item, Integer> {

@Query(nativeQuery=true, value="call your_procedure(:your_parameter)")
public List<Item> getTopItems(int top);
}

我希望这能起作用。

关于java - Spring Jpa @Procedure 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59041278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com