gpt4 book ai didi

java - Hibernate ManyToOne FetchType.LAZY 不起作用?

转载 作者:太空宇宙 更新时间:2023-11-04 12:12:29 24 4
gpt4 key购买 nike

我使用的是 spring 4.1.4.RELEASE + hibernate 4.3.6.Final,这是我的实体代码:

public class BaseEntity implements Serializable {
}


public class MarketInfo extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;

@Column(name = "market_id", unique = true, length = 15)
private String marketId;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "market")
private List<MarketChannelGroup> channelGroups;

public List<MarketChannelGroup> getChannelGroups() {
return channelGroups;
}

public void setChannelGroups(List<MarketChannelGroup> channelGroups) {
this.channelGroups = channelGroups;
}

...
}

public class MarketChannelGroup extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;


@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "market_id", referencedColumnName = "market_id")
private MarketInfo market;
...
}

从我的测试中我可以看到MarketInfo中的channelGroups工作正常(如果我不调用getChannelGroups(),则channelGroups为null),但是如果我调用getChannelGroups(),每个MarketChannelGroup内的MarketInfo都会被获取,而这种情况不应该发生,因为市场的获取模式是FetchType.LAZY。

当我调用它的 getter 时,从控制台我确实看到了以下 hibernate 日志:

Hibernate: select channelgro0_.market_id as market_i5_12_1_, channelgro0_.id as id1_9_1_, channelgro0_.id as id1_9_0_, channelgro0_.channel_group_id as channel_2_9_0_, channelgro0_.channel_group_name as channel_3_9_0_, channelgro0_.channel_group_type as channel_4_9_0_, channelgro0_.market_id as market_i5_9_0_ from market_channel_group channelgro0_ where channelgro0_.market_id=?
Hibernate: select marketinfo0_.id as id1_12_0_, marketinfo0_.enable_flag as enable_f2_12_0_, marketinfo0_.enable_time as enable_t3_12_0_, marketinfo0_.market_id as market_i4_12_0_, marketinfo0_.market_name as market_n5_12_0_, marketinfo0_.stb_count as stb_coun6_12_0_ from market_info marketinfo0_ where marketinfo0_.market_id=?
Hibernate: select marketinfo0_.id as id1_12_0_, marketinfo0_.enable_flag as enable_f2_12_0_, marketinfo0_.enable_time as enable_t3_12_0_, marketinfo0_.market_id as market_i4_12_0_, marketinfo0_.market_name as market_n5_12_0_, marketinfo0_.stb_count as stb_coun6_12_0_ from market_info marketinfo0_ where marketinfo0_.market_id=?
Hibernate: select marketinfo0_.id as id1_12_0_, marketinfo0_.enable_flag as enable_f2_12_0_, marketinfo0_.enable_time as enable_t3_12_0_, marketinfo0_.market_id as market_i4_12_0_, marketinfo0_.market_name as market_n5_12_0_, marketinfo0_.stb_count as stb_coun6_12_0_ from market_info marketinfo0_ where marketinfo0_.market_id=?

有人可以帮忙吗?

更新

ManyToOne 注释没有可选方法,因此 OneToOne 中的解决方案不适用于我的情况。

最佳答案

将 @OneToMany 注释从声明移动到 getter 方法,如下所示:

private List<MarketChannelGroup> channelGroups;

@OneToMany(fetch = FetchType.LAZY, mappedBy = "market")
public List<MarketChannelGroup> getChannelGroups() {
return channelGroups;
}

关于java - Hibernate ManyToOne FetchType.LAZY 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39748211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com