package com.cico.cicocashincashout.model.login.response;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Agent {
#SerializedName("id")
#Expose
private Integer id;
#SerializedName("first_name")
#Expose
private String firstName;
#SerializedName("last_name")
#Expose
private String lastName;
#SerializedName("user_id")
#Expose
private String userId;
#SerializedName("date_of_birth")
#Expose
private String dateOfBirth;
#SerializedName("email")
#Expose
private String email;
#SerializedName("gender")
#Expose
private String gender;
#SerializedName("business_name")
#Expose
private String businessName;
#SerializedName("business_address")
#Expose
private String businessAddress;
#SerializedName("business_phone")
#Expose
private String businessPhone;
#SerializedName("state_id")
#Expose
private Integer stateId;
#SerializedName("local_government_id")
#Expose
private Integer localGovernmentId;
#SerializedName("account_number")
#Expose
private String accountNumber;
#SerializedName("account_name")
#Expose
private String accountName;
#SerializedName("bank_id")
#Expose
private Integer bankId;
#SerializedName("bvn")
#Expose
private String bvn;
#SerializedName("agent_code")
#Expose
private String agentCode;
#SerializedName("identity_type")
#Expose
private Terminal identityType;
#SerializedName("business_type")
#Expose
private String businessType;
#SerializedName("super_agent_id")
#Expose
private Terminal superAgentId;
#SerializedName("status")
#Expose
private String status;
#SerializedName("wallet_no")
#Expose
private String walletNo;
#SerializedName("terminal_id")
#Expose
private Terminal terminalId;
#SerializedName("created_at")
#Expose
private String createdAt;
#SerializedName("updated_at")
#Expose
private String updatedAt;
#SerializedName("activation_code")
#Expose
private String activationCode;
#SerializedName("bank_code")
#Expose
private String bankCode;
#SerializedName("uuid")
#Expose
private String uuid;
#SerializedName("api_key")
#Expose
private String apiKey;
#SerializedName("commission_value")
#Expose
private Terminal commissionValue;
#SerializedName("webhook")
#Expose
private Terminal webhook;
#SerializedName("full_name")
#Expose
private String fullName;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getBusinessName() {
return businessName;
}
public void setBusinessName(String businessName) {
this.businessName = businessName;
}
public String getBusinessAddress() {
return businessAddress;
}
public void setBusinessAddress(String businessAddress) {
this.businessAddress = businessAddress;
}
public String getBusinessPhone() {
return businessPhone;
}
public void setBusinessPhone(String businessPhone) {
this.businessPhone = businessPhone;
}
public Integer getStateId() {
return stateId;
}
public void setStateId(Integer stateId) {
this.stateId = stateId;
}
public Integer getLocalGovernmentId() {
return localGovernmentId;
}
public void setLocalGovernmentId(Integer localGovernmentId) {
this.localGovernmentId = localGovernmentId;
}
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public Integer getBankId() {
return bankId;
}
public void setBankId(Integer bankId) {
this.bankId = bankId;
}
public String getBvn() {
return bvn;
}
public void setBvn(String bvn) {
this.bvn = bvn;
}
public String getAgentCode() {
return agentCode;
}
public void setAgentCode(String agentCode) {
this.agentCode = agentCode;
}
public Terminal getIdentityType() {
return identityType;
}
public void setIdentityType(Terminal identityType) {
this.identityType = identityType;
}
public String getBusinessType() {
return businessType;
}
public void setBusinessType(String businessType) {
this.businessType = businessType;
}
public Terminal getSuperAgentId() {
return superAgentId;
}
public void setSuperAgentId(Terminal superAgentId) {
this.superAgentId = superAgentId;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getWalletNo() {
return walletNo;
}
public void setWalletNo(String walletNo) {
this.walletNo = walletNo;
}
public Terminal getTerminalId() {
return terminalId;
}
public void setTerminalId(Terminal terminalId) {
this.terminalId = terminalId;
}
public String getCreatedAt() {
return createdAt;
}
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
public String getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(String updatedAt) {
this.updatedAt = updatedAt;
}
public String getActivationCode() {
return activationCode;
}
public void setActivationCode(String activationCode) {
this.activationCode = activationCode;
}
public String getBankCode() {
return bankCode;
}
public void setBankCode(String bankCode) {
this.bankCode = bankCode;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getApiKey() {
return apiKey;
}
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}
public Terminal getCommissionValue() {
return commissionValue;
}
public void setCommissionValue(Terminal commissionValue) {
this.commissionValue = commissionValue;
}
public Terminal getWebhook() {
return webhook;
}
public void setWebhook(Terminal webhook) {
this.webhook = webhook;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
}
from the response, identity type is actually a string, I really don't know where I am getting it wrong
"identity_type": "driver's license",
You have mentioned identity_type is an object but you are receiving a string
#SerializedName("identity_type")
#Expose
private Terminal identityType;
Change Terminal to String it would work.
Your identity_type is coming as String but you have declared as Terminal.
Change from
#SerializedName("identity_type")
#Expose
private Terminal identityType;
To
#SerializedName("identity_type")
#Expose
private String identityType;
Hope this will help you.
I am trying to get the value of a JSON response and display it in my textView and editText. But I get a null object reference as an error.
JSON Response:
{
"srNo": 1,
"date": "11/14/2019 12:00:00 AM",
"fieldEngineer": "Angel",
"accountName": "Forever 21 Megamall",
"irNo": 1,
"joNo": 1,
"address": "Mandaluyong City",
"contactPerson": "Jansen Babon",
"designation": "",
"contactNo": "",
"email": "",
"timeIn": "00:00:00",
"timeOut": "00:00:00",
"productType": "Security",
"problem": ""
}
Java class:
private void fetchData() {
JsonObject paramObject = new JsonObject();
Call<ResObj> call = userService.userLogin(paramObject);
call.enqueue(new Callback<ResObj>() {
#Override
public void onResponse(Call<ResObj> call, retrofit2.Response<ResObj> response) {
ResObj resObj = response.body();
String srNo = resObj.getSrNo();
String date = resObj.getDate();
String fieldEngineer = resObj.getFieldEngineer();
String accountName = resObj.getAccountName();
String irNo = resObj.getIrNo();
String joNo = resObj.getJoNo();
String address = resObj.getAddress();
String contactPerson = resObj.getContactPerson();
String designation = resObj.getDesignation();
String contactNo = resObj.getContactNo();
String email = resObj.getEmail();
String timeIn = resObj.getTimeIn();
String timeOut = resObj.getTimeOut();
String productType = resObj.getProductType();
String problem = resObj.getProblem();
//the response I am getting here is null
tvSrNo.setText(srNo);
etdate.setText(date);
etfieldengineer.setText(fieldEngineer);
etaccname.setText(accountName);
etirno.setText(irNo);
etjono.setText(joNo);
JsonObject workObj = new JsonObject();
try {
workObj.addProperty("srNo", resObj.getSrNo());
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void onFailure(Call<ResObj> call, Throwable t) {
}
});
}
I tried using this tvSrNo.setText(resObj.getSrNo()) instead of tvSrNo.setText(srNo) but it still gets the same problem.
I am also using Retrofit.
I expect the result that JSON data will be placed in an editText or textView. But apparently, the response is getting null.
ResObj class:
private String date;
private String address;
private String accountName;
private String contactPerson;
private String timeOut;
private String problem;
private String srNo;
private String fieldEngineer;
private String joNo;
private String irNo;
private String message;
private String designation;
private String email;
private String timeIn;
private String productType;
private boolean status;
private String contactNo;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public String getContactPerson() {
return contactPerson;
}
public void setContactPerson(String contactPerson) {
this.contactPerson = contactPerson;
}
public String getTimeOut() {
return timeOut;
}
public void setTimeOut(String timeOut) {
this.timeOut = timeOut;
}
public String getProblem() {
return problem;
}
public void setProblem(String problem) {
this.problem = problem;
}
public String getSrNo() {
return srNo;
}
public void setSrNo(String srNo) {
this.srNo = srNo;
}
public String getFieldEngineer() {
return fieldEngineer;
}
public void setFieldEngineer(String fieldEngineer) {
this.fieldEngineer = fieldEngineer;
}
public String getJoNo() {
return joNo;
}
public void setJoNo(String joNo) {
this.joNo = joNo;
}
public String getIrNo() {
return irNo;
}
public void setIrNo(String irNo) {
this.irNo = irNo;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTimeIn() {
return timeIn;
}
public void setTimeIn(String timeIn) {
this.timeIn = timeIn;
}
public String getProductType() {
return productType;
}
public void setProductType(String productType) {
this.productType = productType;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
public String getContactNo() {
return contactNo;
}
public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}
Logcat:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.android.ras.ResObj.getSrNo()' on a null object reference
at com.example.android.ras.MainActivity$3.onResponse(MainActivity.java:187)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:71)
at android.os.Handler.handleCallback(Handler.java:907)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7625)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
Change your String to Integer
public class Codebeautify {
private Integer srNo;
private String date;
private String fieldEngineer;
private String accountName;
private Integer irNo;
private Integer joNo;
private String address;
private String contactPerson;
private String designation;
private String contactNo;
private String email;
private String timeIn;
private String timeOut;
private String productType;
private String problem;
// Getter Methods
public Integer getSrNo() {
return srNo;
}
public String getDate() {
return date;
}
public String getFieldEngineer() {
return fieldEngineer;
}
public String getAccountName() {
return accountName;
}
public Integer getIrNo() {
return irNo;
}
public Integer getJoNo() {
return joNo;
}
public String getAddress() {
return address;
}
public String getContactPerson() {
return contactPerson;
}
public String getDesignation() {
return designation;
}
public String getContactNo() {
return contactNo;
}
public String getEmail() {
return email;
}
public String getTimeIn() {
return timeIn;
}
public String getTimeOut() {
return timeOut;
}
public String getProductType() {
return productType;
}
public String getProblem() {
return problem;
}
// Setter Methods
public void setSrNo(Integer srNo) {
this.srNo = srNo;
}
public void setDate(String date) {
this.date = date;
}
public void setFieldEngineer(String fieldEngineer) {
this.fieldEngineer = fieldEngineer;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public void setIrNo(Integer irNo) {
this.irNo = irNo;
}
public void setJoNo(Integer joNo) {
this.joNo = joNo;
}
public void setAddress(String address) {
this.address = address;
}
public void setContactPerson(String contactPerson) {
this.contactPerson = contactPerson;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}
public void setEmail(String email) {
this.email = email;
}
public void setTimeIn(String timeIn) {
this.timeIn = timeIn;
}
public void setTimeOut(String timeOut) {
this.timeOut = timeOut;
}
public void setProductType(String productType) {
this.productType = productType;
}
public void setProblem(String problem) {
this.problem = problem;
}
}
Make your POJO class like this
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
#SerializedName("srNo")
#Expose
private Integer srNo;
#SerializedName("date")
#Expose
private String date;
#SerializedName("fieldEngineer")
#Expose
private String fieldEngineer;
#SerializedName("accountName")
#Expose
private String accountName;
#SerializedName("irNo")
#Expose
private Integer irNo;
#SerializedName("joNo")
#Expose
private Integer joNo;
#SerializedName("address")
#Expose
private String address;
#SerializedName("contactPerson")
#Expose
private String contactPerson;
#SerializedName("designation")
#Expose
private String designation;
#SerializedName("contactNo")
#Expose
private String contactNo;
#SerializedName("email")
#Expose
private String email;
#SerializedName("timeIn")
#Expose
private String timeIn;
#SerializedName("timeOut")
#Expose
private String timeOut;
#SerializedName("productType")
#Expose
private String productType;
#SerializedName("problem")
#Expose
private String problem;
public Integer getSrNo() {
return srNo;
}
public void setSrNo(Integer srNo) {
this.srNo = srNo;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getFieldEngineer() {
return fieldEngineer;
}
public void setFieldEngineer(String fieldEngineer) {
this.fieldEngineer = fieldEngineer;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public Integer getIrNo() {
return irNo;
}
public void setIrNo(Integer irNo) {
this.irNo = irNo;
}
public Integer getJoNo() {
return joNo;
}
public void setJoNo(Integer joNo) {
this.joNo = joNo;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getContactPerson() {
return contactPerson;
}
public void setContactPerson(String contactPerson) {
this.contactPerson = contactPerson;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public String getContactNo() {
return contactNo;
}
public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTimeIn() {
return timeIn;
}
public void setTimeIn(String timeIn) {
this.timeIn = timeIn;
}
public String getTimeOut() {
return timeOut;
}
public void setTimeOut(String timeOut) {
this.timeOut = timeOut;
}
public String getProductType() {
return productType;
}
public void setProductType(String productType) {
this.productType = productType;
}
public String getProblem() {
return problem;
}
public void setProblem(String problem) {
this.problem = problem;
}
}
make sure to have Gson Converter in your retrofit instance
private static Retrofit getRetrofitInstance() {
return new Retrofit.Builder()
.baseUrl(ROOT_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
then make call and put the data in ArrayList
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.android.ras.ResObj.getSrNo()' on a null object reference
Note: NullPointerException because you did not declare SrNo inside the model class
Try to use a jason to java class generator:
http://www.jsonschema2pojo.org/
Source type: JSON
Annotation style: Gson( if you used GSON) or none
Include getters and setters
public class Example {
private Integer srNo;
private String date;
private String fieldEngineer;
private String accountName;
private Integer irNo;
private Integer joNo;
private String address;
private String contactPerson;
private String designation;
private String contactNo;
private String email;
private String timeIn;
private String timeOut;
private String productType;
private String problem;
public Integer getSrNo() {
return srNo;
}
public void setSrNo(Integer srNo) {
this.srNo = srNo;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getFieldEngineer() {
return fieldEngineer;
}
public void setFieldEngineer(String fieldEngineer) {
this.fieldEngineer = fieldEngineer;
}
public String getAccountName() {
return accountName;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public Integer getIrNo() {
return irNo;
}
public void setIrNo(Integer irNo) {
this.irNo = irNo;
}
public Integer getJoNo() {
return joNo;
}
public void setJoNo(Integer joNo) {
this.joNo = joNo;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getContactPerson() {
return contactPerson;
}
public void setContactPerson(String contactPerson) {
this.contactPerson = contactPerson;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public String getContactNo() {
return contactNo;
}
public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTimeIn() {
return timeIn;
}
public void setTimeIn(String timeIn) {
this.timeIn = timeIn;
}
public String getTimeOut() {
return timeOut;
}
public void setTimeOut(String timeOut) {
this.timeOut = timeOut;
}
public String getProductType() {
return productType;
}
public void setProductType(String productType) {
this.productType = productType;
}
public String getProblem() {
return problem;
}
public void setProblem(String problem) {
this.problem = problem;
}
}
first : Check response output,
you can Log.i or Toast it,,,
If your response not load or null ... - (the Problem in here)
Second : if respon Ok, Check your ResObj.getSrNo().
Print again... check
String srNo = resObj.getSrNo();
Log.i srNo... (problem or not)
Or checkyour Class Codebeautify
JsonObject paramObject = new JsonObject();
Call<ResObj> call = userService.userLogin(paramObject); // paramObject is empty object
You are passing empty JsonObject to your API parameter.
So you have to add parameter value to your paramObject. like this
try {
JsonObject paramObject = new JsonObject();
paramObject.addProperty("mobile", mobile);
// add other properties if you have
} catch (JSONException e) {
e.printStackTrace();
}
after that you should call your api like
Call<ResObj> call = userService.userLogin(paramObject);
As i am seeing the problem is in parsing, The retrofit can not mapping as your response is n't having ResObj as root.
{ "ResObj": { "srNo": 1, "date": "11/14/201912: 00: 00AM", "fieldEngineer": "Angel", "accountName": "Forever21Megamall", "irNo": 1, "joNo": 1, "address": "MandaluyongCity", "contactPerson": "JansenBabon", "designation": "", "contactNo": "", "email": "", "timeIn": "00: 00: 00", "timeOut": "00: 00: 00", "productType": "Security", "problem": "" } }
Modify your response or change your request
Call<JSONObject> call = userService.userLogin(paramObject);
Later in extract the values manually.
I have mapping with class admin and class role I want to show the data od admin class in Json format but I have error Http 500 because this is a mapping between class admin and role how can I do this?
this is my class Admin
#Entity
public class Admin implements Serializable {
#Id
#GeneratedValue
private int idAdmin;
private String email;
private String cin;
private String nom;
private String prenom;
private String loginAdmin;
private String adresse;
private Long tele;
private String motPasse;
private boolean actived;
#ManyToOne
#JoinColumn(name = "idRole")
private Role role;
public Admin() {
super();
}
public int getIdAdmin() {
return idAdmin;
}
public void setIdAdmin(int idAdmin) {
this.idAdmin = idAdmin;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getPrenom() {
return prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMotPasse() {
return motPasse;
}
public void setMotPasse(String motPasse) {
this.motPasse = motPasse;
}
public boolean isActived() {
return actived;
}
public void setActived(boolean actived) {
this.actived = actived;
}
public String getCin() {
return cin;
}
public void setCin(String cin) {
this.cin = cin;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public String getLoginAdmin() {
return loginAdmin;
}
public void setLoginAdmin(String loginAdmin) {
this.loginAdmin = loginAdmin;
}
public String getAdresse() {
return adresse;
}
public void setAdresse(String adresse) {
this.adresse = adresse;
}
public Long getTele() {
return tele;
}
public void setTele(Long tele) {
this.tele = tele;
}
}
And this is my role class
#Entity
public class Role implements Serializable {
#Id
#GeneratedValue
private Long idRole;
private String roleName;
#OneToMany(mappedBy = "role")
private List<Admin> admin;
public Role() {
super();
}
public Long getIdRole() {
return idRole;
}
public void setIdRole(Long idRole) {
this.idRole = idRole;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public void setAdmin(List<Admin> admin) {
this.admin = admin;
}
public List<Admin> getAdmin() {
return admin;
}
}
And this is my function in controller to show the data in json format
#RequestMapping(value =" /jsonPosts", method = RequestMethod.GET,produces =
"application/json")
#ResponseBody
public List<Admin> generateJSONPosts() {
return adminService.selectAll();
}
The problem is the mapping between role and admin
I have a model with this attribute
public class Audit extends BaseModel implements Comparable {
#Column
#PrimaryKey
#SerializedName("audit_id")
private int id;
#Column
#SerializedName("address_line_1")
private String addressLine1;
#Column
#SerializedName("address_line_2")
private String addressLine2;
#Column
#SerializedName("city_name")
private String city;
#Column
#SerializedName("zip_code")
private String zipcode;
#Column
#SerializedName("company_id")
private int companyId;
#Column
#SerializedName("company_name")
private String companyName;
#Column
#SerializedName("loc_name")
private String location;
#Column
#SerializedName("inspection_date_time")
private OffsetDateTime inspectionTime;
#Column
#SerializedName("inspection_number")
private String inspectionNumber;
#Column(defaultValue = "0")
private int auditStatus;
#Column(defaultValue = "0")
private int userId;
public int getId() {
return id;
}
public String getAddressLine1() {
return addressLine1;
}
public String getAddressLine2() {
return addressLine2;
}
public String getCity() {
return city;
}
public String getZipcode() {
return zipcode;
}
public int getCompanyId() {
return companyId;
}
public String getCompanyName() {
return companyName;
}
public String getLocation() {
return location;
}
public OffsetDateTime getInspectionTime() {
return inspectionTime;
}
public String getInspectionNumber() {
return inspectionNumber;
}
public void setId(int id) {
this.id = id;
}
public void setAddressLine1(String addressLine1) {
this.addressLine1 = addressLine1;
}
public void setAddressLine2(String addressLine2) {
this.addressLine2 = addressLine2;
}
public void setCity(String city) {
this.city = city;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public void setCompanyId(int companyId) {
this.companyId = companyId;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public void setLocation(String location) {
this.location = location;
}
public void setInspectionTime(OffsetDateTime inspectionTime) {
this.inspectionTime = inspectionTime;
}
public void setInspectionNumber(String inspectionNumber) {
this.inspectionNumber = inspectionNumber;
}
#Override
public int compareTo(#NonNull Object audits) {
int result = -1;
if (audits instanceof Audit) {
Audit audit = (Audit) audits;
if (this.getInspectionTime().isEqual(audit.getInspectionTime())) {
result = 0;
} else if
(this.getInspectionTime().isAfter(audit.getInspectionTime())) {
result = 1;
}
}
return result;
}
public int getAuditStatus() {
return auditStatus;
}
public void setAuditStatus(int auditStatus) {
this.auditStatus = auditStatus;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
In here I get all values to Arraylist using this model
List<Audit> auditList
My list is "auditList", It contains Audit objects, In this auditList I want to chnge one attribute
for ex: I want to change userId value to "3" in all objects, How can I do it once, Is there any solution please Help Me
Using Java 8 Stream :
auditList.stream().forEach(elt -> elt.setUserId(3));
It corresponds to :
for (Audit aud : auditList) {
aud.setUserId(3);
}
You will need to iterate through all elements and update
for (Audit aud : auditList) {
aud.setUserId(3);
}
If you want it on one line then
for (Audit aud : auditList) aud.setUserId(3);
I need to map two pojo class using one to many, but getting the below error
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'testCaseStepsform' at row 1
When I saw the table created by hibernate, I found that a column with data type blob is created
I am adding the code below.
#Entity
#Table(name="TEST_CASE_DESC")
public class TestCaseForm implements Serializable{
/**
*
*/
private static final long serialVersionUID = 10001234L;
#Id
#Column(name="TEST_CASE_ID")
private String testCaseId;
#Column(name="PROJECT_NAME")
private String projectName;
#Column(name="PROJECT_ID")
private String projectId;
#Column(name="RELEASE_NAME")
private String releaseName;
#Column(name="RELEASE_ID")
private String releaseId;
#Column(name="ITERATION")
private String iteration;
#Column(name="TITLE")
private String title;
#Column(name="CREATED_BY")
private String createdBy;
#Column(name="CREATION_DATE")
private Date creationDate;
#Column(name="DESCRIPTION")
private String description;
#Column(name="PRE_CONDITION")
private String preCondition;
#Column(name="POST_CONDITION")
private String postCondition;
#Column(name="TYPE")
private String type;
#Column(name="IMPORTANCE")
private String importance;
private ArrayList<TestCaseStepsForm> testCaseStepsform = new ArrayList<TestCaseStepsForm>();
public String getProjectId() {
return projectId;
}
public void setProjectId(String projectId) {
this.projectId = projectId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCreatedBy() {
return createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPreCondition() {
return preCondition;
}
public void setPreCondition(String preCondition) {
this.preCondition = preCondition;
}
public String getPostCondition() {
return postCondition;
}
public void setPostCondition(String postCondition) {
this.postCondition = postCondition;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public String getRelease() {
return releaseName;
}
public void setReleaseName(String releaseName) {
this.releaseName = releaseName;
}
public String getReleaseId() {
return releaseId;
}
public void setReleaseId(String releaseId) {
this.releaseId = releaseId;
}
public String getIteration() {
return iteration;
}
public void setIteration(String iteration) {
this.iteration = iteration;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getReleaseName() {
return releaseName;
}
public String getImportance() {
return importance;
}
public void setImportance(String importance) {
this.importance = importance;
}
#OneToMany(mappedBy = "TEST_CASE_DESC", cascade = CascadeType.ALL)
public ArrayList<TestCaseStepsForm> getTestCaseStepsform() {
return testCaseStepsform;
}
public void setTestCaseStepsform(ArrayList<TestCaseStepsForm> testCaseStepsform) {
this.testCaseStepsform = testCaseStepsform;
}
public String getTestCaseId() {
return testCaseId;
}
public void setTestCaseId(String testCaseId) {
this.testCaseId = testCaseId;
}
}
#Entity
#Table(name="TEST_CASE_STEP")
public class TestCaseStepsForm implements Serializable{
/**
*
*/
private static final long serialVersionUID = 123456788091L;
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
#Column(name="SERIAL_NUMBER")
private String serialNumber;
#Column(name="TEST_CASE_ID")
private String testCaseId;
#Column(name="INPUT")
private String input;
#Column(name="EXPCETED_OUTPUT")
private String expectedOutput;
#Column(name="STATUS")
private String status;
private TestCaseForm testCaseForm;
public String getTestCaseId() {
return testCaseId;
}
public void setTestCaseId(String testCaseId) {
this.testCaseId = testCaseId;
}
public String getInput() {
return input;
}
public void setInput(String input) {
this.input = input;
}
public String getExpectedOutput() {
return expectedOutput;
}
public void setExpectedOutput(String expectedOutput) {
this.expectedOutput = expectedOutput;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
#ManyToOne( fetch = FetchType.LAZY)
#JoinColumn(name = "TEST_CASE_ID", nullable = false)
public TestCaseForm getTestCaseForm() {
return testCaseForm;
}
public void setTestCaseForm(TestCaseForm testCaseForm) {
this.testCaseForm = testCaseForm;
}
public String getSerialNumber() {
return serialNumber;
}
public void setSerialNumber(String serialNumber) {
this.serialNumber = serialNumber;
}
}
#Configuration
public class TestCaseConfig {
#Bean(name="testCaseForm1")
public TestCaseForm testCaseForm1(){
TestCaseForm tst = new TestCaseForm();
tst.setTestCaseId("1122233");
tst.setProjectId("1234");
tst.setReleaseName("June");
tst.setReleaseId("1707");
tst.setIteration("2");
tst.setProjectName("ExpressPay");
tst.setTitle("ExpressPay");
tst.setCreatedBy("Anirban Deb");
tst.setCreationDate(new Date());
tst.setDescription("ExpressPay Login");
tst.setPreCondition("Active account");
tst.setPostCondition("success");
TestCaseStepsForm str1 = new TestCaseStepsForm();
str1.setTestCaseId("1122233");
str1.setInput("Hello World");
str1.setExpectedOutput("Bye Bye world");
str1.setStatus("Run");
str1.setTestCaseForm(tst);
TestCaseStepsForm str2 = new TestCaseStepsForm();
str2.setTestCaseId("1122233");
str2.setInput("Hello World");
str2.setExpectedOutput("Bye Bye world");
str2.setStatus("Run");
str1.setTestCaseForm(tst);
tst.getTestCaseStepsform().add(str1);
tst.getTestCaseStepsform().add(str2);
return tst;
}
}
public class Main {
public static void main(String[] args) throws MessagingException {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("core-bean.xml");
TestCaseForm test1 = context.getBean("testCaseForm1",TestCaseForm.class);
ITestCaseService testCase = context.getBean("testCaseServiceImp", ITestCaseService.class);
testCase.inserTestCase(test1);
[enter image description here][1]
stopWatch.stop();
System.out.println("Time taken in execution : "+stopWatch.getTotalTimeSeconds());
}
}
You should choose to annotate object properties or getters. Don't use it simultaneously.
Apart, #xl0e answer
You need to correct mapping
#OneToMany(mappedBy = "testCaseForm", cascade = CascadeType.ALL)
public ArrayList<TestCaseStepsForm> getTestCaseStepsform() {
return testCaseStepsform;
}