Ebean @ManyToOne,发现程序未检索到相关对象的所有数据 [英] Ebean @ManyToOne, finder doesn't retrieve all data of related object
问题描述
我正在使用Ebean进行对象映射,并且已经像这样制作了SQL表
创建表公司(id int auto_increment不为null的主键,名称varchar(100));创建表员工(id int auto_increment不为null的主键,名称varchar(100)不为null,company_id int不为null,约束外键(company_id)引用公司(id)关于删除限制关于更新限制);
这是公司的Ebean模型
import javax.persistence.Entity;导入javax.persistence.Id;导入com.avaje.ebean.Model;@实体公共类公司扩展模型{@ID公共整数ID;公共字符串名称;}
和员工模型
@Entity公共类员工扩展模型{@ID公共整数ID;公共字符串名称;@多多上市公司;公共静态查找器<员工长>find = new Finder< Long,Employee>(Long.class,Employee.class);}
运行以下命令
Company company = new Company();company.name =微软";company.save();Employee员工= new Employee();employee.name =约翰先生";employee.company =公司;employee.save();员工mrJohn = Employee.find.where().eq("id",1).findUnique();System.out.println(mrJohn.company.id);System.out.println(mrJohn.company.name);
第一个System.out.println给出1(这是分配给该雇员的公司的正确ID),但是第二个显示null(我希望该值应为"Microsoft"),输出为
1空值
因此,问题是为什么只检索Company模型的ID,而不检索其他相关数据?
-
您可以使用fetch()来急切地获取图形的其他部分.在这种情况下,请提取公司名称,例如:
Employee.find.fetch("company","name").where().eq("id",1).findUnique();
-
简而言之,无法拦截现场访问(除非您增强了呼叫者).因此,对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?
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();
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.
这篇关于Ebean @ManyToOne,发现程序未检索到相关对象的所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!