Hibernate Annotations Question
[EDIT]
I just found out it's currently not implemented in Annotations and that we have to vote on this issue in JIRA. Thanks anyways.
[END EDIT]
Hello Java_dev users,
I was wondering if anybody could help me solve a (what seems to me as) basic problem. In Hibernate mappings, we are allowed to have collections of value objects as follows:
<set name="roles" lazy="true" table="USER_ROLE" >
<key column="USER_ID" />
<element type="string" column="ROLE" not-null="true"/>
</set>
So we only need one entity (User) and this mapping to have a collection of roles (saved as strings in the USER_ROLE table).
Now, my question is, how in the world do I do this in Hibernate Annotations? It seems Hibernate Annotations expects all Sets to be mapped as associations (OneToMany). But that would entail having another Role Class and having the value wrapped in it and all that. That's not what I want. I just to have a User class with a collection of strings that holds the roles... as follows
class User{
...
private Set<String> roles = new HashSet<String>();
...
public Set<String> getRoles() {
return roles;
}
public void setRoles(Set<String> roles) {
this.roles = roles;
}
public void addRole(String role){
roles.add(role);
}
public void removeRole(String role){
roles.remove(role);
}
}
So is it possible to have collections of basic types and use annotations to define them or is it simply not implemented in Hibernate Annotations yet? I've been tearing out my hair on such a stupid issue. Please, anybody, help...
I just found out it's currently not implemented in Annotations and that we have to vote on this issue in JIRA. Thanks anyways.
[END EDIT]
Hello Java_dev users,
I was wondering if anybody could help me solve a (what seems to me as) basic problem. In Hibernate mappings, we are allowed to have collections of value objects as follows:
<set name="roles" lazy="true" table="USER_ROLE" >
<key column="USER_ID" />
<element type="string" column="ROLE" not-null="true"/>
</set>
So we only need one entity (User) and this mapping to have a collection of roles (saved as strings in the USER_ROLE table).
Now, my question is, how in the world do I do this in Hibernate Annotations? It seems Hibernate Annotations expects all Sets to be mapped as associations (OneToMany). But that would entail having another Role Class and having the value wrapped in it and all that. That's not what I want. I just to have a User class with a collection of strings that holds the roles... as follows
class User{
...
private Set<String> roles = new HashSet<String>();
...
public Set<String> getRoles() {
return roles;
}
public void setRoles(Set<String> roles) {
this.roles = roles;
}
public void addRole(String role){
roles.add(role);
}
public void removeRole(String role){
roles.remove(role);
}
}
So is it possible to have collections of basic types and use annotations to define them or is it simply not implemented in Hibernate Annotations yet? I've been tearing out my hair on such a stupid issue. Please, anybody, help...
