【发布时间】:2018-10-30 23:23:02
【问题描述】:
我有具有单向关系的 User 和 Post 实体。当我尝试从特定用户获取所有帖子时,我收到org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: 异常。
根据这些 SO answers,处理此问题的最佳方法是对服务方法/类使用 @Transactional 注释。放置注释对我不起作用。我正在使用 Wildfly 服务器、Hibernate、MySQL 和 Java EE MVC Web 框架。
如何让它发挥作用,即从用户那里获取帖子?我设法通过预先加载来做到这一点,但出于性能原因不建议这样做。
@Transactional
public class UserService {
private List<User> users;
private Set<Post> posts;
@PersistenceContext(unitName = "my-pu")
private EntityManager em;
public List<User> getUsers() {
String qlQuery = "SELECT u FROM User u";
Query query = em.createQuery(qlQuery);
users = query.getResultList();
return users;
}
@Transactional
public Set<Post> getUserPosts(Long userId) {
String qlQuery = "SELECT u FROM User u WHERE u.id = :userId";
Query query = em.createQuery(qlQuery);
query.setParameter("userId", userId);
User user = (User) query.getSingleResult();
posts = user.getPosts();
return posts;
}
}
这是我的服务方法。
@Path("users")
@Controller
public class UserController {
@Inject
private Models models;
@Inject
private UserService service;
@GET
@Produces("text/html")
@View("showUsers.ftl")
public void users() {
List<User> users = service.getUsers();
models.put("users", users);
}
@GET
@Path("{id}")
@Produces("text/html")
@View("showUserPosts.ftl")
public void getPosts(@PathParam("id") Long userId) {
System.out.println("here am i");
Set<Post> posts = service.getUserPosts(userId);
models.put("posts", posts);
}
}
这是我的控制器。
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="my-pu" transaction-type="JPA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/testdb?useSSL=false"/>
<property name="javax.persistence.jdbc.user" value="testuser"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.password" value="test623"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.dialect.storage_engine" value="innodb"/>
<property name="javax.persistence.schema-generation.database.action"
value="drop-and-create"/>
<property name="javax.persistence.sql-load-script-source"
value="META-INF/sql/data.sql" />
</properties>
</persistence-unit>
</persistence>
这是我的持久化单元。
错误信息:
org.jboss.resteasy.spi.UnhandledException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.zetcode.model.User.posts, could not initialize proxy - no Session
at org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:257)
at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:195)
at org.jboss.resteasy.core.SynchronousDispatcher.writeResponse(SynchronousDispatcher.java:539)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:461)
at org.jboss.resteasy.core.SynchronousDispatcher.lambda$invoke$4(SynchronousDispatcher.java:231)
at org.jboss.resteasy.core.SynchronousDispatcher.lambda$preprocess$0(SynchronousDispatcher.java:137)
at org.jboss.resteasy.core.interception.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:361)
at org.jboss.resteasy.core.SynchronousDispatcher.preprocess(SynchronousDispatcher.java:140)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:217)
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:227)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
at io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:67)
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
【问题讨论】:
-
为什么你的方法在查询帖子时却查询
User?加载User,然后获取它的帖子并抛弃用户真的很复杂。您应该改为使用SELECT p FROM Post p ...或SELECT u.posts FROM User u ...。 -
谢谢,注意到了。这对我来说更简单。
-
您发布的错误消息中
role:之后的内容是什么? -
这是
com.zetcode.model.User.posts, could not initialize proxy - no Session我已经附加了一个堆栈跟踪。
标签: java hibernate jakarta-ee orm wildfly