Hibernate用列表更新POJO,将ArrayList弄乱了

时间:2019-04-20 11:52:34

标签: java hibernate

我正在尝试运行休眠更新。它不更新现有记录,而是追加新记录,而使旧记录的索引为空。我发现它一直使用ArrayList而不是List。如何使用列表保留POJO?

这是我的POJO

public class Event implements Serializable{
private static final long serialVersionUID = 1L;
private long oid;
private String address;
private String date;
private List<Fight> fights;
public void setFights(List<Fight> _fights)
{
    this.fights=_fights;
}
public List<Fight> getFights()
{
    return this.fights;
}

public void copy(Event _evt)
{
    this.address=_evt.getAddress();
    this.date=_evt.getDate();
    //this.fights=(List<Fight>)_evt.getFights();
    this.setFights(_evt.getFights());
}
}

hbm.xml

  <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>

GetEvtByDateAddress

    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)
        {
            //The event does not exist
            return null;
        }
        else
        {
            return evt;
        }
    }
    catch(Exception e)
    {
        //System.out.println(e.toString())
        return null;
    }
}

更新代码: 从调试器 _event.fights是ArrayList,从数据库检索的tmpEvent.fights是PersistenceList。 在tmpEvent.copy(_event)之后,tmpEvent.fights成为ArrayList。这会导致问题。如何将tmpEvent.fights保留为列表?

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.copy(_event);
            Transaction tx =session.beginTransaction();
            session.update(tmpEvent);
            session.flush();
            tx.commit();
            session.close();
            //return success
            return 0;
        }
        else
        {
            //doesnt exist
        }
    }
    catch(Exception e){
        return -254;
    }
}

主要

    String date = "11 Jan 2018";
    List<Fight> fights=new ArrayList<Fight>();
    Fight m1=new Fight(1,2);
    Fight m2=new Fight(3,4);
    Fight m3=new Fight(5,6);
    Fight m4=new Fight(7,8);
    fights.add(m1);
    fights.add(m2);
    fights.add(m3);
    fights.add(m4);
    Event event = new Event("address string", date, fights);

    EventDAO eventdao = new EventDAO();
    eventdao.updateEvent(event);

0 个答案:

没有答案