My requirement is, if there is no data then insert, if there is a data then update it.
I have a json data as input and data receiving in my rest call is:
Service class with json data:
Code:
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 class:
Code:
@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:
Code:
@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
Console:
Code:
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=?
my json file:
Code:
[
{
"ID":"101",
"Company":"BEG22",
"Account":"1004",
"Deptid":"13",
"Location":"SM"
...
},
{
"ID":"102",
"Company":"BEG22",
"Account":"1004",
"Deptid":"13",
"Location":"SM"
...
}
]
whenever I run again and again no data is getting inserted since it runs only update statement. Data is empty in db.