I am starting HTML5-client-server application using Rest-services and JPA written java. I've got a problem how to implement server side. Imagine situation that you have the object
public class User {
private String firstName = "";
private String surName = "";
private Address address = "";
}
public class Address {
private long id = 0L;
private String sAddress ="";
}
That is final entity used both rest service and controller when creating/deleting/updating db.
Now I try to insert a new user from ui (using jquery and ajax) to DB and I've got populated JSON-object. But there is that Address-object inside the User-object. Is there any way to implement/use that or should I create new pojos where only is basic types and then a new layer where I populate the final JPA-entities like User with Address-object? I hope that my English is good enough to explain the problem.
- Is this a good idea to create an application where data (JPA-entities) are exactly similar than objects in web service-layer and Client-layer?
- Any idea how to implement that?
- Is it even possible to use own object-types in rest services - HTML5 (JSON) combination or even generally?
You can easily create that kind of problem/app with Netbeans 7.3. First create an DB with two tables, create a project and generate web services from DB and that's it. Now you have got entities, web services with facade, db and project.
I have asked this with sample code before but no answers: https://stackoverflow.com/questions/14936031/mapping-and-parsing-json-objects-in-ui-and-in-java
and gor an ERROR like this:
WARNING: StandardWrapperValve[ServletAdaptor]: PWC1406: Servlet.service() for servlet ServletAdaptor threw exception
org.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [simple type, class entity.Address] from JSON String; no single-String constructor/factory method (through reference chain: entity.User["addressAddressId"])
at org.codehaus.jackson.map.deser.std.StdValueInstantiator._createFromStringFallbacks(StdValueInstantiator.java:379)
at org.codehaus.jackson.map.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:268)