当前位置: 首页 > 知识库问答 >
问题:

Spring boot/hibernate/mysql无法创建EntityManagerFactory

赵钊
2023-03-14

我是春靴新来的,我需要你的帮助。我试图配置我的MySQL DB,JPA(Hibernate),但我遇到了以下异常:

创建类路径资源中定义的名为“实体ManagerFactory”的bean时出错。....

CREATE TABLE IF NOT EXISTS users
(
  id         INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,
  login      VARCHAR(45) NOT NULL,
  password   VARCHAR(45) NOT NULL,
  full_name VARCHAR(100) NOT NULL
);
CREATE UNIQUE INDEX users_unique_login_idx ON users (login);

CREATE TABLE IF NOT EXISTS user_roles
(
  user_id INTEGER NOT NULL,
  role    VARCHAR(45),
  CONSTRAINT user_roles_idx UNIQUE (user_id, role),
  FOREIGN KEY (user_id) REFERENCES users (id)
);

CREATE TABLE IF NOT EXISTS contacts (
  id          INTEGER PRIMARY KEY NOT NULL AUTO_INCREMENT,
  user_id     INTEGER NOT NULL,
  first_name VARCHAR(45) NOT NULL,
  last_name VARCHAR(45) NOT NULL,
  patronymic VARCHAR(45) NOT NULL,
  mobile_phone_number VARCHAR(15),
  home_phone_number VARCHAR(15),
  address VARCHAR(45),
  email VARCHAR(30),
  FOREIGN KEY (user_id) REFERENCES users (id)
);
CREATE UNIQUE INDEX contacts_unique_idx ON contacts (user_id);
#You can use MySQL DB with next properties
spring.jpa.database=MYSQL
spring.datasource.url=jdbc:mysql://localhost:3306/lardi
spring.datasource.username=root
spring.datasource.password=2940063
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
use.SSL=false
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>

        <!-- properties -->
        <property name="connection.url">jdbc:mysql://localhost:3306/lardi</property>
        <property name="connection.username">root</property>
        <property name="connection.password">2940063</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>


        <!-- mapping files -->
        <mapping class="com.model.BaseEntity"/>
        <mapping class="com.model.NamedEntity"/>
        <mapping class="com.model.Contact"/>
        <mapping class="com.model.User"/>
        <mapping class="com.model.Role"/>

    </session-factory>

</hibernate-configuration>
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class SpringBootWebApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootWebApplication.class, args);
    }
}
@Configuration
@EnableTransactionManagement
public class JPAConfig {

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());
        em.setPackagesToScan("com.model");

        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        em.setJpaProperties(additionalProperties());

        return em;
    }

    @Bean
    public DataSource dataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/lardi");
        dataSource.setUsername( "root" );
        dataSource.setPassword( "2940063" );
        return dataSource;
    }

    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);

        return transactionManager;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
        return new PersistenceExceptionTranslationPostProcessor();
    }

    Properties additionalProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.hbm2ddl.auto", "validate");
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        return properties;
    }
}
 @Entity
    @Table(name = "contacts", uniqueConstraints = {@UniqueConstraint(columnNames = {"user_id"}, name = "unique_idx")})
    public class Contact extends BaseEntity {
    
        @Column(name = "first_name")
        private String firstName;

        @Column(name = "last_name")
        private String lastName;

        @Column(name = "patronymic")
        private String patronymic;

        @Column(name = "mobile_phone_number")
        private String mobilePhone;

        @Column(name = "home_phone_number")
        private String homePhone;

        @Column(name = "address")
        private String address;

        @Column(name = "email")
        private String email;
    
        public Contact() {
        }
    
        public Contact(String firstName, String lastName, String patronymic, String mobilePhone, String homePhone, String address, String email ) {
            this(null,firstName,lastName,patronymic,mobilePhone,homePhone,address,email);
        }
    
        public Contact( Integer id, String firstName, String lastName, String patronymic, String mobilePhone, String homePhone, String address, String email ) {
            super(id);
            this.firstName = firstName;
            this.lastName = lastName;
            this.patronymic = patronymic;
            this.mobilePhone = mobilePhone;
            this.homePhone = homePhone;
            this.address = address;
            this.email = email;
        }
    
        public String getFirstName() {
            return firstName;
        }
    
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    
        public String getLastName() {
            return lastName;
        }
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    
        public String getPatronymic() {
            return patronymic;
        }
    
        public void setPatronymic(String patronymic) {
            this.patronymic = patronymic;
        }
    
        public String getMobilePhone() {
            return mobilePhone;
        }
    
        p

ublic void setMobilePhone(String mobilePhone) {
        this.mobilePhone = mobilePhone;
    }

    public String getHomePhone() {
        return homePhone;
    }

    public void setHomePhone(String homePhone) {
        this.homePhone = homePhone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "Contact{" +
                "firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", patronymic='" + patronymic + '\'' +
                ", mobilePhone='" + mobilePhone + '\'' +
                ", homePhone='" + homePhone + '\'' +
                ", address='" + address + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}



@Entity
@Table(name = "users", uniqueConstraints = {@UniqueConstraint(columnNames = "login", name = "users_unique_login_idx")})
public class User extends NamedEntity{

    @Column(name = "password", nullable = false)
    private String password;
    @Column(name = "full_name", nullable = false)
    private String fullName;

    @Enumerated(EnumType.STRING)
    @CollectionTable(name = "user_roles", joinColumns = @JoinColumn(name = "user_id"))
    @Column(name = "role")
    @ElementCollection(fetch = FetchType.LAZY)
    protected Set<Role> roles;

    public User() {
    }

    public User(User u) {
        this(u.getId(), u.getLogin(), u.getPassword(), u. getFullName(), u.getRoles());
    }

    public User(Integer id, String login, String password, String fullName, Role role, Role... roles) {
        this(id, login, password, fullName, EnumSet.of(role, roles));
    }

    public User(Integer id, String login, String password, String fullName, Set<Role> roles) {
        super(id, login);
        this.password = password;
        this.fullName = fullName;
        setRoles(roles);
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = CollectionUtils.isEmpty(roles) ? Collections.emptySet() : EnumSet.copyOf(roles);
    }

    @Override
    public String toString() {
        return "User{" +
                "password='" + password + '\'' +
                ", fullName='" + fullName + '\'' +
                ", roles=" + roles +
                '}';
    }
}

@MappedSuperclass
@Access(AccessType.FIELD)
public class BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    protected Integer id;

    public BaseEntity() {
    }

    public BaseEntity(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
    public boolean isNew() {
        return (this.id == null);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        BaseEntity that = (BaseEntity) o;
        if (id == null || that.id == null) {
            return false;
        }
        return id.equals(that.id);
    }

    @Override
    public int hashCode() {
        return (id == null) ? 0 : id;
    }
}

@MappedSuperclass
public class NamedEntity extends BaseEntity {

    @NotEmpty
    @Column(name = "login", nullable = false)
    protected String login;

    public NamedEntity() {
    }

    protected NamedEntity(Integer id, String login) {
        super(id);
        this.login = login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getLogin() {
        return this.login;
    }

    @Override
    public String toString() {
        return login;
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<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</groupId>
    <artifactId>lardi</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>Phone Book</name>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <start-class>com.SpringBootWebApplication</start-class>

        <!-- JSON -->
        <jackson-json.version>2.8.0</jackson-json.version>

        <!-- WEB jars -->
        <webjars-bootstrap.version>3.3.6</webjars-bootstrap.version>
        <webjars-jquery.version>2.2.4</webjars-jquery.version>
        <webjars-noty.version>2.3.8</webjars-noty.version>
        <webjars-datatables.version>1.10.12</webjars-datatables.version>
    </properties>

    <build>
        <plugins>
            <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    </parent>

    <dependencies>
        <!-- Spring Boot WEB -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- Spring Boot TEST -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Spring Boot Security -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <!-- Spring Boot JPA -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
         </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
        </dependency>
        <!-- MySQL -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- JSON -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson-json.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-hibernate5</artifactId>
            <version>${jackson-json.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
            <version>${jackson-json.version}</version>
        </dependency>
        <!-- Webjars -->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>${webjars-bootstrap.version}</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>${webjars-jquery.version}</version>
        </dependency>
        <dependency>
            <groupId>org.webjars.bower</groupId>
            <artifactId>noty</artifactId>
            <version>${webjars-noty.version}</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>datatables</artifactId>
            <version>${webjars-datatables.version}</version>
        </dependency>
    </dependencies>
</project>

共有1个答案

夏振国
2023-03-14

联系人实体没有引用用户对象的字段user。因此hibernate映射中没有user_id列。Hibernate不检查数据库中的列。

 类似资料:
  • 我在创建sessionFactory对象时遇到问题。我将Hibernate与Maven和MySQL一起使用。怎么修? 日志错误: xml(在src/main/resources中): users.hbm.xml(在src/main/users.hbm.xml中):

  • 我正在使用Hibernate和spring boot。我注意到,如果我从包中包含注释和 并尝试运行spring启动应用程序。我的应用程序启动失败,出现以下错误。 当我删除这些注释并尝试运行应用程序时。应用程序启动良好。 审计实体。JAVA 波姆。xml 我将审计实体嵌入到userentity中。 下面是userEntity的代码。JAVA 我相信这是因为我引入了一些与hibernate相关的类,这

  • 运行时收到此错误。

  • 我刚开始使用hibernate,总是会遇到这样的错误: 错误:HHH000388:不成功:创建表用户(id bigint not null auto_increment,mail varchar(255),passwort varchar(255),primary key(id))错误:从存储引擎获取错误-1 这是我的映射类:

  • 下面是我从Internet上的大多数教程中复制的pom.xml。 然后,下面是我放置在属性文件中的配置: 我会犯什么错?我得到以下错误: 2016-09-25 22:58:18.297信息8452---[main]org.Hibernate.version:hhh000412:Hibernate核心{5.0.11.final}2016-09-25 22:58:18.299信息8452---[ ma

  • Hibernate:/*select generatedAlias0 from User为generatedAlias0*/select user0_.ID为id1_0_,user0_.CREATEDATE为createda2_0_,user0_.Email为email3_0_,user0_.PASSWORD为password4_0_,user0_.Security_LEVEL为security5