Spring启动 - Hibernate JPA没有创建MySql DB表

时间:2017-03-20 15:51:49

标签: 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"
}

application.properties

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

User.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.");

        }

    }
}

提前致谢。

1 个答案:

答案 0 :(得分:0)

您正在为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创建的?他们在你期待的数据库中吗?