Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent Lazy loading in Ebean using Jackson?

I have the following scenario:

public class ContrivedEntity extends Model {

    private Long id;
    private Shipper shipper;
    private Location location;
}

public class Shipper extends Model {

    private Long id;
    private Country country;

}

A ContrivedEntity has several relationships and its relationships themselves also have dependencies... I created a functionality where the user is able to search Contrived Entities.... This search feature does not need dependencies to be loaded, but when I serialize the results using the play's helper Json.toJson it lazy loads the other dependencies resulting in several other queries...

I have managed to prevent this behavior by using Jackson Views(using annotations in the entities that I would like to ignore in the final result)... but I'm looking for something that could be done on Ebean's end... while composing an EBean query...

Is it possible to prevent lazy loading in this manner? In case of a 'No' answer... Does Hibernate provide such functionality? Any help would be really appreciated... If you need more context just leave a comment and I will edit the question.

Thank you so much

like image 278
rodrigoArantes Avatar asked Aug 30 '26 09:08

rodrigoArantes


2 Answers

I think you can use Ebean's PathProperties and JSON support to do exactly what you want. Ebean has good JSON support built in and this is one of the reasons for it to do so.

That is, you can use PathProperties to define the properties you wish to include in both the JSON output and query. In current Ebean you use Query.apply(pathProperties) ... to tell the query what part of the object graph to fetch.

An example:

PathProperties pathProperties =
        PathProperties.parse("(id,status,name,shippingAddress(id,line1,city),billingAddress(*),contacts(*))");

List<Customer> customers = Ebean.find(Customer.class)
    .apply(pathProperties)
    .findList();

String jsonString = Ebean.json().toJson(customers, pathProperties);
System.out.println(jsonString);

Note that Ebean now used Jackson core internally for it's built in JSON support and there is also a Jackson Ebean module in github but not yet released.

It might be good to map Jackson views to Ebean 'pathProperties' but I have not looked at that yet.

Hopefully that helps Cheers, Rob.

like image 68
Rob Bygrave Avatar answered Sep 03 '26 22:09

Rob Bygrave


...but I'm looking for something that could be done on Ebean's end... while composing an Ebean query...

It's not possible, many people would love this feature (me 2), the only sensible way is creating dedicated model class and filling it "manually" with the selected data.

Also using reflections is good way to consider, especially if you have advanced REST api with many different cases.

like image 33
biesior Avatar answered Sep 04 '26 00:09

biesior



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!