Spring Boot - Bean命名为entityManagerFactory

时间:2017-10-20 20:24:49

标签: postgresql hibernate rest spring-boot jhipster

我正在尝试使用jHipster生成一个简单的Spring Boot应用程序来从postgresql获取postreSQL数据库中的文章列表并使用其他控制器显示它,但是当我运行它时我得到了

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in org.adi.security.DomainUserDetailsService required a bean named 'entityManagerFactory' that could not be found.


Action:

Consider defining a bean named 'entityManagerFactory' in your configuration.

但问题是`DomainUserDetailsS​​ervice是jhipster生成的东西,在我添加了我的类后停止了工作。所以我会写下我的课程:

文章实体:

package org.adi.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="articles")
public class Article implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="article_id")
    private int articleId;
    @Column(name="title")
    private String title;
    @Column(name="category")
    private String category;
    public int getArticleId() {
        return articleId;
    }
    public void setArticleId(int articleId) {
        this.articleId = articleId;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getCategory() {
        return category;
    }
    public void setCategory(String category) {
        this.category = category;
    }
}

ArticleDAO(存储库):

@Transactional
@Repository
public class ArticleDAO implements IArticleDAO {
    @PersistenceContext
    private EntityManager entityManager;
    @Override
    public Article getArticleById(int articleId) {
        return entityManager.find(Article.class, articleId);
    }
    @SuppressWarnings("unchecked")
    @Override
    public List<Article> getAllArticles() {
        String hql = "FROM Article as atcl ORDER BY atcl.articleId DESC";
        return (List<Article>) entityManager.createQuery(hql).getResultList();
    }
    @Override
    public void createArticle(Article article) {
        entityManager.persist(article);
    }
    @Override
    public void updateArticle(Article article) {
        Article artcl = getArticleById(article.getArticleId());
        artcl.setTitle(article.getTitle());
        artcl.setCategory(article.getCategory());
        entityManager.flush();
    }
    @Override
    public void deleteArticle(int articleId) {
        entityManager.remove(getArticleById(articleId));
    }
    @Override
    public boolean articleExists(String title, String category) {
        String hql = "FROM Article as atcl WHERE atcl.title = ? and atcl.category = ?";
        int count = entityManager.createQuery(hql).setParameter(1, title)
            .setParameter(2, category).getResultList().size();
        return count > 0 ? true : false;
    }
}

ArticleService:

@Service
public class ArticleService implements IArticleService {
    @Autowired
    private IArticleDAO articleDAO;
    @Override
    public Article getArticleById(int articleId) {
        Article obj = articleDAO.getArticleById(articleId);
        return obj;
    }
    @Override
    public List<Article> getAllArticles(){
        return articleDAO.getAllArticles();
    }
    @Override
    public synchronized boolean createArticle(Article article){
        if (articleDAO.articleExists(article.getTitle(), article.getCategory())) {
            return false;
        } else {
            articleDAO.createArticle(article);
            return true;
        }
    }
    @Override
    public void updateArticle(Article article) {
        articleDAO.updateArticle(article);
    }
    @Override
    public void deleteArticle(int articleId) {
        articleDAO.deleteArticle(articleId);
    }
}

最后我的REST控制器:

@Controller
@RequestMapping("user")
@CrossOrigin(origins = {"http://localhost:4200"})
public class ArticleController {
    @Autowired
    private IArticleService articleService;
    @GetMapping("article")
    public ResponseEntity<Article> getArticleById(@RequestParam("id") String id) {
        Article article = articleService.getArticleById(Integer.parseInt(id));
        return new ResponseEntity<Article>(article, HttpStatus.OK);
    }
    @GetMapping("all-articles")
    public ResponseEntity<List<Article>> getAllArticles() {
        List<Article> list = articleService.getAllArticles();
        return new ResponseEntity<List<Article>>(list, HttpStatus.OK);
    }
    @PostMapping("article")
    public ResponseEntity<Void> createArticle(@RequestBody Article article, UriComponentsBuilder builder) {
        boolean flag = articleService.createArticle(article);
        if (flag == false) {
            return new ResponseEntity<Void>(HttpStatus.CONFLICT);
        }
        HttpHeaders headers = new HttpHeaders();
        headers.setLocation(builder.path("/article?id={id}").buildAndExpand(article.getArticleId()).toUri());
        return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
    }
    @PutMapping("article")
    public ResponseEntity<Article> updateArticle(@RequestBody Article article) {
        articleService.updateArticle(article);
        return new ResponseEntity<Article>(article, HttpStatus.OK);
    }
    @DeleteMapping("article")
    public ResponseEntity<Void> deleteArticle(@RequestParam("id") String id) {
        articleService.deleteArticle(Integer.parseInt(id));
        return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
    }
}

1 个答案:

答案 0 :(得分:0)

您是否创建了持久性单元?

[参考文献] =&GT; https://docs.oracle.com/cd/E19798-01/821-1841/bnbrj/index.html

一旦你已经定义了持久性单元的标签,你就可以像这样创建你的实体管理器:

private final String PERSISTENCE_UNIT_NAME = "PUName";
private EntityManagerFactory eMFactory =  Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager entityManager=eMFactory.createEntityManager();