Hi everybody.
I have a problem witth
Illegal attempt to associate a collection with two open sessionsSomeone can help me please ?
This is my genericDao
Code:
public interface GenericDao<T, PK extends Serializable> {
PK save(T newInstance);
T findById(PK id);
List<T> findAll();
void update(T transientObject);
void remove(T persistentObject);
void saveOrUpdate(T object);
}
and my genericdaoImpl
Code:
public class GenericDaoImpl<T, PK extends Serializable> implements GenericDao<T, PK> {
@Autowired
private SessionFactory sessionFactory;
public GenericDaoImpl() {
super();
}
private Class<T> type;
public void saveOrUpdate(T o) {
try {
getSession().saveOrUpdate(o);
} catch (HibernateException e) {
e.printStackTrace();
}
}
public GenericDaoImpl(Class<T> type) {
this.type = type;
}
public PK save(T o) {
return (PK) getSession().save(o);
}
public T findById(PK id) {
return (T) getSession().get(type, id);
}
public List<T> findAll() {
Criteria crit = getSession().createCriteria(type);
crit.add(Restrictions.eq("enabled", new Boolean(true)));
return crit.list();
}
public void update(T o) {
getSession().update(o);
}
public void merge(T o) {
getSession().merge(o);
}
public void remove(T o) {
getSession().delete(o);
}
public Session getSession() {
boolean allowCreate = true;
return SessionFactoryUtils.getSession(sessionFactory, allowCreate);
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
and my class impl
Code:
@Transactional
@Service("SfdService")
public class SfdServiceImpl extends GenericDaoImpl<Sfd, Integer>implements SfdService,Serializable{
public SfdServiceImpl(Class<Sfd> type) {
super(type);
}
public SfdServiceImpl() {
super();
}
}
Wierdly If I do this:
Sfd sfd=sfdService.findById(1);
sfd.setNom("changeName");
sfdService.save(sfd);I got an error "
org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions"
i suspect my getsession because i think that he create a new session everytime.
Someone can help me please ???