修复 Spring Boot 中存储过程之间的转换问题
Posted
技术标签:
【中文标题】修复 Spring Boot 中存储过程之间的转换问题【英文标题】:Fix casting issue between stored procedure in spring boot 【发布时间】:2020-03-12 13:09:19 【问题描述】:我正在编写一个 Spring Boot 应用程序,我在我的代码中合并了一个存储过程。我有一个有 8 列的实体类,我有一个存储过程,它接受一个参数并从该实体类传回 3 个列。但是,当我调用存储过程时,它会抛出一个java.land.ClassCastException
。我尝试通过将resultClasses
添加到存储过程来解决此问题,但是当我运行该代码时,它失败了,因为它说我正在使用所有 8 个列名。所以我不确定这是因为存储过程问题还是因为我在 Java 代码中实现错误。当我这么说时,我的意思是我认为我选错了。
存储过程:
@Entity
@NamedStoredProcedureQueries(value =
@NamedStoredProcedureQuery(name = "procedure-edit",
procedureName = "GetQueryConf",
resultClasses = AppSelectorTier.class),
@NamedStoredProcedureQuery(name= "procedure-getTierType",
procedureName= "GetTierTypeByObj",
//resultClasses = AppSelectorTier.class,
parameters=
@StoredProcedureParameter(mode= ParameterMode.IN, name= "taddm_obj", type= String.class),
),
)
public class AppSelectorTier implements java.io.Serializable
private static final long serialVersionUID = 1L;
@Id
@Column(name = "tiername")
private String tiername;
@Column(name = "system")
private String system;
@Column(name = "mqllisting1")
private String mqllisting1;
@Column(name = "mqllisting2")
private String mqllisting2;
@Column(name = "tagname_one")
private String tagname_one;
@Column(name = "tagname_two")
private String tagname_two;
@Column(name = "parseinstruction")
private String parseinstruction;
@Column(name = "taddmobj")
private String taddmobj;
// Constructor
public AppSelectorTier(String tiername, String system, String mqllisting1, String mqllisting2, String tagname_one,
String tagname_two, String parseinstruction, String taddmobj)
super();
this.tiername = tiername;
this.system = system;
this.mqllisting1 = mqllisting1;
this.mqllisting2 = mqllisting2;
this.tagname_one = tagname_one;
this.tagname_two = tagname_two;
this.parseinstruction = parseinstruction;
this.taddmobj = taddmobj;
public AppSelectorTier(String tiername, String system,String tagname_two)
super();
this.tiername = tiername;
this.system = system;
this.tagname_two = tagname_two;
// Default Constructor
public AppSelectorTier()
// Getters
public String getTiername()
return tiername;
public String getSystem()
return system;
public String getMqllisting1()
return mqllisting1;
public String getMqllisting2()
return mqllisting2;
public String getTagname_one()
return tagname_one;
public String getTagname_two()
return tagname_two;
public String getParseinstruction()
return parseinstruction;
public String getTaddmobj()
return taddmobj;
// Setters
public void setTiername(String tiername)
this.tiername = tiername;
public void setSystem(String system)
this.system = system;
public void setMqllisting1(String mqllisting1)
this.mqllisting1 = mqllisting1;
public void setMqllisting2(String mqllisting2)
this.mqllisting2 = mqllisting2;
public void setTagname_one(String tagname_one)
this.tagname_one = tagname_one;
public void setTagname_two(String tagname_two)
this.tagname_two = tagname_two;
public void setParseinstruction(String parseinstruction)
this.parseinstruction = parseinstruction;
public void setTaddmobj(String taddmobj)
this.taddmobj = taddmobj;
@Override
public String toString()
return "AppSelectorTier [tiername=" + tiername + ", system=" + system + ", mqllisting1=" + mqllisting1
+ ", mqllisting2=" + mqllisting2 + ", tagname_one=" + tagname_one + ", tagname_two=" + tagname_two
+ ", parseinstruction=" + parseinstruction + ", taddmobj=" + taddmobj + "]";
procedureName= "GetTierTypeByObj"
此过程仅返回 tiername、system 和 tagname_two
服务层:
@SuppressWarnings("unchecked")
public List<AppSelectorTier> getTierTypeFromObj(String parameter)
StoredProcedureQuery storedProcedure = entityManager.createNamedStoredProcedureQuery("procedure-getTierType").setParameter("taddm_obj", parameter);
return storedProcedure.getResultList();
Java 实现:
if (joinQueryService.getTierTypeFromObj("WebService").size() != 0)
AppSelectorTier selectorObj = joinQueryService.getTierTypeFromObj("WebService").get(0);
System.out.println("Tiername: " + selectorObj.getTiername());
System.out.println("System: " + selectorObj.getSystem());
System.out.println("Tagname two: " + selectorObj.getTagname_two());
current.getQuery();
current.setTier(selectorObj.getTiername());
else
temp = "";
current.getQuery();
current.setTier("");
错误:
java.lang.ClassCastException: java.base/[Ljava.lang.Object; cannot be cast to com.UPS.entity.AppSelectorTier
【问题讨论】:
//resultClasses = AppSelectorTier.class 【参考方案1】:你可能错过了:
//resultClasses = AppSelectorTier.class
尝试取消注释。 如果不指定类,则返回 Object 类型。 设置:
resultClasses = AppSelectorTier.class,
【讨论】:
我撤消了评论,现在它说,mqllisting1
是无效列。
检查这个:***.com/questions/7260487/…以上是关于修复 Spring Boot 中存储过程之间的转换问题的主要内容,如果未能解决你的问题,请参考以下文章
使用 jparepository 调用存储过程 Spring Boot 应用程序
Spring Boot:在长时间运行的存储过程中检测到明显的连接泄漏
Spring Boot | 集成MapStruct实现不同类型Java对象间的自动转换
带有存储过程的 Spring Boot MySQL 数据库初始化错误