mysql - Spring boot - Hibernate JPA 不创建 MySql DB 表

标签 mysql hibernate jpa spring-boot

我知道这个问题可能是重复的,但这些解决方案都不适合我。

我希望自动创建表。但是,当我将带有 JSON 格式的 USER 对象的 POST 请求发送到服务器时,出现以下异常。

异常:

    {
  "timestamp": 1490023621440,
  "status": 500,
  "error": "Internal Server Error",
  "exception": "org.springframework.jdbc.BadSqlGrammarException",
  "message": "PreparedStatementCallback; bad SQL grammar [INSERT INTO user(lastName, firstName, dob, phone, email, password) VALUES (?, ?, ?, ?, ?, ?)]; nested exception is java.sql.SQLSyntaxErrorException: Table 'master_slave.user' doesn't exist",
  "path": "/main"
}

应用程序属性

spring.datasource.url=jdbc:mysql://${writerendpoint}:${port}/${database.name}
spring.datasource.username=root
spring.datasource.password=root
#spring.datasource.driverClassName=com.mysql.jdbc.driver

writerendpoint=localhost
port=3306
database.name=master_slave

spring.session.store-type=none

# ===============================
# = JPA / HIBERNATE
# ===============================

# Specify the DBMS
spring.jpa.database = MYSQL

# Show or not log for each sql query
spring.jpa.show-sql = true

# Ddl auto must be set to "create" to ensure that Hibernate will run the
# import.sql file at application startup
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto = create

# SQL dialect for genereting optimized queries
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

spring.jpa.properties.hibernate.default_schema=master_slave

用户.java

@Entity
@Table(name="user")
public class User {

    @Id
    @Column
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int userId;
    @NotEmpty
    @Column
    @Max(value=15)
    private String lastName;
    @NotEmpty
    @Column
    @Max(value=15)
    private String firstName;   
    @NotEmpty
    @Column
    private Date dob;
    @Column
    @Max(value=10)
    private String phone;
    @NotEmpty
    @Column
    @Email
    private String email;
    @NotEmpty
    @Column
    @Min(value=6)
    @Max(value=15)
    private String password;

and their getters and setters.

MainDaoImpl.java

@Repository
@Qualifier("mysql")
public class MainDaoImpl implements MainDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /* (non-Javadoc)
     * @see com.app.masterSlave.dao.RegistrationDao#userRegistration(com.app.masterSlave.model.User)
     */
    @Override
    public void userRegistration(User user) {
        final String sql="INSERT INTO user(lastName, firstName, dob, phone, email, password) VALUES (?, ?, ?, ?, ?, ?)";
        final String lastName=user.getLastName();
        final String firstName=user.getFirstName();
        final Date dob=user.getDob();
        final String phone = user.getPhone();
        final String email = user.getEmail();
        final String password = user.getPassword();
//      BCryptPasswordEncoder encodedPassword = new BCryptPasswordEncoder();            
//      final String password = encodedPassword.encode(user.getPassword());

        int success = jdbcTemplate.update(sql, new Object[] {lastName, firstName, dob, phone, email, password});

        if(success==0) {
            System.out.println("There was an error while insertion.");

        }
        else {
            System.out.println("Row with email: " + email + " inserted successfully.");

        }

    }
}

提前致谢。

最佳答案

您正在为 JPA 配置 hibernate,但不是通过 SQL 插入用户。

如果 JPA 和 JDBC 模板的配置不兼容,您将遇到问题,正如您发布的那样。为了避免这种情况,您可以考虑仅在 JPA 或仅在 JDBC 中执行所有数据库操作。

您正在使用的网址

spring.datasource.url=jdbc:mysql://${writerendpoint}:${port}/${database.name}

已包含数据库名称“master_slave”。此外,您还将 JPA 默认架构设置为相同的名称

spring.jpa.properties.hibernate.default_schema=master_slave

这可能只是多余的,或者是您问题的原因。删除它并尝试一下。

查看数据库。哪些表是由 hibernate 创建的?它们在您期望的数据库中吗?

关于mysql - Spring boot - Hibernate JPA 不创建 MySql DB 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42908150/

相关文章:

mysql - 如果字段值为 0 或 1,如何选择 sum?

java - 如何显示表中属性的属性 - Java

mysql - WHERE 子句过滤不到一小时的时间

java - 保存对象的 toString 值时出现 Stackoverflow 错误 - Java/Hibernate/Spring

java - Hibernate @OneToMany 映射给出异常,

java - mysql事务数据库逻辑可以放在哪一层?

java - 组装 jar 后 - 没有名为 EntityManager 的持久性提供程序

java - 使用 JPA 映射枚举

php - MySQL 日期格式 : Difference to Greenwich time format

Mysql查询成员(member)是否上过课