我使用hibernate 4.1.6.Final。在我的情况下,与@JoinTable的@OneToOne关联的延迟加载不起作用。
TABLES:
CREATE TABLE PERSON
(
"ID" NUMBER NOT NULL ENABLE,
"NAME" VARCHAR2(200 BYTE) NOT NULL,
CONSTRAINT "PERSON_PK" PRIMARY KEY ("ID")
);
CREATE TABLE ADDRESS
(
"ID" NUMBER NOT NULL ENABLE,
"ADDR" VARCHAR2(200 BYTE) NOT NULL,
CONSTRAINT "ADDRESS_PK" PRIMARY KEY ("ID")
);
CREATE TABLE PERSON_ADDRESS
(
"P_ID" NUMBER NOT NULL,
"A_ID" NUMBER NOT NULL,
CONSTRAINT "PA_PK" PRIMARY KEY ("P_ID","A_ID"),
CONSTRAINT "PERS_FK" FOREIGN KEY ("P_ID") REFERENCES PERSON ("ID"),
CONSTRAINT "ADD_FK" FOREIGN KEY ("A_ID") REFERENCES ADDRESS ("ID"),
CONSTRAINT "A_UNIQUE" UNIQUE ("A_ID"),
CONSTRAINT "P_UNIQUE" UNIQUE ("P_ID")
);
和映射
@Entity
@Table(name = "PERSON")
@SequenceGenerator(name = "EMP_SEQ1", sequenceName = "EMP_SEQ")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "EMP_SEQ1")
@Column(name = "ID")
public Long id;
@Column(name = "NAME")
public String name;
@OneToOne(fetch = FetchType.LAZY, optional = false)
@JoinTable(name = "PERSON_ADDRESS", joinColumns = @JoinColumn(name = "P_ID"), inverseJoinColumns = @JoinColumn(name = "A_ID"))
public Address address;
}
@Entity
@Table(name = "ADDRESS")
@SequenceGenerator(name = "EMP_SEQ1", sequenceName = "EMP_SEQ")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "EMP_SEQ1")
@Column(name = "ID")
public Long id;
@Column(name = "ADDR")
public String name;
}
当我通过id获取Person对象时,如
Person person = (Person)session.get(Person.class,1L);
hibernate生成此查询
select
person0_.ID as ID10_0_,
person0_.NAME as NAME10_0_,
person0_1_.A_ID as A1_11_0_
from
PERSON person0_
left outer join
PERSON_ADDRESS person0_1_
on person0_.ID=person0_1_.P_ID
where
person0_.ID=?
如何避免“左外连接”和延迟加载Address类