Search code examples
springspring-bootspring-data-jpa

Data is not getting inserted in Spring boot + Spring data JPA?


I have made a spring boot application, in the application I am spring-data-jpa. The problem is there while inserting the data in database, it shows the data is been inserted but there is no data in database. Following is my code. Please let me know where I am doing mistake. Thanks in advance.

Main Class

    package avs.controller;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.domain.EntityScan;
    import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

    @SpringBootApplication
    @EntityScan(basePackages = {"avs.pojo"})
    @EnableJpaRepositories(basePackages = {"avs.repository"})
    public class AVS {
        public static void main(String[] args) {
            SpringApplication.run(AVS.class, args);
        }
    }

Controller Class

package avs.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import avs.pojo.User;
import avs.repository.UserReposiroty;

@Controller
public class UserController {

    @Autowired
    private UserReposiroty userRepository;

    @RequestMapping(value = "/saveuser", method = RequestMethod.POST)
    @ResponseBody
    @Bean
    public String saveProduct(/*@RequestBody User user*/) {
        User user = new User();
        user.setUserId("ani");
        user.setPassword("ani123");
        user.setApplicatonId(123l);
        userRepository.save(user);

        return user.getUserId().toString();
    }
}

Pojo Class

package avs.pojo;

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

import org.springframework.stereotype.Component;

@Entity
@Component
@Table(name="user")
public class User extends BasePOJO {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id")
    private Long id;

    @Column(name="application_id")
    private Long applicatonId;

    @Column(name="user_id")
    private String userId;

    @Column(name="password")
    private String password;

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Long getId() {
        return id;
    }

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

    public Long getApplicatonId() {
        return applicatonId;
    }

    public void setApplicatonId(Long applicatonId) {
        this.applicatonId = applicatonId;
    }

    @Override
    public String toString(){
        return String.format(
                "Customer[id=%d, userId='%s', password='%s']",
                id, userId, password);
    }

}

Repository Class

package avs.repository;

import org.springframework.data.repository.CrudRepository;

import avs.pojo.User;

public interface UserReposiroty extends CrudRepository<User, Long>  {

}

Application Properties

#port to run tomcat
server.port=8021
#database configuration
spring.datasource.url=jdbc:mysql://localhost/avs_db
spring.datasource.username=avsadmin
spring.datasource.password=avsadmin
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# ===============================
# = JPA / HIBERNATE
# ===============================

# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager).

# Show or not log for each sql query
spring.jpa.show-sql = true

# Hibernate ddl auto (create, create-drop, update): with "update" the database
# schema will be automatically updated accordingly to java entities found in
# the project
spring.jpa.hibernate.ddl-auto = update

# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

build gradle file

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.2.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'

jar {
    baseName = 'avs-bookcab'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

dependencies {
    //Spring boot and MVC
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf', version: '1.5.2.RELEASE'
    compile("org.springframework.boot:spring-boot-devtools")
    //Spring boot and hibernate
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("com.h2database:h2")
    compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.6'

}

Solution

  • If the H2 database is found on your classpath, Spring Boot will automatically set up an in memory H2 database for your use. Just remove H2 dependency from your gradle!