使用OneToOne从Hibernate查询中删除联接

时间:2015-07-24 09:52:26

标签: java hibernate jpa

我有两个简单的实体,一对一映射:

@Entity
@Table(name="DRIVER")
public class Driver implements Serializable {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="ID")
private long id;

@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="CAR_ID",referencedColumnName="ID")
private Car car;
}

@Entity
@Table(name="CAR")
public class Car {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="ID")
private Long id;

@OneToOne(mappedBy="car")
private Driver driver;

@Column(name="MILEAGE")
private Long mileage;
}

要更新我打电话的里程数:

    Car c = em.find(Car.class, id);
    c.setMileage(mileage);

Hibernate查询如下所示:

select
    car0_.ID as ID0_1_,
    car0_.DATE_OF_PURCHASE as DATE2_0_1_,
    car0_.MILEAGE as MILEAGE0_1_,
    car0_.MODEL as MODEL0_1_,
    car0_.PRODUCER as PRODUCER0_1_,
    driver1_.ID as ID1_0_,
    driver1_.CAR_ID as CAR3_1_0_,
    driver1_.NAME as NAME1_0_ 
from
    CAR car0_ 
left outer join
    DRIVER driver1_ 
        on car0_.ID=driver1_.CAR_ID 
where
    car0_.ID=?

我想要的只是查询Car表并删除连接操作。 我试图在两边放置FetchType.LAZY,它稍微改变了查询,但它没有删除连接。

有可能在这样的模型中做到吗?

看起来每当您查询拥有实体时,也会查询拥有的实体,但我不确定这是否是规则。

0 个答案:

没有答案