0
我在程序下運行,並期望hibernate將撥打ObjectNotFoundException
撥打load
和null
致電get
。但我在輸出中看不到book : null
。Session.get拋出異常而不是null
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class LoadAndGetTest {
public static void main(String[] args) {
Configuration configuration = new Configuration().configure();
SessionFactory factory = configuration.buildSessionFactory();
Session session = factory.openSession();
try {
Book book = (Book) session.load(Book.class, "DOES_NOT_EXIST");
} catch(Exception exception) {
exception.printStackTrace();
}
Book book = (Book) session.get(Book.class, "DOES_NOT_EXIST");
System.out.println("book : "+ book);
}
}
Book.java
import java.util.Date;
import java.util.List;
public class Book {
private String isbn;
private String name;
private Publisher publisher;
private Date publishDate;
private int price;
private List chapters;
// Getters and Setters
public Book() {
}
public Book(String isbn, String name, Publisher publisher,
Date publishDate, int price, List chapters) {
super();
this.isbn = isbn;
this.name = name;
this.publisher = publisher;
this.publishDate = publishDate;
this.price = price;
this.chapters = chapters;
}
public String getIsbn() {
return isbn;
}
public String getName() {
return name;
}
public Publisher getPublisher() {
return publisher;
}
public Date getPublishDate() {
return publishDate;
}
public int getPrice() {
return price;
}
public List getChapters() {
return chapters;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public void setName(String name) {
this.name = name;
}
public void setPublisher(Publisher publisher) {
this.publisher = publisher;
}
public void setPublishDate(Date publishDate) {
this.publishDate = publishDate;
}
public void setPrice(int price) {
this.price = price;
}
public void setChapters(List chapters) {
this.chapters = chapters;
}
}
和book.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.chatar.hibernate.receipes.example.domain">
<class name="Book" table="BOOK">
<id name="isbn" type="string" column="ISBN" />
<property name="name" type="string" column="BOOK_NAME" />
<property name="publishDate" type="date" column="PUBLISH_DATE" />
<property name="price" type="int" column="PRICE" />
</class>
</hibernate-mapping>
而且
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">
org.apache.derby.jdbc.EmbeddedDriver
</property>
<property name="connection.url">jdbc:derby://localhost:1527/BookShopDB</property>
<property name="connection.username">book</property>
<property name="connection.password">book</property>
<property name="dialect">org.hibernate.dialect.DerbyDialect</property>
<mapping resource="com/chatar/hibernate/receipes/example/domain/book.hbm.xml" />
</session-factory>
</hibernate-configuration>
而這裏的例外
950 [main] INFO org.hibernate.event.def.DefaultLoadEventListener - Error performing load command
org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.chatar.hibernate.receipes.example.domain.Book#DOES_NOT_EXIST]
at org.hibernate.impl.SessionFactoryImpl$2.handleEntityNotFound(SessionFactoryImpl.java:449)
at org.hibernate.event.def.DefaultLoadEventListener.returnNarrowedProxy(DefaultLoadEventListener.java:320)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:277)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:152)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1080)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:997)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:990)
at com.chatar.hibernate.receipes.example.LoadAndGetTest.main(LoadAndGetTest.java:21)
Exception in thread "main" org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.chatar.hibernate.receipes.example.domain.Book#DOES_NOT_EXIST]
at org.hibernate.impl.SessionFactoryImpl$2.handleEntityNotFound(SessionFactoryImpl.java:449)
at org.hibernate.event.def.DefaultLoadEventListener.returnNarrowedProxy(DefaultLoadEventListener.java:320)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:277)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:152)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1080)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:997)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:990)
at com.chatar.hibernate.receipes.example.LoadAndGetTest.main(LoadAndGetTest.java:21)
但是如果我刪除調用它來加載,然後我可以在輸出中看到book : null
。
LoadAndGetTest.java
無需調用load
public class LoadAndGetTest {
public static void main(String[] args) {
Configuration configuration = new Configuration().configure();
SessionFactory factory = configuration.buildSessionFactory();
Session session = factory.openSession();
Book book = (Book) session.get(Book.class, "DOES_NOT_EXIST");
System.out.println("book : "+ book);
}
}
輸出
書:空
此外,當我叫get
前兩次和load
後,可看到一贈印之前,但而不是後一個。
這裏的LoadAndGetTest
再次
public class LoadAndGetTest {
public static void main(String[] args) {
Configuration configuration = new Configuration().configure();
SessionFactory factory = configuration.buildSessionFactory();
Session session = factory.openSession();
Book book1 = (Book) session.get(Book.class, "DOES_NOT_EXIST");
System.out.println("book : "+ book1);
try {
Book book = (Book) session.load(Book.class, "DOES_NOT_EXIST");
} catch(Exception exception) {
exception.printStackTrace();
}
Book book = (Book) session.get(Book.class, "DOES_NOT_EXIST");
System.out.println("book : "+ book);
}
}
輸出
book : null
1292 [main] INFO org.hibernate.event.def.DefaultLoadEventListener - Error performing load command
org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.chatar.hibernate.receipes.example.domain.Book#DOES_NOT_EXIST]
at org.hibernate.impl.SessionFactoryImpl$2.handleEntityNotFound(SessionFactoryImpl.java:449)
at org.hibernate.event.def.DefaultLoadEventListener.returnNarrowedProxy(DefaultLoadEventListener.java:320)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:277)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:152)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1080)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:997)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:990)
at com.chatar.hibernate.receipes.example.LoadAndGetTest.main(LoadAndGetTest.java:24)
Exception in thread "main" org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.chatar.hibernate.receipes.example.domain.Book#DOES_NOT_EXIST]
at org.hibernate.impl.SessionFactoryImpl$2.handleEntityNotFound(SessionFactoryImpl.java:449)
at org.hibernate.event.def.DefaultLoadEventListener.returnNarrowedProxy(DefaultLoadEventListener.java:320)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:277)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:152)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1080)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:997)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:990)
at com.chatar.hibernate.receipes.example.LoadAndGetTest.main(LoadAndGetTest.java:24)
好吧,得問:你肯定**'書:null'是不是在第一種情況下?請記住,異常堆棧跟蹤去'System.err'和要打印'書:對'System.out' null'。這是可能的,由於緩衝等,那'書:null'可能出現* *前的異常堆棧跟蹤,因爲這兩個流不一定同步。 – dcsohl
是的 - 我確定它不在那裏。我知道兩個流可能不同步,但我期望'book:null'被打印出來 – TheCodingFrog