gpt4 book ai didi

java - 选择查询的 H2 数据库问题 - 结果错误

转载 作者:行者123 更新时间:2023-11-30 22:33:03 25 4
gpt4 key购买 nike

我的 h2 数据库有一个非常奇怪的行为,即对特定数据库表进行简单的选择查询。同样的查询在 MySQL 上运行良好,但是如果我将应用程序的底层数据库切换到 H2,它会返回错误的结果(应用程序既可以在 MySQL 上运行,也可以在 H2 数据库上运行)。

我有第二个几乎相同的查询,它运行良好,所以我将两个查询都发布在这里以便更好地理解。

有两个表:“StudyProtocolNames”和“StudyRfCoils”,它们与表“StudyDetails”具有 N-1 关系(StudyDetails.stId 主键是 StudyProtocolNames 和 StudyRfCoils 上的外键)。

这是父表类“StudyDetails”:

@Entity
@Table(name="StudyDetails",
uniqueConstraints = @UniqueConstraint(columnNames = "StudyInstanceUID"))
public class StudyDetails implements Serializable {

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name="StId", unique = true, nullable = false)
private Long stId;

@Column(name="StudyInstanceUID", unique=true, nullable = false)
private String studyInstanceUID;

@Column(name="SoapResponse")
private int soapResponse;

@Column(name="PatientName")
private String patientName;

@Column(name="RfCoil", nullable = false)
private String rfCoil;

@Column(name="ProtocolName", nullable = false)
private String protocolName;

...

@OneToMany(fetch = FetchType.LAZY, mappedBy = "studyDetails", cascade = CascadeType.ALL)
private Set<StudyProtocolNames> studyProtocolNames = new HashSet<>(0);

@OneToMany(fetch = FetchType.LAZY, mappedBy = "studyDetails", cascade = CascadeType.ALL)
private Set<StudyRfCoils> studyRfCoils = new HashSet<>(0);
...
}

这里是“StudyProtocolNames”和“StudyRfCoils”类:

@Entity 
@Table(name="StudyProtocolNames")
public class StudyProtocolNames implements Serializable {

private static long serialVersionUID = -298254562330274106L;

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "PnId", unique = true, nullable = false)
private Long pnId;


@Column(name="StudyInstanceUID")
private String studyInstanceUID;

@Column(name="ProtocolName", nullable = false)
private String protocolName;

@ManyToOne
@JoinColumn(name = "StId", referencedColumnName="StId")
private StudyDetails studyDetails;
...
}

...

@Entity 
@Table(name="StudyRfCoils")
public class StudyRfCoils implements Serializable {

private static long serialVersionUID = -298254562330274106L;

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "RfcId", unique = true, nullable = false)
private Long rfcId;

@Column(name="StudyInstanceUID")
private String studyInstanceUID;

@Column(name="RfCoil", nullable = false)
private String rfCoil;

@ManyToOne
@JoinColumn(name = "StId", referencedColumnName="StId")
private StudyDetails studyDetails;
...
}

在我的应用程序的某个点,在向“StudyProtocolNames”插入新记录之前,我检查是否已经有具有相同 studyInstanceUID 和 protocolName 的记录。如果已经有这样一条记录,则该方法返回true。

@Override
public Boolean checkDicomStudyProtocolNameExistForStudy(String studyInstanceUID, String protocolName) {

Boolean status = false;

List<String> results=new ArrayList<>();
Session s=HibernateUtil.openSession();
s.beginTransaction();
String hql = "FROM StudyProtocolNames E WHERE E.studyInstanceUID = :studyInstanceUID AND E.protocolName = :protocolName";
Query query = s.createQuery(hql);
query.setParameter("protocolName", protocolName.trim());
query.setParameter("studyInstanceUID", studyInstanceUID);
results = query.list();
s.getTransaction().commit();
s.close();
log.info(results.size() + " Records found for study in checkDicomStudyProtocolNameExistForStudy: " + studyInstanceUID + " and protocolName" + protocolName);
if(results.isEmpty()) {
status = false;
} else {
status = true;
}

return status;
}

我正在对“StudyRfCoils”表进行完全相同的检查(不同的列相同的逻辑):

@Override
public Boolean checkDicomStudyRfCoilExistForStudy(String studyInstanceUID, String rfCoil) {

Boolean status = false;

List<String> results=new ArrayList<>();
Session s=HibernateUtil.openSession();
s.beginTransaction();
String hql = "FROM StudyRfCoils E WHERE E.studyInstanceUID = :studyInstanceUID AND E.rfCoil = :rfCoil";
Query query = s.createQuery(hql);
query.setParameter("rfCoil", rfCoil.trim());
query.setParameter("studyInstanceUID", studyInstanceUID);
results = query.list();
s.getTransaction().commit();
s.close();
log.info(results.size() + " Records found for study in checkDicomStudyRfCoilExistForStudy: " + studyInstanceUID + " and rfCoil: " + rfCoil);
if(results.isEmpty()) {
status = false;
} else {
status = true;
}

return status;

}

表 StudyProtocolNames 的第一个检查查询工作正常,如果记录具有相同的“studyInstanceUID”和“protocolName”,则它返回 true。

然而,第二个查询总是返回 FALSE。它无法找到具有相同“studyInstanceUID”和“rfCoil”的现有记录。所以我在我的数据库表中结束了多条记录,列“studyInstanceUID”和“rfCoil”具有相同的值:

enter image description here

另一个奇怪的事实是,当我切换到 MySQL 时,第二次检查查询工作正常。只有在使用 H2 DB 时才会发生这种奇怪的行为。

最佳答案

似乎这种行为是由我对 rfCoil 字符串值执行的 trim() 引起的:

@Override
public Boolean checkDicomStudyRfCoilExistForStudy(String studyInstanceUID, String rfCoil) {
...
query.setParameter("rfCoil", rfCoil.trim());
...

然而在 MySQL 中这不会发生

关于java - 选择查询的 H2 数据库问题 - 结果错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33322940/

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