且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Ebean @ManyToOne,发现程序未检索到相关对象的所有数据

更新时间:2023-02-11 08:26:21

  1. 您可以使用fetch()来急切地获取图形的其他部分.在这种情况下,请提取公司名称,例如:

    Employee.find.fetch("company","name").where().eq("id",1).findUnique();

  2. 简而言之,无法拦截现场访问(除非您增强了呼叫者).因此,对company.name使用字段访问意味着customer.name是GETFIELD操作,并且未被Ebean拦截,因此未调用延迟加载(因此返回了null).

更改为使用getter/setter意味着在调用customer.getName()时调用了惰性加载.

Java不支持属性(请访问getter和setter).您可以查看其他类似Groovy和Kotlin的JVM语言.

Groovy支持属性,将Groovy与@CompileStatic结合使用的示例是: https://github.com/ebean-orm/avaje-ebeanorm-examples/blob/master/e-groovy https://github.com/ebean-orm/avaje-ebeanorm-examples/tree/master/e-kotlin-maven

干杯,罗布.

I'm using Ebean for my object-mapping and I've made my SQL tables like so

create table company (
  id                int auto_increment not null primary key,
  name              varchar(100)
);

create table employee (
  id                int auto_increment not null primary key,
  name              varchar(100) not null,
  company_id        int not null,
  constraint foreign key (company_id) references company (id)
      on delete restrict on update restrict
);

This is the company Ebean model

import javax.persistence.Entity;
import javax.persistence.Id;
import com.avaje.ebean.Model;
@Entity
public class Company extends Model
{
    @Id
    public Integer id;
    public String name;
}

And employee model

@Entity
public class Employee extends Model
{
    @Id
    public Integer id;
    public String name;
    @ManyToOne
    public Company company;

    public static Finder<Long, Employee> find = new Finder<Long, Employee>(Long.class, Employee.class);
}

When I run the following

Company company = new Company();
company.name = "Microsoft";
company.save();

Employee employee = new Employee();
employee.name = "Mr John";
employee.company = company;
employee.save();

Employee mrJohn = Employee.find.where().eq("id", 1).findUnique();
System.out.println(mrJohn.company.id);
System.out.println(mrJohn.company.name);

The first System.out.println gives 1 (which is the correct id of the company assigned to the employee) but the second shows null (which I expected should have the value "Microsoft"), the output being

1
null

The question therefore is why is only the id of the Company model retrieved, and not the other associated data?

  1. You can use fetch() to eagerly fetch other parts of the graph. In this case fetch the company name like:

    Employee.find.fetch("company","name").where().eq("id",1).findUnique();

  2. In short field access can not be intercepted (unless you enhance the caller). So using field access for company.name meant that customer.name was a GETFIELD operation and that it was not being intercepted by Ebean and hence lazy loading was not invoked (and hence a null was returned).

Changing over to use getters/setters meant that lazy loading was invoked when customer.getName() was called.

Java does not support properties (hance the getters and setters). You can look at other JVM languages that do like Groovy and Kotlin.

Groovy supports properties, an example using Groovy with @CompileStatic is: https://github.com/ebean-orm/avaje-ebeanorm-examples/blob/master/e-groovy https://github.com/ebean-orm/avaje-ebeanorm-examples/blob/master/e-groovy/src/main/groovy/org/example/domain/Customer.groovy

Kotlin supports properties, an example is: https://github.com/ebean-orm/avaje-ebeanorm-examples/tree/master/e-kotlin-maven

Cheers, Rob.