我在 PostgreSQL 中有一个名为“transaction_response”的简单表。在这个表中,只有两列:transaction_id 和 response_id。
我在那里的数据有两行。两者都具有相同的 transaction_id,但不同的 response_id 值。
我创建了以下 Java 类来保存这些数据:
public class TransactionResponseDAO implements java.io.Serializable {
private Integer transactionId;
private Integer responseId;
public Integer getTransactionId() {
return transactionId;
}
public void setTransactionId(Integer transactionId) {
this.transactionId = transactionId;
}
public Integer getResponseId() {
return responseId;
}
public void setResponseId(Integer responseId) {
this.responseId = responseId;
}
public String toString() {
return "Transaction Id: " + transactionId + "\nResponse Id: " + responseId;
}
}
然后我创建了名为“TransactionResponse.hbm.xml”的 Hibernate 配置文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.data.TransactionResponseDAO" table="transaction_response">
<id name="transactionId" column="transaction_id" type="java.lang.Integer">
<generator class="increment"/>
</id>
<property name="responseId" type="java.lang.Integer">
<column name="responses_id" />
</property>
</class>
<query name="findTransactionResponseByTransactionId">
<![CDATA[from com.data.TransactionResponseDAO where transaction_id = :transactionId]]>
</query>
<query name="findTransactionResponseByBothIds">
<![CDATA[from com.data.TransactionResponseDAO where transaction_id = :transactionId
and responses_id = :responseId]]>
</query>
</hibernate-mapping>
在我正在测试的 Java 类中,我有以下代码:
Query q = null;
SessionFactory sFactory = new Configuration().configure("/conf/hibernate.cfg.xml").buildSessionFactory();
Session session = null;
try {
session = sFactory.openSession();
q = session.getNamedQuery("findTransactionResponseByTransactionId");
q.setInteger("transactionId", transactionId);
}catch (Exception e) {
System.err.println("Error running getStatusTab");
e.printStackTrace();
session.close();
sFactory.close();
}
ArrayList<TransactionResponseDAO> results = (ArrayList<TransactionResponseDAO>) q.list();
session.close();
sFactory.close();
System.out.println(">>> Size: " + results.size());
for (TransactionResponseDAO tr : results) {
System.out.println(tr);
System.out.println();
}
当我运行此代码时,它会返回两行,因为它应该。但是,当打印出这些值时......两行的 response_id 是相同的。因此,出于某种原因,它似乎没有引入第二个 response_id 值。
这是什么原因?一如既往,我感谢您的帮助!