1

I followed this article https://www.callicoder.com/spring-boot-rest-api-tutorial-with-mysql-jpa-hibernate/ to integrate MySql with my SpringBoot application. I added the files Note.java and NoteRepository.java to a new package -

1. Note.java - model changes-

package com.citruspay.prepaid.internal.manager.mysql;

import java.io.Serializable;
import java.util.Date;

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 javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.validator.constraints.NotBlank;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@Entity
@Table(name = "Note")
// @EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = { "createdAt", "updatedAt" }, allowGetters = true)
public class Note implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotBlank
    private String title;

    @Column(nullable = false, updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    // @CreatedDate
    private Date createdAt;

    @Column(nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    // @LastModifiedDate
    private Date updatedAt;

    // Getters and Setters ... (Omitted for brevity)
}

2. NoteRepository.java -

package com.citruspay.prepaid.internal.manager.mysql;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface NoteRepository extends JpaRepository<Note, Long> {

}

3. Application.properties changes -

## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url = jdbc:mysql://localhost:3306/test?useSSL=false
spring.datasource.username = root
spring.datasource.password = root


## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = none
spring.jpa.hibernate.show-sql=true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

4. Added the following to App.java -

@EnableJpaRepositories("com.citruspay.prepaid.internal.manager.mysql")
@EntityScan("com.citruspay.prepaid.internal.manager.mysql")
@EnableTransactionManagement

5. Added following to pom.xml -

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

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

6. Following is table name and schema that I have manually inserted -

CREATE TABLE `note` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(60) NOT NULL,
  `createdAt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `updatedAt` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC;

Running application

On running the application in debug mode, and hitting the endpoint to which I have added the findAll() call, I do not get any error on the call on the repository but get an empty result set -

List<Note> note = noteRepository.findAll();

I have added the import of the Note and NoteRepository; and Autowired the NoteRepository.

I tried debugging mysql queries, to see if queries are actually happening at all. Added these to application.properties but no query is printed -

spring.jpa.hibernate.show-sql=true
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

Please correct if my understanding is wrong.

I read JpaRepository findAll() returns empty result but could not fix my problem.

By the way, I am a developer primarily from a PHP background and having a tough time working on SpringBoot's configurations, annotations etc. Could you please recommend some free or paid tutorial videos to quickly get some better grasp.

Update

No query printed in log. I see this log line generated against the findAll() statement execution -

2018-07-06 19:32:08,723 84821 [XNIO-3 task-1] INFO  o.h.h.i.QueryTranslatorFactoryInitiator [QueryTranslatorFactoryInitiator.java:47] - HHH000397: Using ASTQueryTranslatorFactory

Update 2

Sample data -

INSERT INTO `note` (`id`, `title`, `createdAt`, `updatedAt`) VALUES
    (1, 'test', '2018-07-06 19:01:50', '0000-00-00 00:00:00'),
    (2, 'hello', '2018-07-06 19:01:58', '0000-00-00 00:00:00');
Sandeepan Nath
  • 9,966
  • 17
  • 86
  • 144

0 Answers0