我的 Hibernate HQL 查询似乎返回了陈旧的数据。
我有一个名为 Account 的简单 java 类,它的实例映射到具有两个 varchar 列、用户名和姓氏的单个数据库表。
如果我运行 HQL 查询,例如:
List<?> accountList = session.createQuery("from Account where surname is null").list();
正如预期的那样,我得到了一个 Account 对象列表(表中的某些行确实有 null surname 字段)。
然后,我将返回对象的姓氏设置为某个非空值:
Iterator<?> accountIter = accountList.iterator();
while (accountIter.hasNext()) {
Account account = (Account) accountIter.next();
log("Adding surname of Jones to : " + account.getUsername());
account.setSurname("Jones");
}
此时,如果我再次运行 HQL 查询,我希望返回一个空列表(因为所有姓氏都应该是非空的),但我会返回与第一次运行查询时相同的对象。这不是我所期望的。
引用 Hibernate 文档:
http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/
“绝对不能保证 Session 何时执行 JDBC 调用,只能保证它们执行的顺序。但是,Hibernate 确实保证 Query.list(..) 永远不会返回陈旧或不正确的数据。”
这似乎与我的代码的行为相反。查看下面清单 4 中的程序输出,SQL Update 语句发生在所有 select 语句之后,因此最后一个 select 返回不正确的数据。任何人都可以阐明发生了什么,或者我做错了什么?
如果我用事务围绕姓氏的设置,并执行session.saveOrUpdate(account)
一切工作,但我认为这不是必需的。
我希望我的代码尽可能只处理域类,并尽可能地没有持久性代码。
我正在使用带有 Java 1.6 的 Hibernate 4.1.8.Final
我的完整代码清单如下:
清单 1:Main.java:
package uk.ac.york.cserv.hibernatetest;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Main {
private static SessionFactory sf;
Session session;
public static void main(String[] args) {
Main main = new Main();
main.doExample();
}
public Main() {
sf = new Configuration()
.configure("hibernate-ora.cfg.xml")
.buildSessionFactory();
session = sf.openSession();
}
public void closeSession() {
session.flush();
session.close();
}
public List<?> getAccountList() {
return session.createQuery("from Account where surname is null").list();
}
public void printAccountList(List<?> accountList) {
Iterator<?> accountIter = accountList.iterator();
while (accountIter.hasNext()) {
System.out.println(accountIter.next());
}
}
public void log(String msg) {
System.out.println(msg);
}
public void doExample() {
log("Print all accounts with null surnames...");
printAccountList(getAccountList());
log("Adding surnames to accounts that have null surnames...");
//session.beginTransaction();
Iterator<?> accountIter = getAccountList().iterator();
while (accountIter.hasNext()) {
Account account = (Account) accountIter.next();
log("Adding surname of Jones to : " + account.getUsername());
account.setSurname("Jones");
//session.saveOrUpdate(account);
}
//session.getTransaction().commit();
log("Again print all accounts that have null surnames (should be none)...");
printAccountList(getAccountList());
closeSession();
}
}
清单 2:Account.java:
package uk.ac.york.cserv.hibernatetest;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="ACCOUNTS")
public class Account {
@Id
@Column(name = "USERNAME", unique = true, nullable = false)
private String username;
@Column(name = "SURNAME")
private String surname;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
@Override
public String toString() {
return "Account [username=" + username + ", surname=" + surname + "]";
}
}
清单 3:Hibernate-ora.cfg.xml:
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@testhost:1521:test</property>
<property name="connection.username">testschema</property>
<property name="connection.password">testpassword</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Names of the annotated classes -->
<mapping class="uk.ac.york.cserv.hibernatetest.Account"/>
</session-factory>
</hibernate-configuration>
清单 4:程序的输出:
Print all accounts with null surnames...
Hibernate: select account0_.USERNAME as USERNAME0_, account0_.SURNAME as SURNAME0_ from ACCOUNTS account0_ where account0_.SURNAME is null
Account [username=user2, surname=null]
Adding surnames to accounts that have null surnames...
Hibernate: select account0_.USERNAME as USERNAME0_, account0_.SURNAME as SURNAME0_ from ACCOUNTS account0_ where account0_.SURNAME is null
Adding surname of Jones to : user2
Again print all accounts that have null surnames (should be none)...
Hibernate: select account0_.USERNAME as USERNAME0_, account0_.SURNAME as SURNAME0_ from ACCOUNTS account0_ where account0_.SURNAME is null
Account [username=user2, surname=Jones]
Hibernate: update ACCOUNTS set SURNAME=? where USERNAME=?