Announcement Announcement Module
Collapse
No announcement yet.
Null proxy with Lazy loading Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Null proxy with Lazy loading

    I'm lazy loading a class from a manytoone relationship. It all looks fine but when I call a method on the class I get a NullPointerException.

    Here's part of my class:
    Code:
    @Entity
    @Table(name="tblOrder" )
    public class Order {
    
    	@Id
    	@Column(name="intId")
    	@GeneratedValue
    	private Integer orderId;
    
    	@OneToOne(cascade={ALL}, fetch = FetchType.LAZY)
    	@JoinColumn(name="intDeliveryId", referencedColumnName="intId")
    	private OrderDeliveryDetails deliveryDetails;
    
    	@ManyToOne(fetch = FetchType.LAZY)
    	@JoinColumn(name="intCustomerId", referencedColumnName="intId")
    	private Customer customer;
    The deliveryDetails seems to lazy load just fine but the Customer object returns Null.

    Setup: hibernate-3.2.4.sp1, hibernate-annotations-3.3.0.GA,
    spring-framework-2.0.6, OpenSessionInViewFilter, Tomcat 5.5

    I use:
    Code:
    getHibernateTemplate().findByCriteria(criteria)
    Then I've got some debugging code:
    Code:
    logger.debug("$$$ Order id "+order.getOrderId());
    	logger.debug("$$$ Order status "+order.getOrderStatus().getOrderStatusName());
    	logger.debug("$$$ Order deliveryDetails "+order.getDeliveryDetails());
    	logger.debug("$$$ Order customer "+order.getCustomer());
    	if (order.getDeliveryDetails()!= null) {
    		logger.debug("$$$ Order deliveryDetails email "+order.getDeliveryDetails().getEmail());
    	}
    	if (order.getCustomer() == null) {
    		logger.debug("$$$ Customer is null$$");
    	}
    	logger.debug("$$$ Order customer id "+order.getCustomer().getId());
    	logger.debug("$$$ Order customer "+order.getCustomer().getCompany().getName());
    and a shortened log looks like this:

    Code:
    2007-07-19 08:42:24,578 [http-8080-Processor25] DEBUG internet.comp.admin.orders.OrderAdmin - $$$ Order id 14
    2007-07-19 08:42:24,578 [http-8080-Processor25] DEBUG internet.comp.admin.orders.OrderAdmin - $$$ Order status PROCESSING
    2007-07-19 08:42:24,578 [http-8080-Processor25] DEBUG org.hibernate.impl.SessionImpl - initializing proxy: [internet.comp.framework.order.beans.OrderDeliveryDetails#14]
    2007-07-19 08:42:24,578 [http-8080-Processor25] DEBUG org.hibernate.event.def.DefaultLoadEventListener - attempting to resolve: [internet.comp.framework.order.beans.OrderDeliveryDetails#14]
    2007-07-19 08:42:24,578 [http-8080-Processor25] DEBUG org.hibernate.event.def.DefaultLoadEventListener - object not resolved in any cache: [internet.comp.framework.order.beans.OrderDeliveryDetails#14]
    2007-07-19 08:42:24,578 [http-8080-Processor25] DEBUG org.hibernate.persister.entity.AbstractEntityPersister - Fetching entity: [internet.comp.framework.order.beans.OrderDeliveryDetails#14]
    --- GAP ---
    2007-07-19 08:42:24,578 [http-8080-Processor25] DEBUG org.hibernate.loader.Loader - loading entity: [internet.comp.framework.order.beans.OrderDeliveryDetails#14]
    2007-07-19 08:42:24,703 [http-8080-Processor25] DEBUG org.hibernate.engine.TwoPhaseLoad - done materializing entity [internet.comp.framework.order.beans.OrderDeliveryDetails#14]
    2007-07-19 08:42:24,703 [http-8080-Processor25] DEBUG org.hibernate.engine.StatefulPersistenceContext - initializing non-lazy collections
    2007-07-19 08:42:24,703 [http-8080-Processor25] DEBUG org.hibernate.loader.Loader - done entity load
    2007-07-19 08:42:24,703 [http-8080-Processor25] DEBUG internet.comp.admin.orders.OrderAdmin - $$$ Order deliveryDetails internet.comp.framework.order.beans.OrderDeliveryDetails@16b69d7
    2007-07-19 08:42:24,703 [http-8080-Processor25] DEBUG org.hibernate.impl.SessionImpl - initializing proxy: [internet.comp.customer.framework.beans.Customer#2]
    2007-07-19 08:42:24,703 [http-8080-Processor25] DEBUG org.hibernate.event.def.DefaultLoadEventListener - attempting to resolve: [internet.comp.customer.framework.beans.Customer#2]
    2007-07-19 08:42:24,703 [http-8080-Processor25] DEBUG org.hibernate.event.def.DefaultLoadEventListener - object not resolved in any cache: [internet.comp.customer.framework.beans.Customer#2]
    2007-07-19 08:42:24,703 [http-8080-Processor25] DEBUG org.hibernate.persister.entity.AbstractEntityPersister - Fetching entity: [internet.comp.customer.framework.beans.Customer#2]
    --- GAP ---
    2007-07-19 08:42:24,828 [http-8080-Processor25] DEBUG org.hibernate.engine.TwoPhaseLoad - done materializing entity [internet.comp.customer.framework.beans.Customer#2]
    2007-07-19 08:42:24,828 [http-8080-Processor25] DEBUG org.hibernate.engine.StatefulPersistenceContext - initializing non-lazy collections
    2007-07-19 08:42:24,828 [http-8080-Processor25] DEBUG org.hibernate.loader.Loader - done entity load
    2007-07-19 08:42:24,828 [http-8080-Processor24] DEBUG internet.comp.admin.orders.OrderAdmin - $$$ Order customer internet.comp.customer.framework.beans.Customer@15a142f
    2007-07-19 08:42:24,828 [http-8080-Processor24] DEBUG internet.comp.admin.orders.OrderAdmin - $$$ Order deliveryDetails email [email protected]
    2007-07-19 08:42:24,828 [http-8080-Processor24] DEBUG internet.comp.admin.orders.OrderAdmin - $$$ Order customer id null
    java.lang.NullPointerException
    	at internet.comp.admin.orders.OrderAdmin.execute(OrderAdmin.java:45)
    	at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
    	at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
    	at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
    	at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
    Any advice will be greatly received!
    Last edited by Jas-e; Jul 19th, 2007, 03:37 AM.

  • #2
    Update

    Not heard from anyone.

    I thought I'd better explain that this is all a fairly standard setup. If you are very perceptive you would have noticed that
    Code:
    getHibernateTemplate().findByCriteria()
    returns a list and I was using a single row. Here's the missing code:

    Code:
    			List<Order> orders = dao.findByCriteria(adminForm);
    			if (orders.size()>0) {
    				Order order = orders.get(0);
    Any ideas?

    Comment


    • #3
      Oh, and I've looked at in the debugger. The CGLIB class is there but the method calls return Null. How wierd!

      Comment

      Working...
      X