I have a problem with session factory and saving objects to the database using hibernate.
The problem is that when I use sessionFactory.getCurrentSession().save(object), nothing is persisted. If I use a flush() after the save the object is persisted, but the next time an object is saved to the database a copy of the former object is saved again with a new id, together with the new object. Has anyone encountered this before?
Method for persisting object:
public class HibernateDataStatisticsStore extends HibernateGenericStore<DataStatisticsEvent> implements DataStatisticsStore
{
@Override
public int addDataStatisticsEvent( DataStatisticsEvent dataStatistics )
{
int id = (Integer) sessionFactory.getCurrentSession().save(dataStatistics);
sessionFactory.getCurrentSession().flush();
return id;
}
are you testing this through the UI - if so did you remember to add
@Transactional
to the service layer class to enable transactions? This is necessary for modifications to be persisted in db.
regards,
Lars
···
On Mon, Feb 8, 2016 at 3:57 PM, Yrjan Aleksander Frøyland Fraschetti yrjanaff@gmail.com wrote:
I have a problem with session factory and saving objects to the database using hibernate.
The problem is that when I use sessionFactory.getCurrentSession().save(object), nothing is persisted. If I use a flush() after the save the object is persisted, but the next time an object is saved to the database a copy of the former object is saved again with a new id, together with the new object. Has anyone encountered this before?
Method for persisting object:
public class HibernateDataStatisticsStore extends HibernateGenericStore<DataStatisticsEvent> implements DataStatisticsStore
{
@Override
public int addDataStatisticsEvent( DataStatisticsEvent dataStatistics )
{
int id = (Integer) sessionFactory.getCurrentSession().save(dataStatistics);
sessionFactory.getCurrentSession().flush();
return id;
}
We are testing it through the UI, and we had forgotten to use @Transactional. Unfortunately it seems that @Transaction has the same effect as flush(). Any ideas?
Regards,
Yrjan Fraschetti
···
On Mon, Feb 8, 2016 at 3:57 PM, Yrjan Aleksander Frøyland Fraschetti yrjanaff@gmail.com wrote:
I have a problem with session factory and saving objects to the database using hibernate.
The problem is that when I use sessionFactory.getCurrentSession().save(object), nothing is persisted. If I use a flush() after the save the object is persisted, but the next time an object is saved to the database a copy of the former object is saved again with a new id, together with the new object. Has anyone encountered this before?
Method for persisting object:
public class HibernateDataStatisticsStore extends HibernateGenericStore<DataStatisticsEvent> implements DataStatisticsStore
{
@Override
public int addDataStatisticsEvent( DataStatisticsEvent dataStatistics )
{
int id = (Integer) sessionFactory.getCurrentSession().save(dataStatistics);
sessionFactory.getCurrentSession().flush();
return id;
}
There has to something wrong with your business logic - I have never heard about this problem before
···
On Mon, Feb 8, 2016 at 5:27 PM, Yrjan Aleksander Frøyland Fraschetti yrjanaff@gmail.com wrote:
We are testing it through the UI, and we had forgotten to use @Transactional. Unfortunately it seems that @Transaction has the same effect as flush(). Any ideas?
Regards,
Yrjan Fraschetti
feb. 2016 kl. 15.59 skrev Lars Helge Øverland lars@dhis2.org:
Hi,
are you testing this through the UI - if so did you remember to add
@Transactional
to the service layer class to enable transactions? This is necessary for modifications to be persisted in db.
regards,
Lars
–
On Mon, Feb 8, 2016 at 3:57 PM, Yrjan Aleksander Frøyland Fraschetti yrjanaff@gmail.com wrote:
I have a problem with session factory and saving objects to the database using hibernate.
The problem is that when I use sessionFactory.getCurrentSession().save(object), nothing is persisted. If I use a flush() after the save the object is persisted, but the next time an object is saved to the database a copy of the former object is saved again with a new id, together with the new object. Has anyone encountered this before?
Method for persisting object:
public class HibernateDataStatisticsStore extends HibernateGenericStore<DataStatisticsEvent> implements DataStatisticsStore
{
@Override
public int addDataStatisticsEvent( DataStatisticsEvent dataStatistics )
{
int id = (Integer) sessionFactory.getCurrentSession().save(dataStatistics);
sessionFactory.getCurrentSession().flush();
return id;
}