即使使用@Transactional

时间:2019-02-17 19:04:18

标签: java spring hibernate jpa kotlin

当我要获取用户的评论时,很难通过ManyToOne实体映射一个简单的OneToMany。其他答案表明您必须使用entityManager自己创建查询,但这看起来太可怕了。如果没有硬编码内联sql甚至不能做这样简单的事情,那么ORM的意义何在?似乎我做错了什么。

似乎可能与我正在使用模型从jsp访问user.getComments()方法有关。不确定执行此操作的最佳方法是什么。

模式:

CREATE TABLE users (
    id INTEGER AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(255) UNIQUE NOT NULL,
    created_at TIMESTAMP DEFAULT NOW()
);

CREATE TABLE comments (
    id INTEGER AUTO_INCREMENT PRIMARY KEY,
    comment_text VARCHAR(255) NOT NULL,
    photo_id INTEGER NOT NULL,
    user_id INTEGER NOT NULL,
    created_at TIMESTAMP DEFAULT NOW(),
    FOREIGN KEY(user_id) REFERENCES users(id)
);

用户控制器方法:

@RequestMapping("/user")
public ModelAndView getUser(@RequestParam int id) {
    return new ModelAndView("user", "message", userService.getUser(id));
}

UserService:

@Service
public class UserService {

    private UserDAO userDAO;

    @Autowired
    public UserService(UserDAO userDAO) {
        this.userDAO = userDAO;
    }

    @Transactional
    public User getUser(int id) {
        return userDAO.getUser(id);
    }

}

UserDAO:

@Repository
public class UserDAO {

    @PersistenceContext
    EntityManager entityManager;

    @Nullable
    public User getUser(int id)
    {
        return entityManager.find(User.class, id);
    }
}

用户实体:

@Entity
@Table(name="users")
public class User {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private int id;

    @Column(name="username")
    private String userName;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="created_at")
    private Date createdAt;

    @OneToMany(mappedBy="user")
    private List<Comment> comments;

    public int getId() {
        return id;
    }

    public String getUserName() {
        return userName;
    }

    public Date getCreatedAt() {
        return createdAt;
    }

    @Transactional
    public List<Comment> getComments() {
        return comments;
    }
}

评论实体:

@Entity
@Table(name="comments")
public class Comment {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private int id;

    @Column(name="comment_text")
    private String commentText;

    @Column(name="photo_id")
    private int photoId;

    @ManyToOne
    @JoinColumn(name="user_id")
    private User user;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created_at")
    private Date createdAt;

    @Override
    public String toString() {
        return "Comment [id=" + id + ", commentText=" + commentText + ", photoId=" + photoId + ", userId=" + user.getId()
                + ", createdAt=" + createdAt + "]";
    }

    public int getId() {
        return id;
    }

    public String getCommentText() {
        return commentText;
    }

    public int getPhotoId() {
        return photoId;
    }

    public User getUser() {
        return user;
    }

    public Date getCreatedAt() {
        return createdAt;
    }
}

Stacktrace:

  

严重:Servlet [dispatcher]中的Servlet.service()   路径[/ test]引发了异常[正在处理异常   [WEB-INF / jsp / user.jsp]在第[34]行

     

31:用户32:创建日期33:34:          35:   36:37:

     

Stacktrace:]具有根本原因   org.hibernate.LazyInitializationException:无法延迟初始化   角色集合:com.instagramviewer.entity.User.comments,可以   未初始化代理-没有会话   org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:602)     在   org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:217)     在   org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:581)     在   org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:148)     在   org.hibernate.collection.internal.PersistentBag.iterator(PersistentBag.java:303)     在   org.apache.taglibs.standard.tag.common.core.ForEachSupport.toForEachIterator(ForEachSupport.java:348)     在   org.apache.taglibs.standard.tag.common.core.ForEachSupport.supportedTypeForEachIterator(ForEachSupport.java:224)     在   org.apache.taglibs.standard.tag.common.core.ForEachSupport.prepare(ForEachSupport.java:155)     在   javax.servlet.jsp.jstl.core.LoopTagSupport.doStartTag(LoopTagSupport.java:256)     在   org.apache.jsp.WEB_002dINF.jsp.user_jsp._jspx_meth_c_005fforEach_005f0(user_jsp.java:285)     在   org.apache.jsp.WEB_002dINF.jsp.user_jsp._jspService(user_jsp.java:172)     在org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:741)处   org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)     在   org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)     在org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:741)处   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)     在   org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)     在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)     在   org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:712)     在   org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:459)     在   org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:384)     在   org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:312)     在   org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:170)     在   org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:316)     在   org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1370)     在   org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1116)     在   org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1055)     在   org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)     在   org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)     在   org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:634)在   org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:741)处   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)     在   org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)     在   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)     在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200)     在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)     在   org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)     在   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)     在   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)     在   org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)     在   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)     在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)     在   org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)     在   org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)     在   org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:834)     在   org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1415)     在   org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)     在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)     在   java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)     在   org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)     在java.lang.Thread.run(Thread.java:748)

4 个答案:

答案 0 :(得分:2)

@Vlad已经注意到,您正在交易之外引用Comment。由于您使用的是jpql,因此可以急切地将User和他的评论一起提取:

public List<User> getAllUsers()
{
    TypedQuery<User> query = entityManager.createQuery("SELECT e from User e left join fetch e.comments", User.class);
    return (List<User>) query.getResultList();
}

答案 1 :(得分:1)

@Transactional对您无济于事,因为当您尝试加载注释(在JSP级别)时,事务已关闭(它在getUser方法开始时打开,并在结束时关闭)。 可能的方法是在交易中初始化您的评论。

@Transactional
public User getUser(int id) {
    User user = userDAO.getUser(id);
    user.getComments().size(); //initializing
    return user;
}

这是最原始的方法,但是in应该可以为您工作。 如果可行,您稍后可以阅读有关Named Entity Graph的信息。这是初始化所需的惰性关联的更高级的方法。

答案 2 :(得分:1)

我做错了两件事:

首先:我忘记在spring java config类中启用事务管理

@Configuration
@EnableTransactionManagement
public class AppConfig {

  @Bean
  public LocalContainerEntityManagerFactoryBean factoryBean() {
      LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
      factory.setPersistenceProviderClass(HibernatePersistenceProvider.class);

      return factory;
  }

  @Bean
  public PlatformTransactionManager transactionManager(){
     JpaTransactionManager transactionManager = new JpaTransactionManager();
     transactionManager.setEntityManagerFactory(factoryBean().getObject() );
     return transactionManager;
  }
}

第二:正如弗拉德所说,我需要在服务层中将注释列表初始化为事务的一部分

@Transactional
public User getUser(int id) {
    User user = userDAO.getUser(id);
    Hibernate.initialize(user.getComments());
    return user;
}

现在,我可以避免使用join fetch解决方案,这只会在我想检索注释列表的服务层中的任何地方添加一行Java代码。

答案 3 :(得分:0)

我试图从一开始就避免这样做,但是我想让实体的访存类型保持懒惰的最佳方法是在DAO层中创建一个方法,该方法急切地使用join fetch进行访存。

@Nullable
    public User getUser(int id)
    {
        TypedQuery<User> query = entityManager.createQuery("SELECT e from User e left join fetch e.comments c where e.id = :id", User.class);
        query.setParameter("id", id);
        return query.getSingleResult();
    }