ebean-orm / ebean

Ebean ORM
https://ebean.io
Apache License 2.0
1.47k stars 261 forks source link

Add convert to POJO API #289

Closed icode closed 9 years ago

icode commented 9 years ago

Covert a entity to a POJO, remove all load lazy listeners etc entity functions. Use for more serialize/deserialize format. like api:

Ebean.toPojo(entity, PathProperties)
icode commented 9 years ago

Can support it?

rbygrave commented 9 years ago

You need to explain more what this means. Is it returning entity beans with lazy loading turned off .... or is it trying to convert into some other bean?

You should include an example of what you are trying to do to explain it better.

On 13 May 2015 at 23:41, icode notifications@github.com wrote:

Can support it?

— Reply to this email directly or view it on GitHub https://github.com/ebean-orm/avaje-ebeanorm/issues/289#issuecomment-101636284 .

icode commented 9 years ago

like use moxy protobuf deserialize current value bean, not load lazy value.

icode commented 9 years ago

like Kryo

wscommitter commented 9 years ago

I think it is the same need I have, I need to actually disable lazy loading. When you fetch a partial object and then access a field/reference that is not loaded, EBean loads it lazily. I need that it simply returns null, so dynamic introspectors like Jackson or GSON can traverse the whole graph without loading unwanted attributes. Also, it is much easier to configure this on EBean than on the other frameworks. We have a very tangled entity graph, I would use this to extract Bounded Contexts.

In his case, maybe he need a not enhanced version of the bean, since some tools inspect the internals of the object and end up acessing EBean fields. I think the only way to accomplish this is having 2 versions of the class, one not enhanced, so you could load one with lazy loading disabled and copy it to the other. The not enhanced version could be a subclass of the enhanced one, so you would avoid code duplication.

icode commented 9 years ago

@rbygrave Like @wealthsystems said, can you do it?

rbygrave commented 9 years ago

I need to actually disable lazy loading.

There is already support for disabling lazy loading on a bean (but possibly not in the form you need).

like Jackson or GSON

Typically it is not sufficient to ONLY disable lazy loading. That is, once part of the object graph can come from L2 or L3 cache then you typically also need to specify PathProperties (which is one of the reasons Ebean has PathProperties and Jackson support built in) and probably why PathProperties is included in the initial description with the toPojo() method.

In order to add a feature firstly what needs to be defined is a clear use case of what is desired. In this case for example, Ebean.toPojo(entity, PathProperties) ... is not actually sufficient for me to know what that actually does - does it return anything, how does it know what type to return etc. I realise protobuf and Kyro have been mentioned and I did look at Kyro some time back.

In summary, you need to define more precisely your use case (motivation) and give a more complete example of what exactly Ebean.toPojo() would look like in order to support your use case.

Thanks, Rob.

archer-christ commented 9 years ago

How to disable lazy loading fetures in ebean?

rbygrave commented 9 years ago

How to disable lazy loading fetures in ebean?

My mistake. I thought that was exposed through BeanState but no it is not so I'll look to add that.

A point I'd like to make though is that ... having to call BeanState.setDisableLazyLoad(true) on each bean might be painful depending on what the use case is. For example, it might be better add setDisableLazyLoad() to the query so that all beans returned from the query have lazy loading disabled. The point is that the context of the question is important - if the context of the question is clear then there is a much better chance of getting a "GOOD" solution rather than "A" solution.

At the moment I feel there is not enough context for this issue. I would like to know more about what is trying to be achieved so that a good solution is provided.

archer-christ commented 9 years ago

I'm using Ebean with Springmvc , When I want to fetch a partial object ebean works well , but when the Springmvc framework return the JSON result to the frontend the JSON lib like Jackson will access the bean's getter/fields to serialize the bean , so the lazy loading mechanism from ebean tasks effect now. it might be better add setDisableLazyLoad() to the query so that all beans returned from the query have lazy loading disabled , or some other solution ?

Thanks, EChrist.

rbygrave commented 9 years ago

add setDisableLazyLoad() to the query

Well, that is going to be a good solution and in general it applies to any time an external library is used to serialise the object graph (so GSON etc). Yes, there are performance and other reasons why I'd recommend using Ebean's internal JSON support that uses Jackson core under the hood but it is not great to be forced to do that.

So yes, I'll create an enhancement request to do this (add setDisableLazyLoad() to the query). The only thing to note with this which makes it interesting is that we want to disable lazy loading but still support secondary queries (so we can still build arbitrarily complex object graphs). I'll log the enhancement request now.

Cheers, Rob.

rbygrave commented 9 years ago

Ok, I have logged #360 which is a mechanism on the query to disable lazy loading.

At this stage I think that actually solves this problem? I'll close this assuming that is the case. Reopen if this is something else / some other scenario.

Cheers, Rob.