如何以一对一的关系更新新孩子?

时间:2017-10-24 11:45:53

标签: hibernate hibernate-mapping hibernate-criteria

如何在丢失数据的情况下仅使用现有对象添加子项

@SuppressWarnings("unchecked")
public void add_hotspot(int pano_id, JSONObject j_hotspot) {

    Query query = ssf.getCurrentSession().createQuery("from Panoramas where pano_id= :pano_id");

    query.setParameter("pano_id", pano_id);

    List list = query.list();

    List<Panoramas> panorama = (List<Panoramas>) list;

    Panoramas panoramas = panorama.get(0);

    Set<Hotspot> hotspot_set = new HashSet<Hotspot>();

    try {

        Hotspot hotspot = new Hotspot();
        hotspot.setPitch(Float.parseFloat(j_hotspot.getString("pitch")));
        hotspot.setYaw(Float.parseFloat(j_hotspot.getString("yaw")));
        hotspot.setSource_id(Integer.parseInt(j_hotspot.getString("target_id")));
        hotspot.setView_type("Description");
        hotspot_set.add(hotspot);      
        panoramas.setHotspot(hotspot_set);
    } catch (Exception e) {
        e.printStackTrace();
    }

    Session session = ssf.openSession();
    session.beginTransaction();
    session.merge(panoramas);
    session.getTransaction().commit();
    session.close();    
}

0 个答案:

没有答案