-
1. Re: how to use s:selectItems with h:selectManyCheckbox
nickarls Mar 11, 2008 7:39 AM (in response to anatorian)Sounds like a job for <s:convertEntity>
-
2. Re: how to use s:selectItems with h:selectManyCheckbox
anatorian Mar 11, 2008 7:43 AM (in response to anatorian)I tried the s:convertEntity, but I got another error Conversion Error setting value '0 1' for '{userHome.instance.roles}'.
-
3. Re: how to use s:selectItems with h:selectManyCheckbox
nickarls Mar 11, 2008 7:50 AM (in response to anatorian)Have you implemented equals() and hashCode() for your Role class?
-
4. Re: how to use s:selectItems with h:selectManyCheckbox
anatorian Mar 11, 2008 7:54 AM (in response to anatorian)No, let me try.
By the way, how do you paste less-than charater here? I got an instant error message when I type it here.
-
5. Re: how to use s:selectItems with h:selectManyCheckbox
nickarls Mar 11, 2008 8:04 AM (in response to anatorian)The Help-button said:
But if you really want to type a special character such as * or +, you need to escape it with a \. -
6. Re: how to use s:selectItems with h:selectManyCheckbox
anatorian Mar 11, 2008 8:10 AM (in response to anatorian)No, it doesn't work. I implemented equals and hashCode, but it said:
Conversion Error setting value '0 1' for '#{userHome.instance.roles}' -
7. Re: how to use s:selectItems with h:selectManyCheckbox
nickarls Mar 11, 2008 8:16 AM (in response to anatorian)Paste all relevant code (xhtml, entity, backing bean)
-
8. Re: how to use s:selectItems with h:selectManyCheckbox
anatorian Mar 11, 2008 8:29 AM (in response to anatorian)The xhtml:
<h:selectManyCheckbox value=#{userHome.instance.roles}
>
<s:selectItems value=#{roleList.resultList}
var=role
label=#{role.name}
/>
<s:convertEntity />
</h:selectManyCheckbox>The entity:
@Entity
public class User {
private Long id;private Set<Role> roles = new HashSet<Role>(0);
@ManyToMany(targetEntity = Role.class)
@JoinTable()
public Set<Role> getRoles() {
return roles;
}public void setRoles(Set<Role> roles) {
this.roles = roles;
}@Id
@GeneratedValue
public Long getId() {
return id;
}public void setId(Long id) {
this.id = id;
}}
@Entity
@Table
public class Role implements java.io.Serializable {
private Long id;
private Set<User> users = new HashSet<User>(0);@Id
@GeneratedValue
@Column(name =id
, unique = true, nullable = false)
public Long getId() {
return this.id;
}public void setId(Long id) {
this.id = id;
}@ManyToMany(mappedBy=
roles
)
public Set<User> getUsers() {
return this.users;
}public void setUsers(Set<User> users) {
this.users = users;
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Role other = (Role) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}The user home and role list is generated by the seam:
@Name(userHome
)
public class UserHome extends EntityHome<User> {public void setUserId(Long id) {
setId(id);
}public Long getUserId() {
return (Long) getId();
}@Override
protected User createInstance() {
User user = new User();
return user;
}public void wire() {
}public boolean isWired() {
return true;
}public User getDefinedInstance() {
return isIdDefined() ? getInstance() : null;
}}
@Name(
roleList
)
public class RoleList extends EntityQuery {private static final String[] RESTRICTIONS = {
lower(role.code) like concat(lower(#{roleList.role.code}),'%')
,
lower(role.description) like concat(lower(#{roleList.role.description}),'%')
,
lower(role.name) like concat(lower(#{roleList.role.name}),'%')
,};private Role role = new Role();
@Override
public String getEjbql() {
returnselect role from Role role
;
}@Override
public Integer getMaxResults() {
return 25;
}public Role getRole() {
return role;
}@Override
public List<String> getRestrictions() {
return Arrays.asList(RESTRICTIONS);
}}
-
9. Re: how to use s:selectItems with h:selectManyCheckbox
nickarls Mar 11, 2008 8:44 AM (in response to anatorian)Please use code tags
-
10. Re: how to use s:selectItems with h:selectManyCheckbox
anatorian Mar 11, 2008 9:03 AM (in response to anatorian)The xhtml:
<code>
<h:selectManyCheckbox value=#{userHome.instance.roles}>
<s:selectItems value=#{roleList.resultList} var=role label=#{role.name} />
<s:convertEntity />
</h:selectManyCheckbox>
</code>The entity:
<code>
@Entity
public class User {
private Long id;private Set<Role> roles = new HashSet<Role>(0);
@ManyToMany(targetEntity = Role.class)
@JoinTable()
public Set<Role> getRoles() {
return roles;
}public void setRoles(Set<Role> roles) {
this.roles = roles;
}@Id
@GeneratedValue
public Long getId() {
return id;
}public void setId(Long id) {
this.id = id;
}
}@Entity
public class Role{
private Long id;
private Set<User> users = new HashSet<User>(0);@Id
@GeneratedValue
@Column(name =id
, unique = true, nullable = false)
public Long getId() {
return this.id;
}public void setId(Long id) {
this.id = id;
}@ManyToMany(mappedBy=
roles
)
public Set<User> getUsers() {
return this.users;
}public void setUsers(Set<User> users) {
this.users = users;
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Role other = (Role) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
@Name(roleList
)
public class RoleList extends EntityQuery {private static final String[] RESTRICTIONS = {
lower(role.code) like concat(lower(#{roleList.role.code}),'%')
,
lower(role.description) like concat(lower(#{roleList.role.description}),'%')
,
lower(role.name) like concat(lower(#{roleList.role.name}),'%')
,};private Role role = new Role();
@Override
public String getEjbql() {
returnselect role from Role role
;
}@Override
public Integer getMaxResults() {
return 25;
}public Role getRole() {
return role;
}@Override
public List<String> getRestrictions() {
return Arrays.asList(RESTRICTIONS);
}}
@Name(
userHome
)
public class UserHome extends EntityHome<User> {public void setUserId(Long id) {
setId(id);
}public Long getUserId() {
return (Long) getId();
}@Override
protected User createInstance() {
User user = new User();
return user;
}public void wire() {
}public boolean isWired() {
return true;
}public User getDefinedInstance() {
return isIdDefined() ? getInstance() : null;
}
}
</code> -
11. Re: how to use s:selectItems with h:selectManyCheckbox
nickarls Mar 11, 2008 9:06 AM (in response to anatorian)Please use code tags. And check in the preview that they are correct ;-)
-
12. Re: how to use s:selectItems with h:selectManyCheckbox
anatorian Mar 11, 2008 9:33 AM (in response to anatorian)The xhtml: <h:selectManyCheckbox value=#{userHome.instance.roles}> <s:selectItems value=#{roleList.resultList} var=role label=#{role.name} /> <s:convertEntity /> </h:selectManyCheckbox> The entities: @Entity public class User { private Long id; private Set<Role> roles = new HashSet<Role>(0); @ManyToMany(targetEntity = Role.class) @JoinTable() public Set<Role> getRoles() { return roles; } public void setRoles(Set<Role> roles) { this.roles = roles; } @Id @GeneratedValue public Long getId() { return id; } public void setId(Long id) { this.id = id; } } @Entity public class Role{ private Long id; private Set<User> users = new HashSet<User>(0); @Id @GeneratedValue @Column(name = "id", unique = true, nullable = false) public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } @ManyToMany(mappedBy="roles") public Set<User> getUsers() { return this.users; } public void setUsers(Set<User> users) { this.users = users; } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; final Role other = (Role) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; return true; } } @Name("roleList") public class RoleList extends EntityQuery { private static final String[] RESTRICTIONS = { "lower(role.code) like concat(lower(#{roleList.role.code}),'%')", "lower(role.description) like concat(lower(#{roleList.role.description}),'%')", "lower(role.name) like concat(lower(#{roleList.role.name}),'%')",}; private Role role = new Role(); @Override public String getEjbql() { return "select role from Role role"; } @Override public Integer getMaxResults() { return 25; } public Role getRole() { return role; } @Override public List<String> getRestrictions() { return Arrays.asList(RESTRICTIONS); } } @Name("userHome") public class UserHome extends EntityHome<User> { public void setUserId(Long id) { setId(id); } public Long getUserId() { return (Long) getId(); } @Override protected User createInstance() { User user = new User(); return user; } public void wire() { } public boolean isWired() { return true; } public User getDefinedInstance() { return isIdDefined() ? getInstance() : null; } }
-
13. Re: how to use s:selectItems with h:selectManyCheckbox
danielc.roth Mar 11, 2008 9:34 AM (in response to anatorian)Hint: Paste the code, mark it, select
Code block
in the message dropdown. or just put one` before and one ` after your code, which will show
before and one
in the preview.
(I did the same mistake the first time too)
-
14. Re: how to use s:selectItems with h:selectManyCheckbox
nickarls Mar 11, 2008 10:19 AM (in response to anatorian)
Daniel Roth wrote on Mar 11, 2008 09:34 AM:
Hint: Paste the code, mark it, selectCode block
in the message dropdown.
Offer not valid on IE7 currently.