Hvorfor kan jeg ikke få den exception (spring)
Jeg har et kørende spring/hibernate program, jeg kan ligge en zipcode og en customer ned i en DB. Det virker.Men hvis jeg nu forsøger at hente en customer op, som ikke findes, så går det galt.
/// kode ///
public class CustomerDAO {
private HibernateOperations hibernateOperations;
public Customer retrieveCustomer(long customerID) throws CustomerDoesNotExists {
Customer customer;
customer = (Customer) hibernateOperations.load(Customer.class, customerID);
// if (hibernateOperations.contains(customer))
// throw new CustomerDoesNotExists();
return customer;
}
/// slut ///
jeg får denne exception:
log4j:WARN No appenders could be found for logger (org.springframework.beans.factory.xml.XmlBeanDefinitionReader).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:56)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:98)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:158)
at cws.entity.Customer$$EnhancerByCGLIB$$94e98969.toString(<generated>)
at conf.Main.run(Main.java:39)
at conf.Main.main(Main.java:43)
derfor vil jeg fange en exception, og derfra kalde min egen.
hvis jeg læser i hibernate dok,
http://static.springframework.org/spring/docs/1.1.5/api/org/springframework/orm/hibernate/HibernateOperations.html#load(java.lang.Class,%20java.io.Serializable)
så vil hibernate.... load, give mig en
HibernateObjectRetrievalFailureException
hvis en instance ikke findes. Men hvis jeg omgiver min kode, med en try catch block:
public Customer retrieveCustomer(long customerID) throws CustomerDoesNotExists {
Customer customer = null;
try {
customer = (Customer) hibernateOperations.load(Customer.class, customerID);
} catch (HibernateObjectRetrievalFailureException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
throw new CustomerDoesNotExists();
}
return customer;
}
så få jeg samme fejl: altså min try catch bloc er ikke blevet kalt.
log4j:WARN No appenders could be found for logger (org.springframework.beans.factory.xml.XmlBeanDefinitionReader).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:56)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:98)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:158)
at cws.entity.Customer$$EnhancerByCGLIB$$84c91598.toString(<generated>)
at conf.Main.run(Main.java:39)
at conf.Main.main(Main.java:43)
Hvordan skal jeg fedt min egen lille exception ind ?
