'Getting error while fetching data in Spring data JPA for OneToOne relationship

I have two tables named users and doctors and also corressponding JPA entities. Users table has column user_name(unique key, not a primary key) and which is acting as foreign key in doctors table. while fetching data from users table getting below error.

org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private java.lang.String com.api.user.domain.UserEntity.userName] by reflection for persistent property [com.api.user.domain.UserEntity#userName] : 95754

Below are the JPA entities and repository.
JPA Entity - UserEntity.java

@Entity
@Table(name = "users")
public class UserEntity extends GenericEntity {

    @Id
    @Column(name = "_id")
    private Long _id;
    
    @Column(name = "user_name")
    private String userName;
    
    @OneToOne(mappedBy = "userEntity", fetch = FetchType.EAGER, cascade = CascadeType.REFRESH) 
    private DoctorEntity doctorEntity;
}

JPA Entity - DoctorEntity.java

@Entity
@Table(name = "doctors")
public class DoctorEntity extends GenericEntity {
    @Id
    @Column(name="_id")
    private Integer _id;
    
    @OneToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "user_name", nullable = false, referencedColumnName = "user_name")
    private UserEntity userEntity; 
}

JPA Repository

@Repository
@Transactional
public interface UserRepo extends GenericRepo<UserEntity, Long> {
    public UserEntity findByUserName(String userName);
}

@NoRepositoryBean
public interface GenericRepo<GenericEntity, Object> extends JpaRepository<GenericEntity, Object> {

}

Please help, Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source