首页 > 解决方案 > Spring:GCP 和 JPA - 缺少 EntityManagerFactory

问题描述

我有一个使用 Tomcat 的 Spring Boot 的工作实现,可以在 Linux 环境中的 JAR 文件中很好地处理。相同的代码无法与 Google Cloud 一起使用。我正试图让 JPA 工作。

Google Cloud 更喜欢 Jetty 而不是 Tomcat。这可能与我的问题有关,但我不确定如何解决。我有默认推荐的从标准 Spring Boot JAR 到 Google Cloud AppEngine 的 WAR 版本的转换。如果我在 spring.io 站点的预设项目中尝试它们,我的依赖项工作正常。我没有使用数据库,所以我删除了默认的 MySQL。

我的问题与缺少的EntityManagerFactory有关。由于我试图保持这篇文章简短,我只包括我认为至关重要的内容。您可以在以下位置找到完整项目的基本要素: https ://github.com/shatterblast/spring-sylveria_fix-search

基本上,我无法为这个 GCP 项目打开管理控制台。项目和管理控制台都从请求中返回 503。启动过程中没有错误消息,并且执行正常。通过调整代码,我发现EntityManager bean 本身就是问题所在。重新命名 bean 并没有帮助。

谢谢你。

import com.weslange.springboot.sylveria.entity.Gamer;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;

@Repository
public class GamerDAOHibernateImpl implements GamerDAO {

    private EntityManager entityManager;

    @Autowired
    public GamerDAOHibernateImpl( EntityManager theEntityManager ) {
        entityManager = theEntityManager;
    }

    @Override
    public Gamer findById( int id ) {
        Session currentSession = entityManager.unwrap( Session.class );
        Gamer gamer = currentSession.get( Gamer.class, id );
        return gamer;
    }

    @Override
    public void save( Gamer gamer ) {
        Session currentSession = entityManager.unwrap( Session.class );
        currentSession.saveOrUpdate( gamer );
    }

}

.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;

@Repository
public class TokenDAOHibernateImpl implements TokenDAO {

    private EntityManager entityManager;

    @Autowired
    public TokenDAOHibernateImpl(EntityManager theEntityManager) {
        entityManager = theEntityManager;
    }

}

.

import com.weslange.springboot.sylveria.SylveriaServerApplication;
import com.weslange.springboot.sylveria.entity.Token;
import com.weslange.springboot.sylveria.service.GamerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping( "/" )
public class TokenRestController {

    private static final Logger logger = LoggerFactory.getLogger( SylveriaServerApplication.class );

    private GamerService gamerService;

    @Autowired
    public TokenRestController( GamerService gamerService ) {
        gamerService = gamerService;
    }

    @PostMapping( "/" )
    public HashMap addEmployee( @RequestBody Token token, HttpServletRequest request ) {

        HashMap<String, String> returnMap = new HashMap();
        returnMap.put( "sylveriaConnectionSuccess", "true" );

        return returnMap;

    }

}

标签: javaspring-bootrestjpagoogle-cloud-platform

解决方案


推荐阅读