你可以解释一下@JoinColumn注释在没有任何其他关系注释的情况下对一个字段的默认行为。
@Entity
class Employee{
@Id int id;
@JoinColumn(name = "man_id")
Employee manager;
}
jpa是如何进行的?如果员工是与部门有关的双向关系的多方面,其中fetchtype lazy指定在双方,当我访问(询问大小的集合)getEmployees部门实体的属性时,eclipcelink生成sql并从员工实体中选择所有记录。
SELECT ID FROM deps WHERE (ID = ?)
SELECT ID, name, man_id, dep_id FROM emps WHERE (dep_id = ?)
additional requests
SELECT ID, name, man_id, dep_id FROM emps WHERE (ID = ?)
SELECT ID, name, man_id, dep_id FROM emps WHERE (ID = ?)
SELECT ID, name, man_id, dep_id FROM emps WHERE (ID = ?)
.................
如果我用@OnetoOne(实际上是我的意思)将这个属性标记为关系并设置fetchtype为lazy,那么"的工作正确"这意味着在我向员工询问之前,不会提取经理财产。
SELECT ID FROM deps WHERE (ID = ?)
SELECT ID, name, man_id, dep_id FROM deps WHERE (dep_id = ?)
答案 0 :(得分:0)
By not putting a relationship type/annotation, actually means JPA would not treat your attribute as an entity.
Perhaps, you already had your schema in place that's why your queries are working. However, I experimented on this and started with a fresh database. The behavior is different from what you expected. With Hibernate, my chosen JPA provider (with hbm2ddl
property set to create
), it generated the ff. schema:
create table Employee (
id bigint not null auto_increment,
manager tinyblob,
name varchar(255),
primary key (id)
)
Since my manager is an Employee
class implementing Serializable
interface, it was by default mapped to a BLOB type column.
Also by not mapping it with any relationship annotations, means that there will be no foreign key constraints applied to your manager column.