Can't create table with jpa+spring boot

15,955

Solution 1

Try to add @EntityScan

@EntityScan("<package with entities>")
@SpringBootApplication 
public class Application { ... }

Solution 2

With the current information I have a couple of suggestions / Questions that might help you allong:

  • Does the "springapp" database exist with the correct user (root) and password (me) assigned to it in MySQL
  • Has MySQL been started?
  • I see no @Repository definition in your code e.g.

    @Repository public interface CustomerRepository extends JpaRepository{}

Share:
15,955

Related videos on Youtube

Daniel
Author by

Daniel

Updated on June 04, 2022

Comments

  • Daniel
    Daniel almost 2 years

    I'm trying to generate table using JPA. but i can't create it. There is no error in the code, but it seems that there is configuration error. but i can't find it, i tried many configurations but nothing happen. Thanks a lot.

    This is application.property:

    spring.datasource.url=jdbc:mysql://localhost:3306/springapp
    spring.datasource.username= root
    spring.datasource.password= me
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.jpa.hibernate.ddl-auto=create
    

    this is my class:

    @Entity
    public class Customer {
    
        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        private int id;
        private String name;
        private String phone;
    
        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;
        }
        public String getPhone() {
            return phone;
        }
        public void setPhone(String phone) {
            this.phone = phone;
        }
        public Customer(String name, String phone) {
            super();
            this.name = name;
            this.phone = phone;
        }
        public Customer() {
            super();
        }
    }
    

    This is the application:

    @SpringBootApplication
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    

    And this is the controller:

    @RestController @RequestMapping("/customer") public class CustomerController { @Autowired CustomerRepository customerRepository;

    @RequestMapping("/findall")
    @ResponseBody
    public List<Customer> findAll() {
        return customerRepository.findAll();
    }
    

    }

    Pom.xml

        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.example</groupId>
        <artifactId>GStock-3</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>GStock-3</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.9.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <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>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>
    
    • Amol Raje
      Amol Raje over 6 years
      it seems right ..what error you get? is table created in mysql?
    • Daniel
      Daniel over 6 years
      when i check the database, i can't find the table castomer
    • Amol Raje
      Amol Raje over 6 years
    • Lijo
      Lijo about 4 years
      hi do you have sample code github
  • Daniel
    Daniel over 6 years
    yes i have interface extends jpaRepository. the springapp exists with the same username and password. i did: use springapp; show tables:empty set
  • Daniel
    Daniel over 6 years
    The same thing, it's really wreid, !
  • Daniel
    Daniel over 6 years
    Note: I was working with normal spring, with the same username and password, and i used jpa anad i generated tables normally. But now, i moved to spring boot it don't work, i think it's configuration problem.
  • Daniel
    Daniel over 6 years
    where in customer.java?
  • Nikolai  Shevchenko
    Nikolai Shevchenko over 6 years
    No. Near @SpringBootApplication public class Application
  • Daniel
    Daniel over 6 years
    Yesss it works finally, I added @EntityScan("<package with entities>") in application.java. Thank you nikolay and all the others for you help.
  • pixel
    pixel over 2 years
    Thanks @NikolaiShevchenko but with or witout @EntityScan, I get error "com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-551, SQLSTATE=42501, SQLERRMC=MY_USER;CREATE TABLE;DSNDB04, DRIVER=4.29.24" I use Spring Data JPA and by data source is DB2 database. Any idea why I am getting this error? It seem that user MY_USER does not have priviledge to issue CREATE TABLE command; however, I dont know what are priviledges required by Hibernate / JPA.
  • Nikolai  Shevchenko
    Nikolai Shevchenko over 2 years
    @pixel user on whose behalf SQL commands are run might require some set of privileges, but JPA (including Hibernate engine) itself doesn't require any privilieges.
  • pixel
    pixel over 2 years
    Thanks @NikolaiShevchenko. I figured out what the problem was. For DB2, I need to provide schema to my entity's @Table annotation like @Entity @Immutable @Table(name = "CAR_VIEW", schema = "MY_SCHEMA") public class CarEntity implements Serializable { .... With that, the problem with DB2 is gone but now my H2 stopped working so need to figure out how to use entity with schema with H2 database?