使用 Spring JPA/Hibernate 实体将列表中的值与输入字符串值进行比较
Compare value in a List to an input String Value using Spring JPA/Hibernate Entity
我有一种情况,我试图将输入字符串值与我的列表中的值进行比较,该列表是 ForecastTypes 列表(我的实体)。我正在点击的 table 没有主键,这是我无法控制的,所以我试图通过 oracledb 的行号功能建立一个唯一的键。当我 运行 我的程序并将我的输入字符串值与保存通过我的实体检索到的值的列表进行比较时,当它们匹配时(例如 - 字符串输入值是 THANKSGIVING 并且我的 ForecastTypes 列表中的值是 THANKSGIVING)它仍然 returns false 而不是 true 并填充错误而不是返回 true。我正在尝试将输入字符串值“值”与我的 ForecastTypes 实体中指定的“forecastName”列名称进行比较。我不确定我在这里做错了什么,因为我正在使用 contains() 方法来查看我的 forecastList 是否包含作为我的输入值的 String 值。有什么建议吗?我正在使用 Java 8 EE、Spring、JPA 和 Hibernate。我有其他方法可以在我的应用程序中使用相同的模式正常工作,我只是想单独深入了解这个特定的功能,并删除了那些以提高可读性。任何帮助将不胜感激。下面是我的代码,在评论中会告诉你我想要实现的功能以及它们所属的 classes,等等。
//全球
fcstList = OracleImpl.forecastIDList();
//在我的 Validator.java class
中进行布尔值检查
public static boolean isForecastIDValid(String value, List<ForecastTypes> fcstList) {
value = Util.getTrimmedValue(value);
forecastList = fcstList;
if (forecastList.contains(value)) {
return true;
}
return false;
}
//在 Validator 中添加无效预测 ID 错误的方法 class
fc.forEach(fc -> {
if (fc.getErrorList().size() == 0) {
if (!isForecastIDValid(fc.getForecast_ID(), fcstList)) {
fc.getErrorList().add(Constants.FORECAST_ID_INVALID);
}
}
});
//从我的 query.properties 文件中查询 属性
isForecastIDValid = SELECT ROW_NUMBER() OVER( ORDER BY ID DESC ) row_num, ID, TYPE FROM SCHEMA.FCSTIDS
//我的实体:
package com.lance.db.oracle.entity;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Column;
@Entity
public class ForecastTypes {
@Id
@Column(name="row_num")
String rowNumber;
public String getRowNumber() {
return rowNumber;
}
public void setRowNumber(String rowNumber) {
this.rowNumber = rowNumber;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Column(name="TYPE")
String type;
@Column(name="ID")
String forecastName;
public String getForecastName() {
return forecastName;
}
public void setForecastName(String forecastName) {
this.forecastName = forecastName;
}
}
//OracleImpl中的Oracle entitymanagerfactory执行class
package com.lance.db.oracle;
import java.math.BigDecimal;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.core.env.Environment;
import com.lance.db.oracle.entity.ForecastTypes;
@Configuration
@PropertySources({ @PropertySource("classpath:sql/query.properties") })
public class OracleImpl {
private static Logger logger = LogManager.getLogger(OracleImpl.class);
private static EntityManagerFactory emf;
private static Environment env;
public static void setMyConfig(EntityManagerFactory emf, Environment env) {
OracleImpl.emf = emf;
OracleImpl.env = env;
}
public static List<ForecastTypes> forecastIDList() {
EntityManager em = emf.createEntityManager();
Query q = em.createNativeQuery(env.getProperty("isForecastIDValid"), ForecastTypes.class);
List<ForecastTypes> res = q.getResultList();
em.clear();
em.close();
return res;
}
}
问题出在你的Validator.javaclass。您正在将 String 对象与 ForecastTypes 对象进行比较,而这永远不会匹配。
换句话说,您的 ForecastTypes 列表永远不能包含 (".contains") String 对象,因为它是 ForecastTypes 列表 (List)。
它应该像这样工作 - 通过比较 String 和 String:
public static boolean isForecastIDValid(String value, List<ForecastTypes> fcstList) {
value = Util.getTrimmedValue(value);
for (ForecastTypes ft : fcstList) {
if (ft.getForecastName().equals(value))
return true;
}
return false;
}
我有一种情况,我试图将输入字符串值与我的列表中的值进行比较,该列表是 ForecastTypes 列表(我的实体)。我正在点击的 table 没有主键,这是我无法控制的,所以我试图通过 oracledb 的行号功能建立一个唯一的键。当我 运行 我的程序并将我的输入字符串值与保存通过我的实体检索到的值的列表进行比较时,当它们匹配时(例如 - 字符串输入值是 THANKSGIVING 并且我的 ForecastTypes 列表中的值是 THANKSGIVING)它仍然 returns false 而不是 true 并填充错误而不是返回 true。我正在尝试将输入字符串值“值”与我的 ForecastTypes 实体中指定的“forecastName”列名称进行比较。我不确定我在这里做错了什么,因为我正在使用 contains() 方法来查看我的 forecastList 是否包含作为我的输入值的 String 值。有什么建议吗?我正在使用 Java 8 EE、Spring、JPA 和 Hibernate。我有其他方法可以在我的应用程序中使用相同的模式正常工作,我只是想单独深入了解这个特定的功能,并删除了那些以提高可读性。任何帮助将不胜感激。下面是我的代码,在评论中会告诉你我想要实现的功能以及它们所属的 classes,等等。
//全球
fcstList = OracleImpl.forecastIDList();
//在我的 Validator.java class
public static boolean isForecastIDValid(String value, List<ForecastTypes> fcstList) {
value = Util.getTrimmedValue(value);
forecastList = fcstList;
if (forecastList.contains(value)) {
return true;
}
return false;
}
//在 Validator 中添加无效预测 ID 错误的方法 class
fc.forEach(fc -> {
if (fc.getErrorList().size() == 0) {
if (!isForecastIDValid(fc.getForecast_ID(), fcstList)) {
fc.getErrorList().add(Constants.FORECAST_ID_INVALID);
}
}
});
//从我的 query.properties 文件中查询 属性
isForecastIDValid = SELECT ROW_NUMBER() OVER( ORDER BY ID DESC ) row_num, ID, TYPE FROM SCHEMA.FCSTIDS
//我的实体:
package com.lance.db.oracle.entity;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Column;
@Entity
public class ForecastTypes {
@Id
@Column(name="row_num")
String rowNumber;
public String getRowNumber() {
return rowNumber;
}
public void setRowNumber(String rowNumber) {
this.rowNumber = rowNumber;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Column(name="TYPE")
String type;
@Column(name="ID")
String forecastName;
public String getForecastName() {
return forecastName;
}
public void setForecastName(String forecastName) {
this.forecastName = forecastName;
}
}
//OracleImpl中的Oracle entitymanagerfactory执行class
package com.lance.db.oracle;
import java.math.BigDecimal;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.core.env.Environment;
import com.lance.db.oracle.entity.ForecastTypes;
@Configuration
@PropertySources({ @PropertySource("classpath:sql/query.properties") })
public class OracleImpl {
private static Logger logger = LogManager.getLogger(OracleImpl.class);
private static EntityManagerFactory emf;
private static Environment env;
public static void setMyConfig(EntityManagerFactory emf, Environment env) {
OracleImpl.emf = emf;
OracleImpl.env = env;
}
public static List<ForecastTypes> forecastIDList() {
EntityManager em = emf.createEntityManager();
Query q = em.createNativeQuery(env.getProperty("isForecastIDValid"), ForecastTypes.class);
List<ForecastTypes> res = q.getResultList();
em.clear();
em.close();
return res;
}
}
问题出在你的Validator.javaclass。您正在将 String 对象与 ForecastTypes 对象进行比较,而这永远不会匹配。
换句话说,您的 ForecastTypes 列表永远不能包含 (".contains") String 对象,因为它是 ForecastTypes 列表 (List)。
它应该像这样工作 - 通过比较 String 和 String:
public static boolean isForecastIDValid(String value, List<ForecastTypes> fcstList) {
value = Util.getTrimmedValue(value);
for (ForecastTypes ft : fcstList) {
if (ft.getForecastName().equals(value))
return true;
}
return false;
}