-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Unable to force initialization of proxy objects - SOLVED
PostPosted: Thu Oct 14, 2010 3:06 pm 
Newbie

Joined: Thu Oct 14, 2010 2:10 pm
Posts: 2
Hi,

I'm brand new to Hibernate so sorry in advance if this a stupid question. I have included code that seems relevant - happy to provide any additional items that might be helpful in determining a solution.

I have to write a mock web application that manages university course enrollment. There is a domain object that represents an academic department, and a simple DAO that (among other things) can return a list of all departments. I would like this list and its objects to be initialized within the method that creates it, so that the DAO can close the session and then return a usable list to other parts of the application.

The problem is that I cannot seem to force initialization of the objects in the list. Properties are all initialized to null. I have tried the “fetch all properties” clause in my HQL query, as well as calling Hibernate.initialize on each individual list object. Neither has helped.

I'm using annotation-based mapping. Here is an abridged version of my Department domain object:

Code:
@SuppressWarnings("serial")
@Entity
@Table(name="departments")
public class Department implements Serializable{
   private Long id;
   private String code; //A three-letter abbreviation of the department name
   private String name;
   private Set<Course> coursesOffered;
   
   public Department(){}
   
   @Id
   @GeneratedValue
   public Long getId(){
      return id;
   }
   
   protected void setId(Long id){
      this.id = id;
   }
   
   @Basic(optional=false)
   @Column(unique=true)
   public String getCode(){
      return code;
   }
   
   public void setCode(String code){
      this.code = code.toUpperCase();
   }
   
   @Basic(optional=false)
   public String getName(){
      return name;
   }
   
   public void setName(String name){
      this.name = name;
   }
   
   @OneToMany(mappedBy="offeredBy", targetEntity=Course.class, fetch=FetchType.EAGER)
   Set<Course> getCoursesOffered(){
      return coursesOffered;
   }
   
   void setCoursesOffered(Set<Course> coursesOffered){
      this.coursesOffered = coursesOffered;
   }
   
   @Override
   public boolean equals(Object other){
      //omitted for brevity
   }
   
   @Override
   public int hashCode(){
      //omitted for brevity
   }
}


And here is the method in the DAO that creates the list. Note that if I debug this and inspect a list item after the transaction, I will see a CGLIB proxy whose fields are all null.

Code:
public List<Department> getAllDepartments() {

   ArrayList<Department> result = new ArrayList<Department>();
   Object buf;
   Session s = sessionFactory.openSession();
   
   s.beginTransaction();
   
   Query q = s.createQuery("from Department fetch all properties");
   
   Iterator i = q.iterate();
   
   while (i.hasNext()){
      buf = i.next();
      Hibernate.initialize(buf);
      result.add((Department) buf);
   }
      
   s.getTransaction().commit();

   s.close();
         
   return result;
}


My session factory is wired via Spring. I haven't used a separate Hibernate configuration file because it seems that I can provide all relevant configuration to the session factory bean via the Spring configuration, which looks like this:

Code:
<bean id="sessionFactory"
   class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
   <property name="dataSource" ref="dataSource" />
   <property name="annotatedClasses">
      <list>
         <value>bg.cos476.a1.domain.Course</value>
         <value>bg.cos476.a1.domain.Department</value>
         <value>bg.cos476.a1.domain.Student</value>
      </list>
   </property>
   <property name="hibernateProperties">
      <props>
         <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
         <prop key="hibernate.id.new_generator_mappings">true</prop>
      </props>
   </property>
</bean>


Grateful for any advice anyone can lend.


Top
 Profile  
 
 Post subject: Re: Unable to force initialization of proxy objects - SOLVED
PostPosted: Thu Oct 14, 2010 10:21 pm 
Newbie

Joined: Thu Oct 14, 2010 2:10 pm
Posts: 2
Sorry, please disregard. I was so absorbed with the debugger I didn't bother to actually run the code and see if it works, which it does. Although the field variables within the proxy object are null, the property accessors work fine even after the session has been closed. Presumably there is some subtlety of proxies of which I am not aware that would explain this.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.