public class Client {
private String idNum;
private int driverLicence;
private String name;
private String surname;
private String mailAddress;
private String address;
private int phoneNum;
public Client(String idNum, int driverLicence, String name, String surname, String mailAddress, String address, int phoneNum) {
this.address=address;
this.driverLicence=driverLicence;
this.idNum=idNum;
this.mailAddress=mailAddress;
this.name=name;
this.phoneNum=phoneNum;
this.surname=surname;
}
public String getIdNum() {
return idNum;
}
public void setIdNum(String idNum) {
this.idNum = idNum;
}
public int getDriverLicence() {
return driverLicence;
}
public void setDriverLicence(int driverLicence) {
this.driverLicence = driverLicence;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getMailAddress() {
return mailAddress;
}
public void setMailAddress(String mailAddress) {
this.mailAddress = mailAddress;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
this.address = address;
}
public int getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(int phoneNum) {
this.phoneNum = phoneNum;
}
}
THE VALUE OF THE FIELD Client.idNum IS NOT USED
for some reason i am getting this kind of error on every field i have written on this class
ALL getters and setters are generated from eclipse
and all my other classes are fine but for some reason this specific class gives this "error"
i have wasted a lot of time on this and can't seem to find the reason why this happends
any ideas?
I copy pasted my code in, and an issue that may be causing your problem is that the code below returns the incorrect instance variable. Your instance variable is "address" not "Address".
public String getAddress() {
return Address;
}
I have to map this JSONObject into a Java object.
This is my Json:
{"WALLET":{
"ID":"1234",
"BAL":"20.000",
"NAME":"Filomena",
"EMAIL":"filo#gmail.com",
"DOCS":[
{
"ID":"001",
"S":"0",
"TYPE":"CardId",
"VD":"2019"
}
],
"IBANS":[
{
"ID":"001",
"S":"1",
"DATA":"iban",
"SWIFT":"swiftCode",
"HOLDER":"holder"
}
],
"STATUS":"string",
"BLOCKED":"1",
"SDDMANDATES":[
{
"ID":"sddMandateId",
"S":"status",
"DATA":"iban",
"SWIFT":"swiftCode"
}
],
"LWID":"string",
"CARDS":[
{
"ID":"string",
"EXTRA":{
"IS3DS":"string",
"CTRY":"string",
"AUTH":"string",
"NUM":"string",
"EXP":"string",
"TYP":"string"
}
}
],
"FirstName":"string",
"LastName":"string",
"CompanyName":"string",
"CompanyDescription":"string",
"CompanyWebsite":"string"
}
}
This is my Java class:
public class Wallet {
private String id;
private String bal;
private String name;
private String email;
private List<Doc> docs;
private List<Iban> ibans;
private String status;
private String blocked;
private List<SddMandate> sddMandates ;
private String lwid;
private List<Card> cards;
private String firstName;
private String lastname;
private String companyName;
private String companyDescription;
private String companyWebSite;
public Wallet(){
}
public Wallet(String id, String bal, String name, String email, List<Doc> docs, List<Iban> ibans, String status,
String blocked, List<SddMandate> sddMandates, String lwid, List<Card> cards, String firstName,
String lastname, String companyName, String companyDescription, String companyWebSite) {
super();
this.id = id;
this.bal = bal;
this.name = name;
this.email = email;
this.docs = docs;
this.ibans = ibans;
this.status = status;
this.blocked = blocked;
this.sddMandates = sddMandates;
this.lwid = lwid;
this.cards = cards;
this.firstName = firstName;
this.lastname = lastname;
this.companyName = companyName;
this.companyDescription = companyDescription;
this.companyWebSite = companyWebSite;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBal() {
return bal;
}
public void setBal(String bal) {
this.bal = bal;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public List<Doc> getDocs() {
return docs;
}
public void setDocs(List<Doc> docs) {
this.docs = docs;
}
public List<Iban> getIbans() {
return ibans;
}
public void setIbans(List<Iban> ibans) {
this.ibans = ibans;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getBlocked() {
return blocked;
}
public void setBlocked(String blocked) {
this.blocked = blocked;
}
public List<SddMandate> getSddMandates() {
return sddMandates;
}
public void setSddMandates(List<SddMandate> sddMandates) {
this.sddMandates = sddMandates;
}
public String getLwid() {
return lwid;
}
public void setLwid(String lwid) {
this.lwid = lwid;
}
public List<Card> getCards() {
return cards;
}
public void setCards(List<Card> cards) {
this.cards = cards;
}
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 getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public String getCompanyDescription() {
return companyDescription;
}
public void setCompanyDescription(String companyDescription) {
this.companyDescription = companyDescription;
}
public String getCompanyWebSite() {
return companyWebSite;
}
public void setCompanyWebSite(String companyWebSite) {
this.companyWebSite = companyWebSite;
}
Now i'm trying to map the object with gson library.
Wallet walletDetails=gson.fromJson(rispostaGetWalletDetails.toString(), Wallet.class);
System.out.println("Balance: "+walletDetails.getBal());
Now when i try to call method on the object i have always null and not the real value.
How i can do?
You have a wrong root level.
Probably, you need to need to get one level down
JSONObject yourObject = json.get("WALLET");
Wallet walletDetails = gson.fromJson(yourObject.toString(), Wallet.class);
To have Gson handle the correct field name mapping while deserializing, you have to register a FieldNamingStrategy like this (using Java 8):
Gson gson = new GsonBuilder()
.setFieldNamingStrategy(field -> field.getName().toUpperCase())
.create();
The strategy will convert each Java field name to match those in your JSON.
This will cover almost all your fields except for those upper-camel-cased in the JSON response, such as "LastName", "CompanyName", etc. In order to map those too, your FieldNamingStrategy will have to become a little bit smarter, like:
field -> {
String fname = field.getName();
return "firstName".equals(fname) || "companyName".equals(fname) /*etc...*/ ? capitalize(fname) : fname.toUpperCase();
}
and so on, I think you got the idea.
The capitalize() method you can find in libraries like Apache Commons Lang or write your own, it's just for examplification here.
Your object variable name doesn't match the json attribute name. "EMAIL" in json should have same EMAIL in object. To overcome this, you could mention #JsonProperty before your attribute declaraction.
for eg:
#JsonProperty("EMAIL")
private String email;
In JSP in drop down box I have a list of countries. Selected country I should assign to "Organization" object as an object "Country".
JSP:
<select name="country">
<c:forEach var="country" items="${countries}" >
<option value="${country}">
<c:out value="${country.name}" />
</option>
</c:forEach>
</select>
Model, Organisation:
#Entity
public class Organization {
#Id
#GeneratedValue
private Integer id;
private String name;
//I should assign "Country" object to this field
#OneToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
#JoinColumn(name="country")
private Country country;
private String address;
private String phone;
private Long market_cap;
public Organization(Integer id, String name, Country country, String address, String phone, Long market_cap) {
this.id = id;
this.name = name;
this.country = country;
this.address = address;
this.phone = phone;
this.market_cap = market_cap;
}
public Organization() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Long getMarket_cap() {
return market_cap;
}
public void setMarketCap(Long market_cap) {
this.market_cap = market_cap;
}
}
Model, Country:
#Entity
public class Country {
#Id
#GeneratedValue
private Integer id_country;
private String name;
private String isocode;
public Country() {
}
public Country(Integer id_country, String name, String isocode) {
this.id_country = id_country;
this.name = name;
this.isocode = isocode;
}
public Integer getId_country() {
return id_country;
}
public void setId_country(Integer id_country) {
this.id_country = id_country;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIsocode() {
return isocode;
}
public void setIsocode(String isocode) {
this.isocode = isocode;
}
}
Controller method:
#RequestMapping(value="add", method=RequestMethod.GET)
public ModelAndView addOrganization() {
ModelAndView modelAndView = new ModelAndView("add");
Organization organization = new Organization();
modelAndView.addObject("organization", organization);
List<Country> countries = countryService.listOfCountries();
modelAndView.addObject("countries", countries);
return modelAndView;
}
I have Bad Request error.
I was trying a simple hibernate populate the db example. There are two POJO's Employee and Address. When I tried to use both the Employee and Address constructor's with parameters to create two instances an error could not get constructor for org.hibernate.persister.entity.singletableentitypersisterwas thrown but the property accessor methods worked fine. Why did I get the error ?
Ok since I do not have the stack trace right now I shall rephrase my question are property accessor methods preferred over constructors in hibernate?
Employee POJO:
package many2one;
public class Employee {
public int id;
public String firstName;
public String lastName;
public int salary;
public Address address;
public Employee(){}
public Employee(String firstName,String lastName,int salary,Address address){
this.firstName = firstName;
this.lastName = lastName;
this.salary = salary;
this.address = address;
}
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
public String getFirstName(){
return firstName;
}
public void setFirstName(String fname){
this.firstName = fname;
}
public String getLastName(){
return lastName;
}
public void setLastName(String lname){
this.lastName = lname;
}
public int getSalary(){
return salary;
}
public void setSalary(int salary){
this.salary = salary;
}
public Address getAddress(){
return address;
}
public void setAddress(Address address){
this.address = address;
}
#Override
public String toString(){
return id+","+firstName+","+lastName+","+salary+","+address.getStreetName()+","+address.getCityName()+","+address.getStateName()+","+address.getZipcode();
}
}
Address POJO:
package many2one;
public class Address {
public int id;
public String streetName;
public String cityName;
public String stateName;
public String zipcode;
public Employee employee;
public Address(){
}
public Address(String sname,String cname,String statename,String zipcode){
this.streetName = sname;
this.cityName = cname;
this.stateName = statename;
this.zipcode = zipcode;
}
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
public String getStreetName(){
return streetName;
}
public void setStreetName(String streetname){
this.streetName = streetname;
}
public String getCityName(){
return cityName;
}
public void setCityName(String cname){
this.cityName = cname;
}
public String getStateName(){
return stateName;
}
public void setStateName(String statename){
this.stateName = statename;
}
public String getZipcode(){
return zipcode;
}
public void setZipcode(String zipcode){
this.zipcode = zipcode;
}
public Employee getEmployee(){
return employee;
}
public void setEmployee(Employee employee){
this.employee = employee;
}
}
`
Your class should have a default public constructor that does not take any arguments. That's the only constraint with respect to constructors when using Hibernate.
As for the exception, you are probably missing a setter for one of your fields or the setters don't follow the convention expected by Hibernate. But this can only be confirmed if you provide a full stack trace.
are property accessor methods preferred over constructors in
hibernate?
What do you mean by preffered? If you mean are property methods optional, then the answer is no. (Whcih could be one of the reasons for the exception in the first place)
while getting values in the form the bindFromRequest().get() it returns only null value.I got all the String type is null and integr as zer0. Here is my code for controller and model packages and how I can resolve this error:
enter code here
In controller:
public static Result getShow(){
Register register=Form.form(Register.class).bindFromRequest().get();
register.save();
System.out.println(register);
return ok("#Required annotation kicked in.."+register);
}
In Models:
package models;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
#Entity
#Table(name="register")
public class Register {
//private static final long serialVersionUID = 1L;
private String firstname;
private String lastname;
#Id
private String displayname;
private String date;
private String email;
private String password;
private String confirm_password;
private String gender;
private int phone_no;
private String address;
private int zipcode;
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 getDisplayname() {
return displayname;
}
public void setDisplayname(String displayname) {
this.displayname = displayname;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getConfirm_password() {
return confirm_password;
}
public void setConfirm_password(String confirm_password) {
this.confirm_password = confirm_password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getPhone_no() {
return phone_no;
}
public void setPhone__no(int phone_no) {
this.phone_no = phone_no;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getZipcode() {
return zipcode;
}
public void setZipcode(int zipcode) {
this.zipcode = zipcode;
}
}
If bindFromRequest().get() returns null, then the Form didn't validate. To debug this, log Form.form(Register.class).bindFromRequest().errors(), to see the validation errors in the Form. Beyond that no one can tell you what's wrong without seeing the Register class, and the data you're trying to bind to it.
You shouldn't be blindly calling get() on the Form and trying to save it, as this obviously can fail. At least check that it hasErrors() before trying to save it. And if it does have validation errors, you should be passing that Form back to the view to show those errors to the user.
See Handling Binding Failure in the docs.