Я пытаюсь использовать загрузочное приложение Simple Spring для создания пользователей и сохранения их в базе данных с помощью Hibernate, но я получаю эту ошибку.
Причина: org.springframework.beans.factory.BeanCreationException: Ошибка создания бина с именем 'jpaMappingContext': сбой вызова метода init;Вложенное исключение - java.lang.IllegalArgumentException: должна присутствовать хотя бы одна метамодель JPA!на org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean (AbstractAutowireCapableBeanFactory.java:1628) в org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean (AbstractAutowireCapableBeanFactory.java:555) в org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean (AbstractAutowireCapableBeanFactory.java:483) в org.springframework.beans.factory.support.AbstractBeanFactory $ 1.get *bject * 100 * * 100 * включает в себя:* Pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springboot</groupId>
<artifactId>SpringBootHibernateInt</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Конфигурация источника данных:
package com.spring.security.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
@EnableTransactionManagement
public class DataSourceConfig {
@Autowired
Environment env;
@Bean
public DataSource getDataSource() {
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
ds.setUrl(env.getProperty("spring.datasource.url"));
ds.setUsername(env.getProperty("spring.datasource.username"));
ds.setPassword(env.getProperty("spring.datasource.password"));
return ds;
}
@Bean
public Properties getHibernateProperties() {
Properties properties = new Properties();
properties.put("spring.hibernate.dialect", env.getProperty("spring.hibernate.dialect"));
properties.put("spring.hibernate.show-sql", env.getProperty("spring.hibernate.show-sql"));
properties.put("spring.hibernate.format-sql", env.getProperty("spring.hibernate.format-sql"));
properties.put("spring.hibernate.ddl-auto", env.getProperty("spring.hibernate.ddl-auto"));
return properties;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager() {
HibernateTransactionManager htm = new HibernateTransactionManager();
htm.setSessionFactory(getSessionFactory().getObject());
return htm;
}
@Bean
public LocalSessionFactoryBean getSessionFactory() {
LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean();
lsfb.setDataSource(getDataSource());
lsfb.setHibernateProperties(getHibernateProperties());
// lsfb.setAnnotatedClasses(User.class);
lsfb.setPackagesToScan(env.getProperty("entitymanager.packagesToScan"));
return lsfb;
}
}
User.java
package com.spring.security.model;
import javax.persistence.*;
@Entity
@Table(name = "User")
public class User {
public User(String name) {
this.name = name;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
UserDAOImpl.java
package com.spring.security.dao;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.spring.security.model.User;
@Repository
@Transactional
public class UserDAOImpl {
@Autowired
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public User findUser(String userName) {
return (User) sessionFactory.getCurrentSession()
.createQuery("from User where name = " + userName);
}
public void createUser(User user) {
sessionFactory.getCurrentSession()
.save(user);
}
}
Контроллер
package com.spring.security.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.spring.security.dao.UserDAOImpl;
import com.spring.security.model.User;
@Controller
public class HomeController {
@Autowired
private UserDAOImpl userService;
@RequestMapping(value = "/save")
@ResponseBody
public String create(String name) {
try {
User user = new User(name);
userService.createUser(user);
} catch (Exception ex) {
return ex.getMessage();
}
return "User succesfully saved!";
}
}
Run.java
package com.spring.security;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Run {
public static void main(String[] args) {
SpringApplication.run(Run.class, args);
}
}
Application.properties
################### JDBC Configuration ###############################
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3307/***
spring.datasource.username=***
spring.datasource.password=***
################### Hibernate Configuration ##########################
spring.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.hibernate.show-sql=true
spring.hibernate.format-sql=true
spring.hibernate.ddl-auto=create
entitymanager.packagesToScan = com