ebean-orm / ebean

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

Miss data when I save a @Entity which has Lazy fetch type #3456

Closed deverlex closed 1 week ago

deverlex commented 1 month ago

Hi, I have complex relation of entities

public class A extends Model {
    @Id
    private Long id;
    @ManyToOne
    private B child;
}
public class B extends Model {
    @Id
    private Long id;
    @ManyToOne(fetch = FetchType.LAZY)
    private C data;
}

When I find A by @Id and access to B (child), I see the C (data ) is null. I added B (child) to a list and call saveAll() them. The C data to be null.

I think B (child) will have C data object has Id only

rbygrave commented 1 month ago

I added B (child) to a list and call saveAll() them

What list? You haven't actually shown us what you are doing.

deverlex commented 1 month ago

I added B (child) to a list and call saveAll() them

What list? You haven't actually shown us what you are doing.

I found a list A from database, and I get B from a items and add to a list

List<A> listA = finder.query().findList();
List<B> listB = new ArrayList<>();
for (A a : listA) {
  B b = a.getChild();
  b.setOther(...)
  listB.add(b);
}
DB.byName(dbName).saveAll(listB);
deverlex commented 1 month ago

I added B (child) to a list and call saveAll() them

What list? You haven't actually shown us what you are doing.

oh, I see the field has fetch type is lazy is not load object with id straightway. But after some call get any fields, the field have been loaded object with id.

How to load object with id straightway?

rbygrave commented 2 weeks ago

How to load object with id straightway?

Can you ask your question again? I mean, the mapping has FetchType.LAZY, you need to show us the ORM query that you are using + the mapping (like FetchType.LAZY) + the SQL that is generated ... and state why it doesn't do what you think it should be doing. Can you provide that?

rbygrave commented 1 week ago

Maybe we should close this if you don't have any follow up question here?

deverlex commented 1 week ago

How to load object with id straightway?

Can you ask your question again? I mean, the mapping has FetchType.LAZY, you need to show us the ORM query that you are using + the mapping (like FetchType.LAZY) + the SQL that is generated ... and state why it doesn't do what you think it should be doing. Can you provide that?

Thank u for your time. I think I need call a setter for an field that is object in ebean.