2
votes

I am trying to update the data base schema from defiend JpaEntities but spring boot is not able to generate the database tables. Log file:

application.properties file:

spring.datasource.url= jdbc:mysql://localhost:3306/banque_db
spring.datasource.username= root
spring.datasource.password=
spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto= update
spring.jpa.show-sql= true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

client entity class:

 @Entity(name="client")
public class Client implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long code;
private String name;
private String email;
@OneToMany(mappedBy = "compte")
private Collection<Compte> comptes;
// + getters and setters

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.0.RELEASE com.Tuto

<artifactId>MaBanque</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MaBanque</name>
<description>Demo project for Spring Boot</description>

<properties>
    <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-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>       
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </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>

3

3 Answers

3
votes

actually, the issue was the entity classes were not in the right package. org.example that contains the main class and org.entities for some reason spring boot did not generate the tables so when i moved the entity package to become like org.example.entities and run the app , all the tables where successfully generated. thank you guys for help I appriciate :)

1
votes

You have to set spring.jpa.hibernate.ddl-auto = create for db creation or create-drop

See docs for more details.

0
votes

First problem: The application.properties file:

  1. If you set attribute spring.jpa.hibernate.ddl-auto = update, it will not create the schema for you. Instead, it will, you know, update the existing database, which you currently does not have.
  2. The default value for spring.jpa.hibernate.ddl-auto is create-drop but you are overriding it.
  3. Your application.properties must be in

src/main/resources

folder.

==> you should fix to:

spring.jpa.hibernate.ddl-auto = create - drop

Second problem: The entity class. You should add @Column on top of the attributes, like so:

`@Entity(name="client")
public class Client implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long code;
@Column
private String name;
@Column
private String email;
@OneToMany(mappedBy = "compte")
private Collection<Compte> comptes;
// + getters and setters`