Search code examples
spring-bootjpamodel-associations

OneToOne Association In JPA not working using Spring boot and spring data JPA


I am trying to implement the OneToOne association in JPA and trying to join two tables using spring boot and spring data JPA. I created one spring boot microservice and implemented the one to one association in my model. But when I am running code I am getting the following error ,

Caused by: org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElement

Here My First model class Users.java is like following,

@Entity
@Table(name = "users")
public class Users implements Serializable {

private static final long serialVersionUID = 9178661439383356177L;

@Id

@Column(name="user_id")
public Integer userId;

@Column(name="username")
public String username;

@Column(name="password")
public String password;


}

And I am testing association by controller using following code,

@GetMapping("/load")
    public Users load() {
        return (Users) userObj.findAll();
    }

Can anyone help to resolve this association issue please ?


Solution

  • This is wrong.

    @OneToOne(mappedBy="nuserId")
    public Set<UserRoleMapping> roleUserRoleMappingMappingJoin;
    }
    

    OneToOne means only one object..right?

    See this for mappings understandings.

    https://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/collections.html#collections-persistent