Hibernate SaveOrUpdate不会插入

时间:2014-09-15 22:41:13

标签: hibernate

我的要求是,如果没有数据则插入,如果有数据则更新它。

我有一个json数据作为输入,我的休息呼叫中的数据接收是:

带有json数据的服务类:

mergeAccountDAO.mergeDataList(iterateJson(content));
....
JSONArray jsonArray = (JSONArray) jsonObjectInstance;
for(int i=0; i<jsonArray.size(); i++ ){
   JSONObject jsonObject= (JSONObject) jsonArray.get(i);
   mergeAccountList = buildContent(stringBuilder,jsonObject);
}
List<MergeAccount> mergeAccountList = new ArrayList<MergeAccount>();
private List<MergeAccount> buildContent(StringBuilder stringBuilder, JSONObject jsonObject) {
MergeAccount mergeAccount = new MergeAccount();
String idValue = (jsonObject.get("ID"))==null?"":jsonObject.get("ID").toString();
int id = 0;
if(idValue != null && !idValue.equals("") ){
    id = Integer.parseInt(idValue);
}
...

mergeAccount.setId(id);
mergeAccountList.add(mergeAccount);
return mergeAccountList;

DAO课程:

@Autowired
private SessionFactory sessionFactory;
public String mergeDataList(List<MergeAccount> mergeAccountList) throws DIException {
    session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    for (MergeAccount mergeAccount : mergeAccountList) {
        session.saveOrUpdate(mergeAccount);
     }
     tx.commit();
     session.close();       
     return "Success";
}

POJO:

   @Entity
@Table(name="SIAS_EARNING_ACCOUNT_MAP")
public class MergeAccount  implements Serializable{

    @Id
    @Column(name="EARNING_ACCOUNT_ID", unique = true, nullable = false)
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;


    @Column(name = "AS_ACCOUNT_NAME")
    private String accountName;

    @Column(name = "AS_ACCOUNT_TYPE")
    private String accountType;

    @Column(name = "ACCT_SYSTEM_ID")
    private String accountSystemId;

    @Column(name = "ACCESS_ID")
    private Integer accessId;

    @Column(name = "EMPLID")    
    private String employeeId;

    @Column(name = "CREATED_BY")
    private String createdBy;

    @Column(name = "ERNCD") 
    private String erncd;

    @Column(name = "ERNCD_DESCRIPTION")
    private String erncdDesc;

    @Column(name = "MAPPING_ACTIVE")
    private String mappingActive;

    @Column(name = "AS_ACCOUNT_ACTIVE")
    private String accountActive;

    @Column(name = "MAPPING_EFFECTIVE_DATE")
    private Date mappingEffDate ;

    @Column(name = "MAPPING_NAME")
    private String mappingName;

    @Column(name = "MAPPING_TYPE")
    private String mappingType;

    @Column(name = "TRINET_COMPANY")
    private String trinetCompany;

    @Column(name = "UPDATED_BY") 
    private String updatedBy;

    @Column(name = "UPDATED_DATE")
    private Date updateDate ;

    @Column(name = "CREATED_DATE")
    private Date createdDate ;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

          //getter/setter for all fields

控制台:

Hibernate: update SIAS_EARNING_ACCOUNT_MAP set ACCESS_ID=?, AS_ACCOUNT_ACTIVE=?, AS_ACCOUNT_NAME=?, ACCT_SYSTEM_ID=?, AS_ACCOUNT_TYPE=?, CREATED_BY=?, CREATED_DATE=?, EMPLID=?, ERNCD=?, ERNCD_DESCRIPTION=?, MAPPING_ACTIVE=?, MAPPING_EFFECTIVE_DATE=?, MAPPING_NAME=?, MAPPING_TYPE=?, TRINET_COMPANY=?, UPDATED_DATE=?, UPDATED_BY=? where EARNING_ACCOUNT_ID=?
Hibernate: update SIAS_EARNING_ACCOUNT_MAP set ACCESS_ID=?, AS_ACCOUNT_ACTIVE=?, AS_ACCOUNT_NAME=?, ACCT_SYSTEM_ID=?, AS_ACCOUNT_TYPE=?, CREATED_BY=?, CREATED_DATE=?, EMPLID=?, ERNCD=?, ERNCD_DESCRIPTION=?, MAPPING_ACTIVE=?, MAPPING_EFFECTIVE_DATE=?, MAPPING_NAME=?, MAPPING_TYPE=?, TRINET_COMPANY=?, UPDATED_DATE=?, UPDATED_BY=? where EARNING_ACCOUNT_ID=?

我的json文件:

[
{
"ID":"101",
"Company":"BEG22",
"Account":"1004",
"Deptid":"13",
"Location":"SM"
...
},

{
"ID":"102",
"Company":"BEG22",
"Account":"1004",
"Deptid":"13",
"Location":"SM"
...
}
]

每当我一次又一次地运行时,没有数据被插入,因为它只运行update语句。 db中的数据为空。

修改

列EARNING_ACCOUNT_ID是oracle表中的序列。总是有一些序列号。

1 个答案:

答案 0 :(得分:0)

SaveOrUpdate基于ID工作。

如果有ID,请更新 否则插入。