春季:GCP和JPA-缺少EntityManagerFactory

时间:2020-03-21 01:40:22

标签: java spring-boot rest jpa google-cloud-platform

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

相对于Tomcat,Google Cloud更喜欢Jetty。这可能与我的问题有关,但我不确定如何解决。建议将Google Cloud的AppEngine从标准的Spring Boot JAR默认转换为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;

    }

}

0 个答案:

没有答案
相关问题