0

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.

  1. 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?
  2. Any idea how to implement that?
  3. 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)
Community
  • 1
  • 1
Sami
  • 2,311
  • 13
  • 46
  • 80

3 Answers3

0

Are you asking how to create a User JPA object (corresponding to a USER table), containing an Address object (corresponding to an ADDRESS table)?

If so, here are some answers:

  1. It is perfectly valid to do this, but some developers and frameworks advocate a layer of abstraction between the web service layer and the client layer. Some advocate using these entities directly (for example, Rails typically uses this model). Others advocate creating specific data transfer objects for this usage (for example, generating server stubs from an existing WSDL file). The advantage of the former is less duplication; the advantage of the latter is a layer of abstraction, so that if you change your domain, you don't have to change your web service layer.
  2. Use the @OneToOne annotation, at least from the User to the Address, and possibly the other direction as well. The page linked above even uses your example of an employee with an address. :)

Hope that helps!

Eric Galluzzo
  • 3,191
  • 1
  • 20
  • 20
  • No, it is easy in Netbeans, because it generates JPA-entities automatically when creates Web services from DB. I am just asking how to solve problem what causes when I've got nested objects like User-entity--->Address-entity-->address(String) – Sami Feb 23 '13 at 20:42
0

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?

In RESTful WebServices you call HTTP-methods on resources. Usually a HTTP-GET gets a resource defined by an URI. So that request has to be processed, the data retrieved and put back in form of a response. If the request is a pure retrieval of data, the data on the server and on the client should be similar.

Any idea how to implement that?

You could use a DAO-object here, that access your persistance layer.

Is it even possible to use own object-types in rest services - HTML5 (JSON) combination or even generally?

REST has literally nothing to do with HTML, it is a paradigm of how a service works. HTML is a language you could use to interact with a client like a browser.

sschrass
  • 7,014
  • 6
  • 43
  • 62
  • Thanks a lot for answering! So there is no possibility to use Address-object in client like return JSON.stringify({ "user.addressAddressId.address": $('#address').val();}); You said: "You could use a DAO-object here, that access your persistance layer." Is this really better solution than change DB and JPA-entities similar to UI? One new layer, a lot of code to implement and run time objects to eat time and memory? – Sami Feb 23 '13 at 22:21
  • using REST you are returning a response, that of course could contain a String, an integer, an XMLfile or an JSON-object. – sschrass Feb 23 '13 at 22:25
  • I think you don't get the idea behind REST fully. I recommend reading this first: https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm and then decide if you want to build a RESTful WebService. And if you apt for REST, have a brief look here: http://www.vogella.com/articles/REST/article.html. – sschrass Feb 23 '13 at 22:31
  • When adding/posting/inserting/adding a new User-object what includes a new Address-object, is that possible or not when using JSON and Rest-services if your rest service is like that: POST AT Consumes({"application/xml", "application/json"}) public void create(User entity) { super.create(entity); //Facade-patterns here for all types.. } I just want to know is it even possible or not without new mapping/parsing/copying data from one object to another layer :) Yes or No? Thanks for the links, I'll read those. – Sami Feb 23 '13 at 22:48
  • How @SatelliteSD ? Could you pls answer to that question: http://stackoverflow.com/questions/14936031/mapping-and-parsing-json-objects-in-ui-and-in-java Getting data over rest is working fine, posting data without own object type like Address is working fine, but if I've got own types inside the entity, it is not working. – Sami Feb 23 '13 at 23:24
  • On client side just let the user fill in his stuff, without id (since he can't know it), http-post it. On server side add a constructor that takes just the address string and checks the db for this entry. You have to put all neccessary informations inside the request first! And information the client couldn't have, the server must provide for himself. Keep in mind the question how and why the client should have an id of a db entry. – sschrass Feb 23 '13 at 23:49
  • public UserFacadeREST() { super(User.class); } I have no idea how to get an address-field from JSON-object in web service, could you help me a little bit? – Sami Feb 24 '13 at 23:31
  • Now I know that my architecture is possible and should be working. The problem is my newbie knowledge in javascript, jquery and Json-stuff. http://stackoverflow.com/questions/15058982/how-to-use-json-stringify-when-fields-are-own-object-types – Sami Feb 25 '13 at 10:23
0

according to exception you got , all you missing is proper constructor for your Address object. You need Address(String x) constructor.

Igor Cunko
  • 59
  • 2