skip to Main Content

i am creating a new backend APIs for the online shop, I have created entities class for item, cart, user etc. However, when I am running the app the tables are not getting created in the DB.

I am not getting any errors in logs as well, so this is a bit confusing for me to understand what could be the issue.

below are the user class and application.properties

package com.onlineDairyShop.entities;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;

@Entity
@Table(name= "user")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    
    private String name;
    
    private int age;
    
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "address_id", referencedColumnName = "id")
    private Address address;
    
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "cart_id", referencedColumnName = "id")
    private Cart cart;

    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 int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public Cart getCart() {
        return cart;
    }

    public void setId(Cart cart) {
        this.cart = cart;
    }
    
    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + "]";
    }
    
}

here is the application properties

server.port=8183

#db configuration
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/OnlineShop
spring.datasource.username=root
spring.datasource.password=franklampard08@


# hibernate configuration and JPA configuration

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.open-in-view=true

here is the 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.onlineDairyShop</groupId>
    <artifactId>onlineDairyShop</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>onlineDairyShop</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</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>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </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>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

here is the log for the same


2024-04-23T00:22:00.380+05:30  INFO 1688 --- [           main] c.o.o.OnlineDairyShopApplication         : No active profile set, falling back to 1 default profile: "default"
2024-04-23T00:22:00.786+05:30  INFO 1688 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2024-04-23T00:22:00.800+05:30  INFO 1688 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 8 ms. Found 0 JPA repository interfaces.
2024-04-23T00:22:01.082+05:30  INFO 1688 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8181 (http)
2024-04-23T00:22:01.089+05:30  INFO 1688 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2024-04-23T00:22:01.089+05:30  INFO 1688 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.20]
2024-04-23T00:22:01.126+05:30  INFO 1688 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2024-04-23T00:22:01.127+05:30  INFO 1688 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 705 ms
2024-04-23T00:22:01.216+05:30  INFO 1688 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2024-04-23T00:22:01.254+05:30  INFO 1688 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 6.4.4.Final
2024-04-23T00:22:01.274+05:30  INFO 1688 --- [           main] o.h.c.internal.RegionFactoryInitiator    : HHH000026: Second-level cache disabled
2024-04-23T00:22:01.365+05:30  INFO 1688 --- [           main] o.s.o.j.p.SpringPersistenceUnitInfo      : No LoadTimeWeaver setup: ignoring JPA class transformer
2024-04-23T00:22:01.376+05:30  INFO 1688 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2024-04-23T00:22:01.566+05:30  INFO 1688 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Added connection com.mysql.cj.jdbc.ConnectionImpl@f18b738
2024-04-23T00:22:01.567+05:30  INFO 1688 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2024-04-23T00:22:01.606+05:30  WARN 1688 --- [           main] org.hibernate.orm.deprecation            : HHH90000025: MySQLDialect does not need to be specified explicitly using 'hibernate.dialect' (remove the property setting and it will be selected by default)
2024-04-23T00:22:01.742+05:30  INFO 1688 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000489: No JTA platform available (set 'hibernate.transaction.jta.platform' to enable JTA platform integration)
2024-04-23T00:22:01.747+05:30  INFO 1688 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2024-04-23T00:22:02.043+05:30  INFO 1688 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8181 (http) with context path ''
2024-04-23T00:22:02.049+05:30  INFO 1688 --- [           main] c.o.o.OnlineDairyShopApplication         : Started OnlineDairyShopApplication in 1.923 seconds (process running for 2.428)

please help and thank you in advance.

I tried searching the answer for this in google, asked fellow colleagues and followed some of the stackoverflow questions as well, However it is not getting resolved

2

Answers


  1. have you tried changing the ddl-auto to ‘create’ for the first run ?

    spring.jpa.hibernate.ddl-auto=create

    Login or Signup to reply.
  2. It looks like Spring Boot JPA is not finding your entity classes. This can be due the package structure. The recommended structure is to have the main application class in the base package. In your case it would be com.onlineDairyShop. Just try it.

    If this is not an option for you then you must add some annotations to your main application class. For entities and repositories. Like this:

    @EntityScan("com.onlineDairyShop.entities")
    @EnableJpaRepositories("com.onlineDairyShop.repositories")
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search