小编Jav*_*ava的帖子

如何解决 org.hibernate.NonUniqueObjectException:具有相同标识符值的不同对象已与会话关联:

我正在List使用 hibernate 保存 a ,但它会引发以下异常:

org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:
Run Code Online (Sandbox Code Playgroud)

我正在使用的代码如下,但我不知道为什么它会引发异常:

public void save(List<UserItem> list)
{
    //getHibernateTemplate().saveOrUpdateAll(list);

    //getHibernateTemplate().deleteAll(list);
    sessFactory = getHibernateTemplate().getSessionFactory();
    Session session = sessFactory.getCurrentSession();
    for (UserItem bean : list) {
        session.saveOrUpdate(bean);
    }
}
Run Code Online (Sandbox Code Playgroud)

正确的保存方法是什么List

java hibernate

5
推荐指数
1
解决办法
1万
查看次数

如何在Java中将String转换为jsonObject?

这是jsonObject的字符串

{"no":1000,"name":"xxx","code":345}
Run Code Online (Sandbox Code Playgroud)

我想将此字符串转换为JSONObject。在这里编码

try {
    URL url = new URL("http://localhost:8090/search?numer="+no);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    conn.setRequestProperty("Accept", "application/json");
    if (conn.getResponseCode() != 200) {
        throw new RuntimeException("Failed : HTTP error code : "+ conn.getResponseCode());
    }

    BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
    String output ;
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
        System.out.println(output);//output:{"no":1000,"name":"xxx","code":345}
    }
    conn.disconnect();

} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

java

-1
推荐指数
1
解决办法
8858
查看次数

标签 统计

java ×2

hibernate ×1