SpringBoot integration Jpa

SpringBoot version: 2.2.4

A simple demo

Add dependencies to POM files:

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

Configuring database Connections

Spring. The datasource. Url = JDBC: mysql: / / 127.0.0.1:3306 / note? useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=Copy the code

Add entities

@Entity(name = "notebook")
@ToString
@Getter
@Setter
public class Note implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @Column(name = "title")
    private String title;
    @Column(name = "content")
    private String content;
}
Copy the code

Add the repository

public interface NoteRepository extends JpaRepository<Note.Integer> {}Copy the code

Add the service

public interface NoteService {

    Note getNote(Integer id);
}
Copy the code

The service implementation

@Service
public class NoteServiceImpl implements NoteService {

    @Autowired
    private NoteRepository noteRepository;

    @Override
    public Note getNote(Integer id) {
        returnnoteRepository.findById(id).get(); }}Copy the code

Add the Controller

@RestController
@RequestMapping("/note")
public class NoteController {

    @Autowired
    private NoteService noteService;

    @RequestMapping("/get")
    public Note getNote(Integer id){
        returnnoteService.getNote(id); }}Copy the code

The problem

In NoteServiceImpl noteRepository. FindById (id). The get (); Not dated by terepository.getone (ID)

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.springboot.jpa.model.Note$HibernateProxy$LH4iQSqZ["hibernateLazyInitializer"])
Copy the code

This article was automatically published by ArtiPub