nekome
nekome

Reputation: 23

gwt hibernate one-to-many data fetching

I realize that using GWT and Hibernate I have to use DTO (Data Transfer Objects) objects which are almost as same as Hibernate classes (lets assume I'm not using Gilead or Dozer or some other lib).

So if I, for example, have following entities:

Company (id, name, city_id), 
Bank (id, name), 
BankAccount(id, account, bank_id, company_id), 
City(id, name)

so city can have multiple companies, and both bank(needs to) and company (can) contain multiple bank accounts. Something like:

Bank -< BankAccount >- Company >- City

These are attributes my hibernate and DTO classes have

class Company{
    int id;
    String name;
    City city;
    Set<BankAccount> accounts;
    //with appropriate getters and setters
}
class Bank{
    int id;
    String name;
    Set<BankAccount> accounts;
    //with appropriate getters and setters
}
class BankAccount{
    int id;
    String account;
    Bank bank;
    Company company;
    //and all get set
}
class City{
    int id;
    String name;
    //all get set
}

DTO classes are same and only have DTO as suffix for their names.

Short question is: What is best way obtaining DTO objects from Hibernate objects?

explanation: As you can see, each Bank contains multiple BankAccounts, and each BankAccount has reference to a Bank it belongs to.

So I cannot make dummy hard copy constructors because it would imply lots of manual and complex work (maybe not for this simple example but problem gets complicated as new entities are involved).

If I want to get all companies, I would do something like:

//necessary Hibernate Stuff
List<Company> companies = new ArrayList<Company>(s.createQuery("from Company").list());

Now I have only the companies in hibernate objects but I also need their respectful cities, bank accounts and banks their accounts belong to. Also I have to copy all this to a DTO object but I want to avoid duplicates.

How do I do that?

How do I return multiple sets of banks, accounts, cities, companies in multiple classes that contain references to objects that are not duplicated throughout those classes, as DTO objects?

I cannot create copy constructors in DTO objects because I may create infinite recursion, and if I do avoid that somehow, I don't know how would I not create duplicates.

I hope I was clear and didn't create duplicate myself (there are few questions but haven't seen some with GWT (DTO) objects).

Upvotes: 1

Views: 891

Answers (1)

Ioan Agopian
Ioan Agopian

Reputation: 788

You can use RequestFactory instead of GWT-RPC, so you don't have to manually copy your entities to DTO objects. RequestFactory uses simple interfaces (named entities proxies) on the client, to fill the role of DTO objects. From the docs:

RequestFactory automatically propagates bean-style properties between entities on the server and the corresponding EntityProxy on the client. Furthermore, the EntityProxy interface enables RequestFactory to compute and send only changes ("deltas") to the server.

Upvotes: 1

Related Questions