我想使用主键获取eventdate的对象。下面是我正在执行的查询
EventData eventData = entityManager.find(EventData.class, eventdataid);
在控制台中执行这个命令之后,我得到的查询是
select eventsajgj0_.FILE_ID as FILE_ID8_14_0_, eventsajgj0_.id as
id1_12_0_, eventsajgj0_.id as id1_12_1_, eventsajgj0_.CODE as CODE2_12_1_,
eventsajgj0_.DATE as DATE3_12_1_, eventsajgj0_.FILE_ID as FILE_ID8_12_1_,
eventsajgj0_.MILLIS as MILLIS4_12_1_, eventsajgj0_.ORDER_NR as
ORDER_NR5_12_1_, eventsajgj0_.TYPE as TYPE6_12_1_, eventsajgj0_.VALUE as
VALUE7_12_1_ from eventdata eventsajgj0_**where eventsajgj0_.FILE_ID=?**
order by eventsajgj0_.ORDER_NR
请注意,上面查询中的where子句是针对文件id(外键)而不是id(eventdata主键)
dao结构如下
public class EventData implements Serializable {
private static final long serialVersionUID = 1L;
public EventData() {
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="FILE_ID")
private ApplicationFile file;
getter & setters
}
public class ApplicationFile implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
bi-directional many-to-one association to Event
@OneToMany(mappedBy="file", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@OrderBy("orderNr")
private List<EventData> eventsajgjd;
getter & setters
}
我的问题是,当我在eventdata表上执行查询时,为什么要使用file\ id而不是id进行查询。
ps:如果我将applicationfile的fetch类型更改为lazy,那么执行的查询是在id上,而不是在文件id上。
(从注解中添加:)
CREATE TABLE eventdata (
ID int(11) NOT NULL AUTO_INCREMENT,
FILE_ID int(11) DEFAULT NULL,
PRIMARY KEY (ID),
KEY eventdata_ibfk_1 (FILE_ID),
CONSTRAINT eventdata_ibfk_1 FOREIGN KEY (FILE_ID)
REFERENCES files (ID) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=297502 DEFAULT CHARSET=utf8
1条答案
按热度按时间xmd2e60i1#
我打赌是因为你绘制了
EventData
/ApplicationFile
双向(您有一个类型为List<EventData>
在ApplicationFile
实体所以加载一个
EventData
意味着急切地加载相关的ApplicationFile
因此急切地加载所有相关的EventData
.我假设相关的applicationfile示例已经在entitymanager一级缓存中(否则查询应该连接到files表)