My association is bi-directional meaning i can add a new group msg by either saving the message itself or adding the msg to the msg list of the group it belongs and then updating the group in the database and the code for both approaches if below
public void saveGroupMessage(GroupMessage groupMessage) throws Exception { getHibernateTemplate().save(groupMessage); getHibernateTemplate().refresh(group); }
OR
public void saveGroupMessage(GroupMessage groupMessage, UserGroup group) throws Exception {
List<GroupMessage> msgs = group.getGroupMessages(); msgs.add(groupMessage); group.setGroupMessages(msgs); getHibernateTemplate().update(group); getHibernateTemplate().refresh(group);
}
The reason i call group.setGroupMessages(msgs) is because the parameter am passing is a list of msgs includiing a newly added msg. By the way, the msg gets saved in the database coz i see it there, and everything works fine when this collection is not set to lazy, the problem only shows up when i set it to lazy, then i try to call Hibernate.initialise(Object) manually(With a new session), the list of msgs gets initialised but never includes the newly added msg even though it actually exists in the database, unless i restart the web server. Am realising that spring's hibernate templete class is the problem, because if i look up the list of msgs when managing my own hibernate session, the new msg shows up, is think i have to find a way of synhronizing spring's hibernateTemplate with the new session i created when intialiaising the lazy collection or with the database itself, what do u think guys? I appreciate any help.
|