Question

i'm a android developer. but now, i need to create a webservice project. i try to create a spring project by STS. and i found some demo code from internet. but it doesn't work. when i run the application. console output error log:

  • error log:

     Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'test_spring.customer' doesn't exist
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
            at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
            at com.mysql.jdbc.Util.getInstance(Util.java:386)
            at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
            at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
            at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
            at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617)
            at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
            at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2825)
            at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2156)
            at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2459)
            at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2376)
            at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2360)
            at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:187)
            ... 48 more
    
  • Application.java

    public class Application {
    
        public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Application.class);
    
        CustomerRepository repository = context.getBean(CustomerRepository.class);
    
        // save a couple of customers
        repository.save(new Customer("Jack", "Bauer"));
        repository.save(new Customer("Chloe", "O'Brian"));
        repository.save(new Customer("Kim", "Bauer"));
        repository.save(new Customer("David", "Palmer"));
        repository.save(new Customer("Michelle", "Dessler"));
    
        // fetch all customers
        Iterable<Customer> customers = repository.findAll();
        System.out.println("Customers found with findAll():");
        System.out.println("-------------------------------");
        for (Customer customer : customers) {
            System.out.println(customer);
        }
        System.out.println();
    
        // fetch an individual customer by ID
        Customer customer = repository.findOne(1L);
        System.out.println("Customer found with findOne(1L):");
        System.out.println("--------------------------------");
        System.out.println(customer);
        System.out.println();
    
        // fetch customers by last name
        List<Customer> bauers = repository.findByLastName("Bauer");
        System.out.println("Customer found with findByLastName('Bauer'):");
        System.out.println("--------------------------------------------");
        for (Customer bauer : bauers) {
            System.out.println(bauer);
        }
    
              context.close();
        }
    }
    
  • Customer.java

        @Entity
        public class Customer {
    
            @Id
            @GeneratedValue(strategy=GenerationType.AUTO)
            private long id;
            private String firstName;
            private String lastName;
    
            protected Customer() {}
    
            public Customer(String firstName, String lastName) {
                this.firstName = firstName;
                this.lastName = lastName;
            }
    
            @Override
            public String toString() {
                return String.format(
                        "Customer[id=%d, firstName='%s', lastName='%s']",
                        id, firstName, lastName);
            }
    
        }
    
  • CustomerRespository.java

      public interface CustomerRepository extends CrudRepository<Customer, Long> {
    
           List<Customer> findByLastName(String lastName);
      }
    
  • application.properties

       spring.datasource.url=jdbc:mysql://localhost/test_spring
       spring.datasource.username=user
       spring.datasource.password=123456
       spring.datasource.driverClassName=com.mysql.jdbc.Driver
    
    
       spring.jpa.hibernate.ddl-auto=create-drop
       spring.jpa.database-platform=org.hibernate.dialect.SQLServerDialect
       spring.jpa.show-sql=true
    
  • pom.xml

     <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.27</version>
        </dependency>
    </dependencies>
    

=================================

why it doesn't auto create table.

please help me fix it or tell me how to create a webservice project, thank you very much. haha

Was it helpful?

Solution

I think you may just need to add the following to application.properties:

spring.jpa.generate-ddl=true

However, you have also configured it to use SQLServerDialect. That should be:

org.hibernate.dialect.MySQL5InnoDBDialect

or

org.hibernate.dialect.MySQLDialect
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top