Home > Enterprise >  data is not getting saved in database using springboot?
data is not getting saved in database using springboot?

Time:10-14

My entity class looks like this

Person.java
package com.poc.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Person {
//properties
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String fName;
    private String lName;

    public Person() {

    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getfName() {
        return fName;
    }

    public void setfName(String fName) {
        this.fName = fName;
    }

    public String getlName() {
        return lName;
    }

    public void setlName(String lName) {
        this.lName = lName;
    }
}

when i run the spring boot application it is running perfectly but table is no being created in database

My application file looks something like this

spring.jpa.show-sql= true
spring.datasource.url= jdbc:postgresql://localhost:5432/Comapany
spring.datasource.username= postgres
spring.datasource.password= 0000
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation= true
spring.jpa.hibernate.ddl-auto= update

can you tell me what is wrong here thank you in advance.

CodePudding user response:

Spring provides a JPA-specific property that Hibernate can use to generate the DDL: spring.jpa.hibernate.ddl-auto

Hibernate properties include: create, update, create-drop, validate and none.

  • create: Hibernate will first drop the existing tables and then create new ones.
  • update: The object model created based on the mapping is compared with the existing data in the schema and then Hibernate will update the schema with the new information. It will never delete existing tables or columns even if they are no longer required by your application.
  • create-drop: Similar to create but after all operations have been completed, Hibernate will drop the database. Usually used for unit testing.
  • validate: Hibernate only validates whether the tables and columns exist, otherwise it will throw an exception.
  • none: This value is used to disable DDL generation.

CodePudding user response:

Try this:

@Entity
@Table("person") 
//or what table name you used in your database for Person

public class Person{
}

CodePudding user response:

I think you are missing some propertie values in application.properties file, please check the below file which might help you

spring.datasource.name=localSource
spring.jpa.generate-ddl=true
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql= true
spring.datasource.url= jdbc:postgresql://localhost:5432/Comapany
spring.datasource.username= postgres
spring.datasource.password= 0000
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation= true
spring.jpa.hibernate.ddl-auto= update

I hope, it helps!

  • Related