OneToOne Association In JPA not working using Spring boot and spring data JPA
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 ?
@OneToMany
Users
What documentation told you that having
@OneToOne on a multi valued field of type List is correct?– Billy Frost
Aug 27 at 7:33
@OneToOne
List
Its not multivalued type. Check my updated question please.
– Mr.DevEng
Aug 27 at 7:35
The error message says clearly
@OneToMany and you had a OneToMany before changing your question. And now you don't have one. And now the error message is not relevant to the question. So if you are going to rewrite questions open a new question rather than deeming all responses redundant– Billy Frost
Aug 27 at 7:48
@OneToMany
OneToMany
3 Answers
3
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
Yes. For each user in users table is mapped to only one entry in userrolemapping table. So here I thought that one to one mapping. Did you got my association that how I am thinking . Please let me know if I went in wrong direction.
– Mr.DevEng
Aug 27 at 7:06
looks like you want to use manytomany so that 3rd table will be automatically will get created if mapping and mappedBy is correct.let mappings handle everything.see this baeldung.com/hibernate-many-to-many
– Alien
Aug 27 at 7:08
Annotation @OneToOne defines a single-valued association to another entity, and in your case you associate a user to a Set of UserRoleMapping instead of associating it with a single object of that class. Use @ManyToOne annotation
No. for each user record in user have the corresponding record in userrolemapping . So I am thought that this is a one to one mapping.Please letme know If I went wrong. Thank you for your response.
– Mr.DevEng
Aug 27 at 7:09
if he uses manytoone also public Set<UserRoleMapping> roleUserRoleMappingMappingJoin; this will be wrong as for manytoone also we should have single object only.
– Alien
Aug 27 at 7:10
If each user has a unique record in UserRoleMapping, then why you use Set? As far as I understand from the table name, it is an association for users and roles in a different table, and in this case you need to use @ManyToMany
– cthem
Aug 27 at 7:12
Actually the exception refers to an invalid @OneToMany, @ManyToMany or @CollectionOfElement mapping
@OneToMany, @ManyToMany or @CollectionOfElement
and this can only be
@OneToMany()
@JoinColumn(name="nuser_id" , referencedColumnName="nuserId")
public Users nuserId;
If the @OneToMany relation is valid change this at first to
@OneToMany
@OneToMany()
@JoinColumn(name="nuser_id" , referencedColumnName="nuserId")
public List<Users> users;
If the @OneToMany relation is NOT valid change this to
@OneToMany
@OneToOne()
@JoinColumn(name="nuser_id" , referencedColumnName="nuserId")
public Users users;
Thank you for your response sir. But I still doubt with this one to many. Because each record in users have corresponding record in userrolemapping. Not many. So how it would be one to many ? I did not understood your point?
– Mr.DevEng
Aug 27 at 7:12
You have one role that is associated to many Users. E.g. ONE Role 'User' will possibly have MANY users.
– mrkernelpanic
Aug 27 at 7:14
No. In my case I am uses multi tenant model application. In one tenant one user only have one role. May have different , but in another tenant only. Not in same.And I have separate separate DB for each tenant. Here only possibility is one record is only mappes to one record in userrolemapping. I hope that what I am trying to say. Please let me know sir,
– Mr.DevEng
Aug 27 at 7:18
My answer is about your exception and not a consulting to your technical problems/ questions. See my updated answer.
– mrkernelpanic
Aug 27 at 7:23
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What documentation told you that having
@OneToManyon a single valued field of typeUsersis correct?– Billy Frost
Aug 27 at 7:16