1

I am making a small Rest API using Spring boot and mysql as database. I am getting "No serializer found" error with fetch type lazy. My application is working fine with fetch type eager but I want fetch type to lazy so how can I resolve this.

User Model:

@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(nullable = false,length = 50)
private String firstName;
@Column(nullable = false,length = 50)
private String lastName;
@Column(nullable = false,unique = true)
private String email;
@OneToOne(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
@JoinColumn(name = "address_id",referencedColumnName = "id")
@JsonIgnoreProperties("user")
private Address address;
}

Address Model:

@Entity
@Table(name = "addresses")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(nullable = false,length = 255)
private String street;
@Column(nullable = false)
private int postalCode;
@Column(nullable = false,length = 100)
private String city;
@OneToOne(mappedBy = "address",fetch = FetchType.LAZY,cascade = CascadeType.ALL)
@JsonIgnoreProperties("address")
private User user;
}

User Service:

@Service
public class UserService {

private final UserRepository userRepository;

@Autowired
public UserService(UserRepository userRepository) {
    this.userRepository = userRepository;
}

public User save(User user){
    return userRepository.save(user);
}

public List<User> find(){
    return userRepository.findAll();
}

public User find(Integer id){
    return userRepository.findById(id).get();
}

public void delete(Integer id){
    userRepository.deleteById(id);
}
}

User Controller:

@RestController
@RequestMapping(path = "users")
public class UserController {
private final UserService userService;
@Autowired
public UserController(UserService userService) {
    this.userService = userService;
}

@GetMapping
public List<User> get(){
    return userService.find();
}

@GetMapping(path = "{id}")
public User get(@PathVariable Integer id){
    return userService.find(id);
}

@PostMapping
public User post(@RequestBody User user){
    return userService.save(user);
}

@DeleteMapping(path = "{id}")
public boolean delete(@PathVariable Integer id){
    userService.delete(id);
    return true;
}
}

Stack Trace:

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: java.util.ArrayList[0]->com.kumasroh.usersapp.models.User["address"]->com.kumasroh.usersapp.models.Address$HibernateProxy$VM2Pif4w["hibernateLazyInitializer"])
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77) ~[jackson-databind-2.13.1.jar:2.13.1]
at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1300) ~[jackson-databind-2.13.1.jar:2.13.1]
at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:400) ~[jackson-databind-2.13.1.jar:2.13.1]

1 Answers1

0

I think fetchType Lazy is used in OneToMany relationship on the otherhand fetchType Eager is used in OneToOne Relationship.