Build object where one field depends on the other - amazon-dynamodb

I want to build an object where one of my class field depends on the other. Somewhat like
import lombok.Builder;
import lombok.Data;
#Data
#Builder
public class MyModel {
#DynamoDBHashkey
private String key = encrypt(value1, value2);
#DynamoDBAttribute
private String value1;
#DynamoDBAttribute
private String value2;
}
MyModel model = MyModel.builder()
.value1(1002020)
.value2(1384818)
.build();
Now when I do model.key() I want to be able to retrieve this value.

Probably I'm missing something but you could just add getter. According to documentation Annotation Type DynamoDBHashKey
Annotation for marking a property as the hash key for a modeled class. Applied to the getter method or the class field for a hash key property.
#Data
#Builder
public class MyModel {
#DynamoDBAttribute
private String value1;
#DynamoDBAttribute
private String value2;
#DynamoDBHashkey
public String getKey() {
return encrypt(value1, value2);
}
}

Related

how to get the values from properties file in the test cases

I am getting null while reading the values from .properties file when i am executing the test case. here while debugging the test case i am able to see the values which are loaded from properties file when the curser is there in the test class but when the curser enters into the actual class in that class i am getting the same values as null. And my code is as follows
Thanks in advance
#RestController
#PropertySource("classpath:/com/example/prop.properties")
public class ReadProp {
#Value("${name}")
private String name;
#Value("${rollNo}")
private String rollNo;
#RequestMapping(value="/")
public void getDetails(){
System.out.println(name);
System.out.println(rollNo);
}
}
and the test case is as follows
#RunWith(SpringRunner.class)
#SpringBootTest
#PropertySource("classpath:/com/example/prop.properties")
public class ReadPropTest {
private ReadProp readProp = new ReadProp();
#Value("${name}")
private String name;
#Value("${rollNo}")
private String rollNo;
#Test
public void readValues() {
System.out.println(name);
System.out.println(rollNo);
readProp.getDetails();
}
}
Instead of creating a new object using new ReadProp(). You should Autowire it.
#Autowired
ReadProp readProp;
in your test class. If you create an object using new, you don't get the bean which spring created with all the value assigned using #Value.
Try something like this :
#PropertySource("classpath:prop.properties")// your error
public class ReadPropTest {
#Value("${name}")
private String name;
#Value("${rollNo}")
private String rollNo;
}

List<SomeObject> mList is not supported in Realm

I am working with Realm ORM for my application. My Application has three Model classes that extends RealmObject. In one of the class I have defined a List of object which is creating problem.
my first class;
public class Party extends RealmObject implements Parcelable{
public String name;
public String name_en;
public String name_ne;
public String address;
public String phoneNumber;
//get and setters
//parceable
}
My second class;
public class CreatePurchaseOrderRow extends RealmObject implements Parcelable {
public String name;
public float amount;
public String specification;
public String remarks;
public Party party;
// getter setter
//parceable
}
And finally My third class implements List of objects of Second class. that is,
public class CreatePurchasOrder extends RealmObject implements Parcelable {
public int num;
public Date date;
public List<CreatePurchaseOrderRow> createPurchaseOrderRows;
//getter setter
//parceable
}
the List feeds is creating problem.
i have the screenshot for error message here
Realm is not schemaless database. I tried finding the solutions but i cannot. Can anyone help me with this?
Thanks in Advance
RealmObjects can not have fields of type List<>(java.util.List), you have to use RealmList<> instead:
public class CreatePurchasOrder extends RealmObject implements Parcelable {
public int num;
public Date date;
public RealmList<CreatePurchaseOrderRow> createPurchaseOrderRows;
//getter setter
//parceable
}
See also: https://realm.io/docs/java/latest/#many-to-many

How to pass parameter to controller to get the correct object in spring mvc

I define an object like
public class DrivelogBean implements Serializable{
private String backInfoIdentify;
private DriVehNum driVehNum;
public static class TotalMileageIntd implements Serializable{
private static final long serialVersionUID = -3268743972404969523L;
private String totalMileage;
private String mileageTime;
public String getTotalMileage () {
return totalMileage;
}
public void setTotalMileage (String totalMileage) {
this.totalMileage = totalMileage;
}
public String getMileageTime () {
return mileageTime;
}
public void setMileageTime (String mileageTime) {
this.mileageTime = mileageTime;
}
}
}
and my controller is like:
#RequestMapping(value="saveDriveLog",method = RequestMethod.GET)
public #ResponseBody ResultBean saveDriveLog(DrivelogBean drivelogBean){
driveLogService.addDriveLog (drivelogBean);
ResultBean resultBean = new ResultBean();
resultBean.setRet (1);
resultBean.setDescripion (UsConstants.DRIVELOG_SAVE);
return resultBean;
}
I want request parameters convert to drivelogBean
and my url is like that:
http://127.0.0.1:8080//manage/drivelog/saveDriveLog/?backInfoIdentify=2&totalMileageIntd["driverNum%22]=1&totalMileageIntd["driveCode"]=2
but the page prompt
HTTP ERROR: 404 Problem accessing //manage/drivelog/saveDriveLog/error. Reason:Not Found
and i change the url like :
http://127.0.0.1:8080//manage/drivelog/saveDriveLog/?commendWord=2&totalMileageIntd.driverNum=1&totalMileageIntd.driveCode=2
but the drivelogBean parameter, the property driverNum of totalMileageIntd and the property driveCode of totalMileageIntd is null.
So how can I set the correct url pass parameter to the drivelogBean?
I don't know if binding inner static class works in spring.
Your parametter must match a setter
commendWord=2 => you must have setCommendWord on the class DrivelogBean
totalMileageIntd.driverNum=1 => you must have a setDriverNum() on and a getTotalMileageIntd
The class should look like this (I skipped getter and setter to save space but they must exists)
public class DrivelogBean implements Serializable{
private String backInfoIdentify;
private DriVehNum driVehNum;
private TotalMileageIntd totalMileageIntd ;
public static class TotalMileageIntd implements Serializable{
private static final long serialVersionUID = -3268743972404969523L;
private String totalMileage;
private String mileageTime;
}
}
In this case all the parametter you can use are :
backInfoIdentify=XXX
driVehNum=XXX
totalMileageIntd.totalMileage=XXX
totalMileageIntd.mileageTime=XXX
nothing else

Curiosities in deserializing collections with gson 2

I have these classes
public class Application {
public String name;
public String ico;
public List<MenuStruct> menu =new ArrayList<MenuStruct>();
//Constructor
public Application() { }
}
public class MenuStruct {
public String id;
public String type;
public String parent;
public String name;
public String secId;
//Constructor
public MenuStruct() {}
}
If I try to deserialize a collection directly in this way:
ApplicationManager apm= new ApplicationManager();
s="[ {\"name\":\"reg_salida\" , \"ico\":\"document-open-2-32x32.ico\" }]";
apm.apps=(new Gson()).fromJson(s,apm.apps.getClass() );
for (Application ap:apm.apps){
System.out.println(ap.name); //gets error here
}
I get a java.lang.ClassCastException.
But if I try to deserialize its containig class ApplicationManager it does not fail.
s="{ \"apps\": [ {\"name\":\"reg_salida\" , \"ico\":\"document-open-2-32x32.ico\" }]}";
ApplicationManager apm=(new Gson()).fromJson(s,ApplicationManager.class);
for (Application ap:apm.apps){
System.out.println(ap.name); // now no errors here! and shows reg_salida
}
Is this a bug of gson 2.2.4? or maybe I am doing something not correct?
Eduard.
You have to provide full definition of property class. Your example should looks like that:
manager.apps = gson.fromJson(json, new TypeToken<List<Application>>() {}.getType());

Why Jackson Annotation Is Being Ignored For Class Inside List?

I'm using Jackson in Spring MVC #ResponseBody to generate JSON for jqGrid. I'm using a POJO like this for JSON required by jqGrid:
public class Grid<T> implements Serializable {
private int totalPages;
private int currentPage;
private long totalRecords;
private List<T> listData;
// getter & setter...
}
I'm putting domain model retrieved from Hibernate JPA (maybe a proxy because there is lazy fetching in some attributes) such as:
#Entity #Cacheable
public class Item implements Serializable {
#Id #GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
#Version
private int version;
#NotBlank
private String name;
#JsonIgnore #Lob #Basic(fetch=FetchType.LAZY)
private byte[] image;
// getter and setter...
}
This is my code in Spring MVC controller:
#RequestMapping(value="listgrid", method=RequestMethod.GET, produces="application/json")
#ResponseBody
public Grid<T> listGrid(#RequestParam(value="page", required=false) Integer page,
#RequestParam(value="rows", required=false) Integer rows,
#RequestParam(value="sidx", required=false) String sidx,
#RequestParam(value="sord", required=false) String sord,
#RequestParam(value="_search", required=false) String search) {
// ...
Grid<T> grid = new Grid<T>();
Page<T> objPage = retrieveData(...);
grid.setListData(objPage.getContent());
grid.setCurrentPage(objPage.getNumber()+1);
grid.setTOtalPages(objPage.getTotalPages());
grid.setTotalRecords(objPage.getTotalElements());
return grid;
}
I've put #JsonIgnore in image attribute, but the resulting JSON will always contains image. How to ignore this attribute?
Have you tried #JsonIgnore on the getter ?

Resources