(1)在seamspace里面的userList的页面中,只用
<h:dataTable
id="threads"
value="#{users}"
var="user"
styleClass="security"
cellspacing="0"
headerClass="header"
rowClasses="odd,even"
columnClasses=",,enabled,action">
<h:column width="auto">
<f:facet name="header">
User name
</f:facet>
#{user}
</h:column>
<h:column width="auto">
<f:facet name="header">
Action
</f:facet>
<s:link value="Delete" action="#{identityManager.deleteUser(userSearch.selectedUser)}"
rendered="#{s:hasPermission('seam.user', 'delete')}"
/>
</h:column>
</h:dataTable>
value="#{users}" 这样就可以把所有的user全部列出来。如果这里谁有疑问的可以问我,呵呵。
另外在删除的时候identityManager.deleteUser(userSearch.selectedUser)这样就可以轻而易举的删除。
(2) 我想表达的意思是我自己CMS项目的userList.xhtml有自己的业务逻辑在里面不可能象它这样就可以搞定。我的所有功能我都实现了,现在就剩下删除的功能没有实现。
因为表有依赖关系在里面所有简单的删除不行。必须先删除user_role表中数据,再删除users表中数据。
但是要用它的那个方法identityManager.deleteUser(userSearch.selectedUser),这种条件是必须用value="#{users}"把所有的users列出来,才能用它。所有我就只能抛弃这样的方法,自己去找了。
(3)我自己写:
<s:link id="delete" value="Delete" οnclick="JavaScript:if(confirm('Are you sure to delete it?')){return true;}else{return false;}"
action="#{usersHome.remove}" rendered="true"
view="/usersadmin/UsersList.xhtml">
<f:param name="usersId" value="#{_users.id}" />
</s:link>
我来覆盖掉@Override public String remove(){}方法。但是我在写删除的时候发现这个表user_role没有对应的实体给它,所有用实体对应的删除方法也行不通了。
(4) 我想用eclipse反向从数据库里面生成实体,把中间表user_role的实体弄出来。结果还是没有达到我要的目的。因为user_role表只有2个字段是:user_id和role_id,且它们一起作为主键。这我就没有见过了。晕死了!! 结果我反向生成的实体,你知道生成了什么吗? 下面是实体的内容。
package org.domain.entityTest.entity;
// Generated 2009-9-10 15:41:59 by Hibernate Tools 3.2.2.GA
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.hibernate.validator.NotNull;
/**
* UsersRole generated by hbm2java
*/
@Entity
@Table(name = "users_role", catalog = "cms")
public class UsersRole implements java.io.Serializable {
private UsersRoleId id;
public UsersRole() {
}
public UsersRole(UsersRoleId id) {
this.id = id;
}
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name = "usersId", column = @Column(name = "users_id", nullable = false)),
@AttributeOverride(name = "roleId", column = @Column(name = "role_id", nullable = false)) })
@NotNull
public UsersRoleId getId() {
return this.id;
}
public void setId(UsersRoleId id) {
this.id = id;
}
}
package org.domain.entityTest.entity;
// Generated 2009-9-10 15:41:59 by Hibernate Tools 3.2.2.GA
import javax.persistence.Column;
import javax.persistence.Embeddable;
import org.hibernate.validator.NotNull;
/**
* UsersRoleId generated by hbm2java
*/
@Embeddable
public class UsersRoleId implements java.io.Serializable {
private long usersId;
private long roleId;
public UsersRoleId() {
}
public UsersRoleId(long usersId, long roleId) {
this.usersId = usersId;
this.roleId = roleId;
}
@Column(name = "users_id", nullable = false)
@NotNull
public long getUsersId() {
return this.usersId;
}
public void setUsersId(long usersId) {
this.usersId = usersId;
}
@Column(name = "role_id", nullable = false)
@NotNull
public long getRoleId() {
return this.roleId;
}
public void setRoleId(long roleId) {
this.roleId = roleId;
}
public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof UsersRoleId))
return false;
UsersRoleId castOther = (UsersRoleId) other;
return (this.getUsersId() == castOther.getUsersId())
&& (this.getRoleId() == castOther.getRoleId());
}
public int hashCode() {
int result = 17;
result = 37 * result + (int) this.getUsersId();
result = 37 * result + (int) this.getRoleId();
return result;
}
}
竟然有2个实体,这个我从来没有见过的。所有在后面写remove方法的时候写不出来。
(5) 最后终于想出来办法了。
@Override
public String remove(){
if(this.usersId != null && !"".equals(this.usersId)){
entityManager.createNativeQuery("delete ur.* from users_role ur where ur.users_id="+this.usersId).executeUpdate();
removeUser = entityManager.find(Users.class, this.usersId);
entityManager.remove(removeUser);
}
return "removed";
}
直接用entityManager的createNativeQuery本地SQL语句就可以搞定了。
依次谨献:我本人。哈哈! 以后可以回顾。