1

I am new at spring Mvc and im trying to create database table through hibernate. i've done it before but now the table is not creating and i cant figure out the mistake.

MY MODEL CLASS:

@Entity
public class User implements Serializable, UserGetters{
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long id;

    @Column(unique=true)
    private String logonEmail;

    private String name;

    @Column(updatable=false)
    private String password;

    @JsonIgnore
    @Column(nullable = false, columnDefinition = "TINYINT(1)")
    private boolean isEmailVerified;

    // getters and setters

}

Application.properties:

        spring.datasource.url=jdbc:mysql://localhost:3306/basicdb?createDatabaseIfNotExist=true
        spring.datasource.username=root
        spring.datasource.password=ace123
        spring.data.rest.basePath=/api


        spring.datasource.driver-class-name=com.mysql.jdbc.Driver
        spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
        spring.jpa.hibernate.ddl-auto=create
        allowed.origins=http://localhost:9000
        logging.level.org.springframework.security=DEBUG
        logging.level.org.hibernate.SQL=DEBUG
        logging.level.com.reservos=DEBUG
        logging.level.com.sendgrid=DEBUG
        logging.level.org.hibernate.tool.hbm2ddl=debug
        server.port=3000

pom.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>

        <groupId>org.springframework</groupId>
        <artifactId>gs-spring-boot</artifactId>
        <version>0.1.0</version>

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.3.RELEASE</version>
        </parent>

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-rest-webmvc</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-rest</artifactId>
            </dependency>

            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>

             <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>

            <!-- tag::actuator[] -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <!-- end::actuator[] -->
            <!-- tag::tests[] -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <!-- end::tests[] -->
        </dependencies>

        <properties>
            <java.version>1.8</java.version>
        </properties>

        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
                <plugin>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>integration-test</goal>
                                <goal>verify</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>

    </project>

When I am running the code, mysql workbench creates the database but the User table is not created in it. I'll be glad if anyone can help me out

Mike Lischke
  • 48,925
  • 16
  • 119
  • 181
T.azeez
  • 13
  • 6

2 Answers2

0

There are several possible causes:

Your entity classes are in the same or in a sub-package relative one where you have you class with @EnableAutoConfiguration. If not then your spring app does not see them and hence will not create anything in db

Check your config

spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=test
spring.datasource.password=

Your application.properties must be in src/main/resources folder.

If you did not specify dialect correctly it might try to default to bundled together with boot in-memory database I could see that it tries to connect to local HSQL (see console output) instance and fail at updating the schema.

Rahul Singh
  • 19,030
  • 11
  • 64
  • 86
0

try removing :

columnDefinition = "TINYINT(1)"
aled evans
  • 21
  • 2