私はテーブルLOCATIONでCRUD操作を行っているシンプルな休止状態のクラッシュアプリケーションに取り組んでいます。 私はmaven 3.0、hibernate 3.6、oracle 10g、java 8を使用しています。Hibernateの選択クエリがデータベースから最新のレコードを返さない
私はこれらの4つの操作を実行しています。 1)テーブルの場所に行を挿入する(成功) 2)その行を取得してステータスを確認する(成功) 3)同じレコードの1つの列を更新する(データベースが更新されている) 4)同じ行を取得する再び。ここにキャッチがあります。手順3で既に更新されている行の更新された状態を取得していません。手順2の取得で返された結果が表示されます。
ここは私のメインクラスです。
public class App {
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
//step 1
session.beginTransaction();
saveLocation(session);
session.getTransaction().commit();
//step 2
session.beginTransaction();
retrieveLocation(session);
session.getTransaction().commit();
//step 3
session.beginTransaction();
updateLocation(session);
session.getTransaction().commit();
//step 4
session.beginTransaction();
retrieveLocation(session);
session.getTransaction().commit();
}
private static void saveLocation(final Session session) {
Location location = new Location();
location.setZip("751001");
location.setCity("Bhubaneswar");
location.setCountry("India");
location.setRegion("Asia pacific");
session.save(location);
}
private static void retrieveLocation(final Session session) {
Query query = session.createQuery("from org.bhawani.practice.hibernate.location.Location where zip = :zip");
query.setParameter("zip", "751001");
List<Location> locations = query.list();
for (Location location : locations) {
System.out.println("City: " + location.getCity());
}
}
private static void updateLocation(final Session session) {
Query query = session.createQuery(
"update org.bhawani.practice.hibernate.location.Location set city = :city where zip = :zip");
query.setParameter("city", "Cuttack");
query.setParameter("zip", "751001");
query.executeUpdate();
}
}
はここにここに私のHibernateマッピングxmlですLocation.java
package org.bhawani.practice.hibernate.location;
public class Location {
private String zip;
private String city;
private String country;
private String region;
public Location() {
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
}
私のモデルクラスです。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.bhawani.practice.hibernate.location.Location"
table="LOCATION">
<id name="zip" type="string">
<column name="ZIP" />
<generator class="assigned" />
</id>
<property name="city" type="string">
<column name="CITY" />
</property>
<property name="country" type="string">
<column name="COUNTRY" />
</property>
<property name="region" type="string">
<column name="REGION" />
</property>
</class>
</hibernate-mapping>
ここに私の表の説明があります。ここで
Name Null Type
------- -------- ------------------
ZIP NOT NULL VARCHAR2(255 CHAR)
CITY VARCHAR2(255 CHAR)
COUNTRY VARCHAR2(255 CHAR)
REGION VARCHAR2(255 CHAR)
は私の休止状態の設定ファイルが
ある<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521:XE</property>
<property name="hibernate.connection.username">hr</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.default_schema">HR</property>
<property name="show_sql">true</property>
<mapping resource="org/bhawani/practice/hibernate/location/Location.hbm.xml" />
</session-factory>
</hibernate-configuration>
ご回答ありがとうございます。 –