0

Event.hbm.xml one-to-many relationship for event to fights

<class name="Event" table="event">
  <id name="oid" type="long" column="oid">
  <generator class="increment">
     <param name="initial_value">1</param>
  </generator>
  </id>
<property name="address">
  <column name="address"/>
</property>
<property name="date">
   <column name="date"/>   
</property>
  <list name="fights" cascade="all">  
          <key column="eventOid"></key>  
          <index column="type"></index>  
          <one-to-many class="Fight"/>  
 </list>  
</class>

Retrieve existing Event

    public Event getEvtByDateAddress(String _date, String _address)
{
    try
    {
        if(!session.isOpen())
        {
            session = HibernateUtil.getSessionFactory().openSession();
        }
        session.beginTransaction();
        Criteria criteria = session.createCriteria(Event.class);
        criteria.add(Restrictions.eq("date", _date));
        criteria.add(Restrictions.eq("address", _address));
        Event evt = (Event)criteria.uniqueResult();
        if (evt==null)
        {
            return null;
        }
        else
        {
            return evt;
        }
    }
    catch(Exception e)
    {
        return null;
    }
}

The update is called.

    public int updateEvent(Event _event)
{
    try
    {
        if(!session.isOpen())
        {
            session = HibernateUtil.getSessionFactory().openSession();
        }
        Event tmpEvent=new Event();
        if((tmpEvent=this.getEvtByDateAddress(_event.getDate(), _event.getAddress()))!=null)
        {
            tmpEvent.setFights(_event.getFights());
            Transaction tx =session.beginTransaction();
            session.update(tmpEvent);
            session.flush();
            tx.commit();
            session.close();
            return 0;
        }
        else
        {
            return -1;
        }
    }
    catch(Exception e){
        return -254;
    }
}

After the update, results unexpected. New records are inserted instead of updated.

oid boxer1Oid   boxer2Oid   eventOid    type
  1         1           3         \N    \N
  2         2           4         \N    \N
  3         5           6         \N    \N
  4         7           8         \N    \N
  5         1           2          1    0
  6         3           4          1    1
  7         5           6          1    2
  8         7           8          1    3

Expected Result

oid boxer1Oid   boxer2Oid   eventOid    type
  1         1           2          1     0
  2         3           4          1     1
  3         5           6          1     2
  4         7           8          1     3

I simply retrieve the existing records and modify it, and it results unexpectedly.

if((tmpEvent=this.getEvtByDateAddress(_event.getDate(),_event.getAddress()))!=null)

            tmpEvent.setFights(_event.getFights());
            -------
            session.update(tmpEvent);

What am I missing to get the expected result?

Thanks

1 Answer 1

1

Make sure this.getEvtByDateAddress sets the oid of the entity returned by getFights.

Also, do not create a new event. Event tmpEvent=new Event(); can just be Event tmpEvent = null;

Sign up to request clarification or add additional context in comments.

4 Comments

From the debugger, tmpEvent=this.getEvtByDateAddress, where the fights of tmpEvent is a PersistentList. Is it an expected result?
What are the oid values of the entities returned by getFights()?
_event.getFights() the oid of it are all 0.
And that is your problem. If the oid is 0, hibernate will insert instead of update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.