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.