FetchMode如何在Spring Data JPA中工作
我的项目中确实有三个模型对象之间的关系(文章末尾的模型和存储库片段)。
当我调用PlaceRepository.findById
它时,会触发三个选择查询:
(“ sql”)
SELECT * FROM place p where id = arg
SELECT * FROM user u where u.id = place.user.id
SELECT * FROM city c LEFT OUTER JOIN state s on c.woj_id = s.id where c.id = place.city.id
(对我而言)这是非常不正常的行为。据阅读Hibernate
文档后所知,它应该始终使用JOIN查询。在类中FetchType.LAZY
更改为 查询时(带有附加SELECT
的查询)没有什么区别,在类中更改为 (使用JOIN 查询时)的查询也没有区别。FetchType.EAGERPlaceCityFetchType.LAZYFetchType.EAGER
当我使用CityRepository.findById
抑制射击时,有两个选择:
SELECT * FROM city c where id = arg
SELECT * FROM state s where id = city.state.id
我的目标是在所有情况下都具有sam行为(始终为JOIN或SELECT,尽管首选JOIN)。
型号定义:
地点:
@Entity@Table(name = "place")
public class Place extends Identified {
@Fetch(FetchMode.JOIN)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_user_author")
private User author;
@Fetch(FetchMode.JOIN)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "area_city_id")
private City city;
//getters and setters
}
City:
@Entity@Table(name = "area_city")
public class City extends Identified {
@Fetch(FetchMode.JOIN)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "area_woj_id")
private State state;
//getters and setters
}
Repositories:
PlaceRepository
public interface PlaceRepository extends JpaRepository<Place, Long>, PlaceRepositoryCustom { Place findById(int id);
}
UserRepository:
public interface UserRepository extends JpaRepository<User, Long> { List<User> findAll();
User findById(int id);
}
CityRepository:
public interface CityRepository extends JpaRepository<City, Long>, CityRepositoryCustom { City findById(int id);
}
回答:
我认为Spring Data忽略了FetchMode。在使用Spring Data时,我总是使用@NamedEntityGraph
和@EntityGraph
批注
@Entity@NamedEntityGraph(name = "GroupInfo.detail",
attributeNodes = @NamedAttributeNode("members"))
public class GroupInfo {
// default fetch mode is lazy.
@ManyToMany
List<GroupMember> members = new ArrayList<GroupMember>();
…
}
@Repository
public interface GroupRepository extends CrudRepository<GroupInfo, String> {
@EntityGraph(value = "GroupInfo.detail", type = EntityGraphType.LOAD)
GroupInfo getByGroupName(String name);
}
以上是 FetchMode如何在Spring Data JPA中工作 的全部内容, 来源链接: utcz.com/qa/423173.html