3

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>
4
  • it seems right ..what error you get? is table created in mysql? Commented Dec 8, 2017 at 13:46
  • when i check the database, i can't find the table castomer Commented Dec 8, 2017 at 13:47
  • go through it:spring.io/guides/gs/accessing-data-mysql Commented Dec 8, 2017 at 13:51
  • hi do you have sample code github Commented Mar 23, 2020 at 3:40

3 Answers 3

9

Try to add @EntityScan

@EntityScan("<package with entities>")
@SpringBootApplication 
public class Application { ... }
Sign up to request clarification or add additional context in comments.

6 Comments

where in customer.java?
No. Near @SpringBootApplication public class Application
Yesss it works finally, I added @EntityScan("<package with entities>") in application.java. Thank you nikolay and all the others for you help.
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.
@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.
|
1

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{}

3 Comments

yes i have interface extends jpaRepository. the springapp exists with the same username and password. i did: use springapp; show tables:empty set
The same thing, it's really wreid, !
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.
0

execute following on your mysql:

mysql> create database db_example; -- Create the new database

mysql> create user 'springuser'@'localhost' identified by 'ThePassword'; -- Creates the user

mysql> grant all on db_example.* to 'springuser'@'localhost'; -- Gives all the privileges to the new user on the newly created database

and add into application.xml:

spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword

OR

you can go through this link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.