JPA会急切地获取非实体属性吗?(Will JPA eagerly fetch non-entity attributes?)
我想知道在创建常规查询时是否急切地或懒惰地获取诸如Int或Varchar2类的属性。
@Entity @Table(name = "ROOM") public class Room implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue @Column(name = "room_id") private Integer id; @Column(name = "number") private String number; @Column(name = "capacity") private Integer capacity; //Will this be fetched eagerly??? @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "building_id") private Building building; ... ... ... }基本上:
Room room = getRoomById(5); // select room from Room room where room.id = 5; Integer roomCap = room.getCapacity(); //Will this create another query?或者它已经在room内了?
I want to know if attributes such as Int or Varchar2 are fetched eagerly or lazily when creating a regular query.
@Entity @Table(name = "ROOM") public class Room implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue @Column(name = "room_id") private Integer id; @Column(name = "number") private String number; @Column(name = "capacity") private Integer capacity; //Will this be fetched eagerly??? @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "building_id") private Building building; ... ... ... }Basically:
Room room = getRoomById(5); // select room from Room room where room.id = 5; Integer roomCap = room.getCapacity(); //Will this create another query?Or will it be inside the room object already?
最满意答案
渴望或懒惰的提取策略适用于您希望从另一个表加载数据而不是属于同一实体或表的列的关系或关联实体。 检索实体时不会获取属于同一实体或表的列或字段,也不会触发单独的查询。
The fetching strategies eager or lazy apply to relationships or associated entities where you want to load the data from another table and not to the columns that are part of the same entity or table. Columns or fields that belong to the same entity or table are fetched when you retrieve the entity and no separate query is fired.
更多推荐
发布评论