It’s late in the office and you are stuck with this strange Jpa code with JoinColumns and cascades and you cannot find what goes wrong. You wish there is a way to view the queries printed and also the values.
With a little tweaking to your Spring Boot application this is possible.

With the help of lombock heres is our jpa model.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
package com.gkatzioura.hibernatelog.dao;
 
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
 
import lombok.Data;
 
@Data
@Entity
@Table(name = "application_user")
public class ApplicationUser {
 
    @Id
    private Long id;
 
    private String username;
 
    private String password;
 
}

It’s repository

1
2
3
4
5
6
package com.gkatzioura.hibernatelog.dao;
 
import org.springframework.data.repository.CrudRepository;
 
public interface ApplicationUserRepository extends CrudRepository {
}

A not found exception

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.gkatzioura.hibernatelog.controller;
 
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
 
@ResponseStatus(value = HttpStatus.NOT_FOUND)
class ApplicationUserNotFoundException extends RuntimeException {
 
    public ApplicationUserNotFoundException() {
    }
 
    public ApplicationUserNotFoundException(String message) {
        super(message);
    }
 
    public ApplicationUserNotFoundException(String message, Throwable cause) {
        super(message, cause);
    }
 
    public ApplicationUserNotFoundException(Throwable cause) {
        super(cause);
    }
 
    public ApplicationUserNotFoundException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }
}

And a controller

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.gkatzioura.hibernatelog.controller;
 
import java.util.Optional;
 
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
 
import com.gkatzioura.hibernatelog.dao.ApplicationUser;
import com.gkatzioura.hibernatelog.dao.ApplicationUserRepository;
 
@RestController
public class ApplicationUserController {
 
    private final ApplicationUserRepository applicationUserRepository;
 
    public ApplicationUserController(ApplicationUserRepository applicationUserRepository) {
        this.applicationUserRepository = applicationUserRepository;
    }
 
    @GetMapping("/user/{id}")
    @ResponseBody
    public ApplicationUser getApplicationUser(@PathVariable Long id) {
        Optional applicationUser = applicationUserRepository.findById(id);
        if(applicationUser.isPresent()) {
            return applicationUser.get();
        } else {
            throw new ApplicationUserNotFoundException();
        }
    }
 
}

By adding the following to application.yaml we ensure the creation of the table through hibernate, the logging of the queries, the formatting of the sql queries logged and also the actual parameters values displayed.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
spring:
  jpa:
    hibernate:
      ddl-auto: create
    properties:
      hibernate:
        show_sql: true
        use_sql_comments: true
        format_sql: true
logging:
  level:
    org:
      hibernate:
        type: trace

Just

1
curl http://localhost:8080/user/1

And you got your logs.