I have a Person class:
我有一个Person类:
@Entity
public class Person {
@Id
@GeneratedValue
private Long id;
@ManyToMany(fetch = FetchType.LAZY)
private List<Role> roles;
// etc
}
With a many-to-many relation that is lazy.
与多对多的关系是懒惰的。
In my controller I have
在我的控制器中我有
@Controller
@RequestMapping("/person")
public class PersonController {
@Autowired
PersonRepository personRepository;
@RequestMapping("/get")
public @ResponseBody Person getPerson() {
Person person = personRepository.findOne(1L);
return person;
}
}
And the PersonRepository is just this code, written according to this guide
而PersonRepository只是这个代码,根据本指南编写
public interface PersonRepository extends JpaRepository<Person, Long> {
}
However, in this controller I actually need the lazy-data. How can I trigger its loading?
但是,在这个控制器中,我实际上需要惰性数据。如何触发加载?
Trying to access it will fail with
试图访问它将失败
failed to lazily initialize a collection of role: no.dusken.momus.model.Person.roles, could not initialize proxy - no Session
未能懒惰地初始化角色集合:no.dusken.momus.model.Person.roles,无法初始化代理 - 没有会话
or other exceptions depending on what I try.
或其他例外取决于我尝试的内容。
My xml-description, in case needed.
我的xml描述,如果需要的话。
Thanks.
谢谢。
6 个解决方案
#1
162
You will have to make an explicit call on the lazy collection in order to initialize it (common practice is to call .size()
for this purpose). In Hibernate there is a dedicated method for this (Hibernate.initialize()
), but JPA has no equivalent of that. Of course you will have to make sure that the invocation is done, when the session is still available, so annotate your controller method with @Transactional
. An alternative is to create an intermediate Service layer between the Controller and the Repository that could expose methods which initialize lazy collections.
您必须对延迟集合进行显式调用才能对其进行初始化(通常的做法是为此目的调用.size())。在Hibernate中有一个专门的方法(Hibernate.initialize()),但JPA没有相应的。当然,当会话仍然可用时,您必须确保调用已完成,因此使用@Transactional注释您的控制器方法。另一种方法是在Controller和Repository之间创建一个中间服务层,它可以公开初始化惰性集合的方法。
Update:
Please note that the above solution is easy, but results in two distinct queries to the database (one for the user, another one for its roles). If you want to achieve better performace add the following method to your Spring Data JPA repository interface:
请注意,上述解决方案很简单,但会导致对数据库的两个不同查询(一个针对用户,另一个针对其角色)。如果要实现更好的性能,请将以下方法添加到Spring Data JPA存储库接口:
public interface PersonRepository extends JpaRepository<Person, Long> {
@Query("SELECT p FROM Person p JOIN FETCH p.roles WHERE p.id = (:id)")
public Person findByIdAndFetchRolesEagerly(@Param("id") Long id);
}
This method will use JPQL's fetch join clause to eagerly load the roles association in a single round-trip to the database, and will therefore mitigate the performance penalty incurred by the two distinct queries in the above solution.
此方法将使用JPQL的fetch join子句在单个往返数据库中急切加载角色关联,从而减轻上述解决方案中两个不同查询所带来的性能损失。
#2
25
Though this is an old post, please consider using @NamedEntityGraph (Javax Persistence) and @EntityGraph (Spring Data JPA). The combination works.
虽然这是一篇旧帖子,但请考虑使用@NamedEntityGraph(Javax Persistence)和@EntityGraph(Spring Data JPA)。组合起作用。
Example
例
@Entity
@Table(name = "Employee", schema = "dbo", catalog = "ARCHO")
@NamedEntityGraph(name = "employeeAuthorities",
attributeNodes = @NamedAttributeNode("employeeGroups"))
public class EmployeeEntity implements Serializable, UserDetails {
// your props
}
and then the spring repo as below
然后是如下的弹簧回购
@RepositoryRestResource(collectionResourceRel = "Employee", path = "Employee")
public interface IEmployeeRepository extends PagingAndSortingRepository<EmployeeEntity, String> {
@EntityGraph(value = "employeeAuthorities", type = EntityGraphType.LOAD)
EmployeeEntity getByUsername(String userName);
}
#3
12
You have some options
你有一些选择
- Write a method on repository that return a initialized entity as R.J suggested.
- 在存储库上编写一个返回初始化实体的方法,如R.J所建议的那样。
More work, best performance.
更多的工作,最好的表现。
- Use OpenEntityManagerInViewFilter to keep session open for the entire request.
- 使用OpenEntityManagerInViewFilter为整个请求保持会话打开。
Less work, usually acceptable in web enviroments.
减少工作量,通常在网络环境中可以接受。
- Use a helper class to initialize entities when required.
- 使用辅助类在需要时初始化实体。
Less work, useful when OEMIV is not at option, for example in a Swing application, but may be useful too on repository implementations to initialize any entity in one shot.
减少工作量,当OEMIV不可用时很有用,例如在Swing应用程序中,但在存储库实现上也可能有用,可以一次性初始化任何实体。
For the last option, I wrote a utility class, JpaUtils to initilize entities at some deph.
对于最后一个选项,我写了一个实用程序类,JpaUtils来在某些deph中初始化实体。
For example:
例如:
@Transactional
public class RepositoryHelper {
@PersistenceContext
private EntityManager em;
public void intialize(Object entity, int depth) {
JpaUtils.initialize(em, entity, depth);
}
}
#4
6
it can only be lazily loaded whilst within a transaction. So you could access the collection in your repository, which has a transaction - or what I normally do is a get with association
, or set fetchmode to eager.
它只能在交易中延迟加载。因此,您可以访问存储库中的集合,该集合具有事务 - 或者我通常所做的是获取关联,或将fetchmode设置为eager。
#5
5
I think you need OpenSessionInViewFilter to keep your session open during view rendering (but it is not too good practice).
我认为你需要OpenSessionInViewFilter来保持你的会话在视图渲染过程中打开(但这不是太好的做法)。
#6
0
You can do the same like this:
你可以像这样做:
@Override
public FaqQuestions getFaqQuestionById(Long questionId) {
session = sessionFactory.openSession();
tx = session.beginTransaction();
FaqQuestions faqQuestions = null;
try {
faqQuestions = (FaqQuestions) session.get(FaqQuestions.class,
questionId);
Hibernate.initialize(faqQuestions.getFaqAnswers());
tx.commit();
faqQuestions.getFaqAnswers().size();
} finally {
session.close();
}
return faqQuestions;
}
Just use faqQuestions.getFaqAnswers().size()nin your controller and you will get the size if lazily intialised list, without fetching the list itself.
只需在您的控制器中使用faqQuestions.getFaqAnswers()。size(),如果懒惰的初始化列表,您将获得大小,而无需获取列表本身。
#1
162
You will have to make an explicit call on the lazy collection in order to initialize it (common practice is to call .size()
for this purpose). In Hibernate there is a dedicated method for this (Hibernate.initialize()
), but JPA has no equivalent of that. Of course you will have to make sure that the invocation is done, when the session is still available, so annotate your controller method with @Transactional
. An alternative is to create an intermediate Service layer between the Controller and the Repository that could expose methods which initialize lazy collections.
您必须对延迟集合进行显式调用才能对其进行初始化(通常的做法是为此目的调用.size())。在Hibernate中有一个专门的方法(Hibernate.initialize()),但JPA没有相应的。当然,当会话仍然可用时,您必须确保调用已完成,因此使用@Transactional注释您的控制器方法。另一种方法是在Controller和Repository之间创建一个中间服务层,它可以公开初始化惰性集合的方法。
Update:
Please note that the above solution is easy, but results in two distinct queries to the database (one for the user, another one for its roles). If you want to achieve better performace add the following method to your Spring Data JPA repository interface:
请注意,上述解决方案很简单,但会导致对数据库的两个不同查询(一个针对用户,另一个针对其角色)。如果要实现更好的性能,请将以下方法添加到Spring Data JPA存储库接口:
public interface PersonRepository extends JpaRepository<Person, Long> {
@Query("SELECT p FROM Person p JOIN FETCH p.roles WHERE p.id = (:id)")
public Person findByIdAndFetchRolesEagerly(@Param("id") Long id);
}
This method will use JPQL's fetch join clause to eagerly load the roles association in a single round-trip to the database, and will therefore mitigate the performance penalty incurred by the two distinct queries in the above solution.
此方法将使用JPQL的fetch join子句在单个往返数据库中急切加载角色关联,从而减轻上述解决方案中两个不同查询所带来的性能损失。
#2
25
Though this is an old post, please consider using @NamedEntityGraph (Javax Persistence) and @EntityGraph (Spring Data JPA). The combination works.
虽然这是一篇旧帖子,但请考虑使用@NamedEntityGraph(Javax Persistence)和@EntityGraph(Spring Data JPA)。组合起作用。
Example
例
@Entity
@Table(name = "Employee", schema = "dbo", catalog = "ARCHO")
@NamedEntityGraph(name = "employeeAuthorities",
attributeNodes = @NamedAttributeNode("employeeGroups"))
public class EmployeeEntity implements Serializable, UserDetails {
// your props
}
and then the spring repo as below
然后是如下的弹簧回购
@RepositoryRestResource(collectionResourceRel = "Employee", path = "Employee")
public interface IEmployeeRepository extends PagingAndSortingRepository<EmployeeEntity, String> {
@EntityGraph(value = "employeeAuthorities", type = EntityGraphType.LOAD)
EmployeeEntity getByUsername(String userName);
}
#3
12
You have some options
你有一些选择
- Write a method on repository that return a initialized entity as R.J suggested.
- 在存储库上编写一个返回初始化实体的方法,如R.J所建议的那样。
More work, best performance.
更多的工作,最好的表现。
- Use OpenEntityManagerInViewFilter to keep session open for the entire request.
- 使用OpenEntityManagerInViewFilter为整个请求保持会话打开。
Less work, usually acceptable in web enviroments.
减少工作量,通常在网络环境中可以接受。
- Use a helper class to initialize entities when required.
- 使用辅助类在需要时初始化实体。
Less work, useful when OEMIV is not at option, for example in a Swing application, but may be useful too on repository implementations to initialize any entity in one shot.
减少工作量,当OEMIV不可用时很有用,例如在Swing应用程序中,但在存储库实现上也可能有用,可以一次性初始化任何实体。
For the last option, I wrote a utility class, JpaUtils to initilize entities at some deph.
对于最后一个选项,我写了一个实用程序类,JpaUtils来在某些deph中初始化实体。
For example:
例如:
@Transactional
public class RepositoryHelper {
@PersistenceContext
private EntityManager em;
public void intialize(Object entity, int depth) {
JpaUtils.initialize(em, entity, depth);
}
}
#4
6
it can only be lazily loaded whilst within a transaction. So you could access the collection in your repository, which has a transaction - or what I normally do is a get with association
, or set fetchmode to eager.
它只能在交易中延迟加载。因此,您可以访问存储库中的集合,该集合具有事务 - 或者我通常所做的是获取关联,或将fetchmode设置为eager。
#5
5
I think you need OpenSessionInViewFilter to keep your session open during view rendering (but it is not too good practice).
我认为你需要OpenSessionInViewFilter来保持你的会话在视图渲染过程中打开(但这不是太好的做法)。
#6
0
You can do the same like this:
你可以像这样做:
@Override
public FaqQuestions getFaqQuestionById(Long questionId) {
session = sessionFactory.openSession();
tx = session.beginTransaction();
FaqQuestions faqQuestions = null;
try {
faqQuestions = (FaqQuestions) session.get(FaqQuestions.class,
questionId);
Hibernate.initialize(faqQuestions.getFaqAnswers());
tx.commit();
faqQuestions.getFaqAnswers().size();
} finally {
session.close();
}
return faqQuestions;
}
Just use faqQuestions.getFaqAnswers().size()nin your controller and you will get the size if lazily intialised list, without fetching the list itself.
只需在您的控制器中使用faqQuestions.getFaqAnswers()。size(),如果懒惰的初始化列表,您将获得大小,而无需获取列表本身。