0

I'm using Hibernate for SQL query. After I get a list of MyEntity beans, I want to create a list of CustomEntity beans which contain data same as MyEntity but only with the fields I want. I want to create this CustomEntity because I don't want to send back all information to the client but when I retrieve from database, I want to retrieve all information. I don't want to select columns in query. What is the correct pattern to implement this CustomEntity bean and load values into it ?

TechCrunch
  • 2,924
  • 5
  • 45
  • 79
  • and what have you tried? and what is your entity? and what reduced set of data do you want to get? and why are you using SQL and not JPQL? why do you think there is just one "correct" way? – Neil Stockton Feb 01 '15 at 14:07
  • 3
    If I understand correctly, you're looking for a for loop: `for (MyEntity e : list) { resultList.add(createCustomObjectFromMyEntity(e)); }`. How could it be simpler? – JB Nizet Feb 01 '15 at 14:16
  • @JBNizet you are correct. – TechCrunch Feb 01 '15 at 15:26
  • Is that for a json response? – Martin Frey Feb 01 '15 at 16:39
  • Yes, I need to send back a JSON response. Since I have a join in my entity with a look up table, the lookup table record is also going back in json at this time. Thats why I want to construct a custom response object. – TechCrunch Feb 01 '15 at 17:08
  • 1
    From your comments I understood that you want to exclude a field while serializing the JSON. If you are using Gson library you can mark the entity with `@Expose` annotation for the fields which you want to be present in the JSON and the library will exclude the rest. check this [link](http://stackoverflow.com/questions/4802887/gson-how-to-exclude-specific-fields-from-serialization-without-annotations) – Aditya Feb 01 '15 at 17:49
  • @Aditya, is there a way to include a key/value in the new JSON, because I'll be excluding the JOIN column, I want to keep the key value of the lookup table used in JOIN. May be use a transient? – TechCrunch Feb 01 '15 at 18:12

3 Answers3

0

One way to do is to write a converter which will take list of your entities and convert it to the custome entity and return that object. another way is to use inheritance(hibernate inheritance) and then fire the same query but I think in your case its not required so I would go with the first approach. I am guessing here is that you need to display this custom object on view. If this is the reason you are doing it, then you should use DTO and write a converter which will convert your actual entities in to custom ones.

Here are some links Wiki DTO & Transfer Object Pattern

Bilbo Baggins
  • 2,899
  • 10
  • 52
  • 77
0

I think you cant do that only if you have a new Entity CustomEntity(with required column).For Example

MyEntity

class MyEntity{

private long id;

private String att1;

private String att2;

}

and if you only want att1 in CustomEntity so you have to create

CustomEntity

 class CustomEntity{

    private long id;

    private String att1;

   }

Then In you controller get all the list of MyEntity then create CustomEntity like

    List<MyEntity> me=MyEntity.all();  //return List of MyEntity

    for(MyEntity m:me){

    CustomEntity cm=new CustomEntity();

    cm.setAtt1(m.getAtt1);              //setter and getter

    CustomEntity.create(cm);            //create method of CustomEntity
    }

   List<CustomEntity> cmList=CustomEntity.all();  //get List of all Customentity

Then if you are done with that you can delete all CustomEntity

singhakash
  • 7,891
  • 6
  • 31
  • 65
0

If this is about filtering certain properties for a JSON response you could simply put some @JsonIgnore annotations on the fields you dont want to serialize. This is normally not enough as for several responses you probably like to serialize the same object in different ways.

One solution is indeed to create DTOs for each of these responses and use them together with Jackson for example. A "simple" for loop and create the object you need. This is foolproof in the means of not having to deal with bidirection in json serialisation for example but it might need you to generate a lot of code.

Another way would be JsonViews where you can define what is to be serialised. I cannot tell you alot about that as i have never used it.

My favored way is JsonFilter. This works similar to views but there is "only" a very basic implementation in jackson which allows filtering on specific classes.

Just yesterday I have created a github project which is supposed to deal with filtering based on the absolute path. (Ant style) Here's the link. Probably it fulfills your need.

https://github.com/Antibrumm/jackson-antpathfilter

If you are dealing directly with hibernate entities in the view layer you might need the `OpenEntityManagerInViewInterceptor' to avoid LazyLoadingExceptions.

Martin Frey
  • 10,025
  • 4
  • 25
  • 30