Problem with session factory and hibernate

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;

    }

Hibernate-mapping:

<hibernate-mapping>
  <class name="org.hisp.dhis.datastatistics.DataStatisticsEvent" table="datastatisticsevent">
    <cache usage="read-write" />
    <id name="id" column="eventid">
      <generator class="native" />
    </id>
    <property name="type" column="eventtype">
      <type name="org.hibernate.type.EnumType">
        <param name="enumClass">org.hisp.dhis.datastatistics.EventType</param>
      </type>
    </property>
    <property name="timestamp" column="timestamp" type="java.util.Date"></property>
    <property name="userId" column="userid" type="int" ></property>
  </class>
</hibernate-mapping>

Regards,

Yrjan Fraschetti

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;

    }

Hibernate-mapping:

<hibernate-mapping>
  <class name="org.hisp.dhis.datastatistics.DataStatisticsEvent" table="datastatisticsevent">
    <cache usage="read-write" />
    <id name="id" column="eventid">
      <generator class="native" />
    </id>
    <property name="type" column="eventtype">
      <type name="org.hibernate.type.EnumType">
        <param name="enumClass">org.hisp.dhis.datastatistics.EventType</param>
      </type>
    </property>
    <property name="timestamp" column="timestamp" type="java.util.Date"></property>
    <property name="userId" column="userid" type="int" ></property>
  </class>
</hibernate-mapping>

Regards,

Yrjan Fraschetti


Mailing list: https://launchpad.net/~dhis2-devs

Post to : dhis2-devs@lists.launchpad.net

Unsubscribe : https://launchpad.net/~dhis2-devs

More help : https://help.launchpad.net/ListHelp

Lars Helge Øverland

Lead developer, DHIS 2

University of Oslo

Skype: larshelgeoverland

http://www.dhis2.org

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;

    }

Hibernate-mapping:

<hibernate-mapping>
  <class name="org.hisp.dhis.datastatistics.DataStatisticsEvent" table="datastatisticsevent">
    <cache usage="read-write" />
    <id name="id" column="eventid">
      <generator class="native" />
    </id>
    <property name="type" column="eventtype">
      <type name="org.hibernate.type.EnumType">
        <param name="enumClass">org.hisp.dhis.datastatistics.EventType</param>
      </type>
    </property>
    <property name="timestamp" column="timestamp" type="java.util.Date"></property>
    <property name="userId" column="userid" type="int" ></property>
  </class>
</hibernate-mapping>

Regards,

Yrjan Fraschetti


Mailing list: https://launchpad.net/~dhis2-devs

Post to : dhis2-devs@lists.launchpad.net

Unsubscribe : https://launchpad.net/~dhis2-devs

More help : https://help.launchpad.net/ListHelp

Lars Helge Øverland

Lead developer, DHIS 2

University of Oslo

Skype: larshelgeoverland

http://www.dhis2.org

The call to .flush should be removed.

There has to something wrong with your business logic - I have never heard about this problem before :wink:

···

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

  1. 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;

    }

Hibernate-mapping:

<hibernate-mapping>
  <class name="org.hisp.dhis.datastatistics.DataStatisticsEvent" table="datastatisticsevent">
    <cache usage="read-write" />
    <id name="id" column="eventid">
      <generator class="native" />
    </id>
    <property name="type" column="eventtype">
      <type name="org.hibernate.type.EnumType">
        <param name="enumClass">org.hisp.dhis.datastatistics.EventType</param>
      </type>
    </property>
    <property name="timestamp" column="timestamp" type="java.util.Date"></property>
    <property name="userId" column="userid" type="int" ></property>
  </class>
</hibernate-mapping>

Regards,

Yrjan Fraschetti


Mailing list: https://launchpad.net/~dhis2-devs

Post to : dhis2-devs@lists.launchpad.net

Unsubscribe : https://launchpad.net/~dhis2-devs

More help : https://help.launchpad.net/ListHelp


Lars Helge Øverland

Lead developer, DHIS 2

University of Oslo

Skype: larshelgeoverland

http://www.dhis2.org

Lars Helge Øverland

Lead developer, DHIS 2

University of Oslo

Skype: larshelgeoverland

http://www.dhis2.org