Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

No default constructor for entity

I’m trying to create an API and i’ve made this code so far:

@Builder
@Data
@Entity
public class IcesiUser {
    @Id
    private UUID userId;
    private String firstName;
    private String lastName;
    private String email;
    private String phoneNumber;
    private String password;
    @OneToMany(mappedBy = "user")
    private List<IcesiAccount> accounts;
    @ManyToOne
    @JoinColumn(name = "icesi_role_role_id")
    private IcesiRole role;}

This is the repository

public interface UserRepository extends JpaRepository<IcesiUser, UUID> {@Query("select u from IcesiUser u where u.email = ?1")Optional<IcesiUser> findByEmail(String email);

@Query("select u from IcesiUser u where u.phoneNumber = ?1")
Optional<IcesiUser> findByPhoneNumber(String phoneNumber);

}

And this is the service that saves the user

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@AllArgsConstructor
@Service
public class UserService {

    private final UserRepository userRepository;
    private final UserMapper userMapper;
    public IcesiUser save(UserCreateDTO user) {
        Optional<IcesiUser> userFoundByEmail = userRepository.findByEmail(user.getEmail());
        Optional<IcesiUser> userFoundByPhoneNumber = userRepository.findByPhoneNumber(user.getPhoneNumber());
        if(userFoundByEmail.isPresent() || userFoundByPhoneNumber.isPresent()){
            throw new RuntimeException("User already exists");
        }else{
            IcesiUser icesiUser = userMapper.fromIcesiUserDTO(user);
            icesiUser.setUserId(UUID.randomUUID());
            return userRepository.save(icesiUser);
        }

    }
    public Optional<IcesiUser> findUserByEmail(String email) {
        return userRepository.findByEmail(email);
    }

    public Optional<IcesiUser> findUserByPhoneNumber(String phoneNumber) {
        return userRepository.findByPhoneNumber(phoneNumber);
    }
}

It runs but when I send a POST request via the controller it appears the exception of
org.hibernate.InstantiationException: No default constructor for entity: : com.example.TallerJPA.model.IcesiUser

I have another project as an example and it doesnt have much difference, does anyone know why is this happening?

I’ve tried making a normal builder in the IcesiUser class but when I try to run the application an error shows up.

Also I tried adding @Getter and @Setter as I saw in another post but it does nothing.

>Solution :

When using JPA, entities must have a default (no-argument) constructor.

@Builder
@Data
@Entity
public class IcesiUser {
    // ... (your fields)

    public IcesiUser() {
    }

    // to keep the builder pattern, you can create an all-argument constructor:
    public IcesiUser(UUID userId, String firstName, String lastName, String email, String phoneNumber, String password, List<IcesiAccount> accounts, IcesiRole role) {
        this.userId = userId;
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.phoneNumber = phoneNumber;
        this.password = password;
        this.accounts = accounts;
        this.role = role;
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading