我遇到了Ebean的问题。我有通常的对象PsecUser,PsecRoles和PsecPermission。 用户可以拥有多个权限或角色,角色可以拥有多个权限。 这里的代码(摘录):
@Entity
public class PsecPermission {
@Id
@GeneratedValue
private Long id;
@Column(unique=true, nullable=false)
private String name;
@Column(nullable=false)
private String type = PsecBasicPermission.class.getName();
@Column(nullable=false)
private String target;
@Column(nullable=false)
private String actions;
}
@Entity
public class PsecRole {
@Id
@GeneratedValue
private Long id;
@Column(unique=true, nullable=false)
private String name;
@Temporal(TemporalType.TIMESTAMP)
private Date lastUpdate;
@ManyToMany(fetch=FetchType.EAGER)
private List<PsecPermission> psecPermissions;
private boolean defaultRole = false;
}
我写了以下帮助方法:
public PsecRole createOrUpdateRole(String name, boolean defaultRole, String... permissions) {
PsecRole result = server.find(PsecRole.class).
where().eq("name", name).findUnique();
if (result == null) {
result = new PsecRole();
result.setName(name);
}
final List<PsecPermission> permissionObjects = server.find(PsecPermission.class).
where().in("name", (Object[])permissions).findList();
result.setPsecPermissions(permissionObjects);
result.setDefaultRole(defaultRole);
final Set <ConstraintViolation <PsecRole>> errors =
Validation.getValidator().validate(result);
if (errors.isEmpty()) {
server.save(result);
server.saveManyToManyAssociations(result, "psecPermissions");
} else {
log.error("Can't save role: " + name +"!");
for (ConstraintViolation <PsecRole> constraintViolation : errors) {
log.error(" " + constraintViolation);
}
}
return result;
}
并尝试以下测试:
@Test
public void testCreateOrUpdateRole() {
String[] permNames = {"Test1", "Test2", "Test3"};
List <PsecPermission> permissions = new ArrayList <PsecPermission>();
for (int i = 0; i < permNames.length; i++) {
helper.createOrUpdatePermission(permNames[i], "target"+ i, "actions" +i);
PsecPermission perm = server.find(PsecPermission.class).where().eq("name", permNames[i]).findUnique();
assertThat(perm.getTarget()).isEqualTo("target" + i);
assertThat(perm.getActions()).isEqualTo("actions" + i);
permissions.add(perm);
}
PsecRole orgRole = helper.createOrUpdateRole(ROLE, false, permNames);
testRole(permNames, orgRole);
PsecRole role = server.find(PsecRole.class).where().eq("name", ROLE).findUnique();
testRole(permNames, role);
}
private void testRole(String[] permNames, PsecRole role) {
assertThat(role).isNotNull();
assertThat(role.getName()).isEqualTo(ROLE);
assertThat(role.isDefaultRole()).isEqualTo(false);
assertThat(role.getPermissions()).hasSize(permNames.length);
}
如果检查readed角色的权限数,则会失败。它总是0。 我查看了数据库,发现psec_role_psec_permission一直是空的。
知道代码有什么问题吗?
你可以从https://github.com/opensource21/ebean-samples/downloads获得一个纯Ebean示例,它使用ebean中的eclipse-plugin。
答案 0 :(得分:3)
此问题有两种解决方案:
只需在PsceRole
添加级联选项即可@ManyToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
private List<PsecPermission> psecPermissions;
并删除您在cascade-solution-branch中找到的server.saveManyToManyAssociations(result, "psecPermissions");
。
更清洁的解决方案,因为您不需要定义cascase-也许您不需要它:
只是不要替换列表,只需将您的条目添加到列表中即可。更好的是添加新的并删除旧的。这意味着createOrUpdateRole
:
result.getPsecPermissions().addAll(permissionObjects);
而不是
result.setPsecPermissions(permissionObjects);