Reputation: 13530
There are User
,Person
and Employee
entities.
User
knows about both Person
and Employee
.
hibernate config for User:
...
<many-to-one name="person" class="entry.Person" column="perid" not-null="true" cascade="all"/>
<many-to-one name="employee" class="entry.Employee" column="empid" not-null="true" cascade="all"/>
...
DAO:
public List<User> getUsers() {
return getHibernateTemplate().find("from User");
}
When fetch users with getUsers()
,that Person
and Employee
data is NULL
.
Have I missed some .hbm
configuration or I should manually add more subselects to DAO
?
EDIT
context:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource"/>
</property>
...
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<bean id="userDao" class="dao.UserDaoHibernate">
<property name="factory">
<ref local="sessionFactory"/>
</property>
</bean>
dao:
private SessionFactory factory;
public void setFactory(SessionFactory factory) {
this.factory = factory;
}
@Transactional
public List<User> getUsers() {
Session s = this.factory.getCurrentSession();
...
got No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
,when try to execute getCurrentSession()
Upvotes: 1
Views: 859
Reputation: 9265
By default, associations in Hibernate are lazy fetched unless otherwise specified in your Hibernate configuration, or initialized in code. Eager fetching in configuration is not encouraged, to avoid unexpected consequences. In your code above, you will need to explicitly initialize the Person
and Employee
associations.
Since you are using Spring, for coding ease you should stop using the HibernateTemplate
(which hasn't been recommended to use for a while), and move over to using Hibernate directly. Then, you can use Hibernate.initialize
on the associations (or some other method, like setting FetchMode
) to initialize them.
Upvotes: 1