【发布时间】:2011-08-30 10:44:00
【问题描述】:
我只有一周的 GAE/Java 经验,并尝试将遗留应用程序(使用 PHP/MySQL 开发)移植到 GAE+JDO。我现在在创建两个表(GAE 中的种类)之间的关系时遇到了一个基本问题。
就是这样: 我们有一个保存用户身份验证信息的 Users 表。它还有一个字段user_role,其中存储了role_id,它实际上是另一个表user_roles的外键。
从 GAE 中的 Entity-Relationship 文档中,我了解到 DataStore 不支持外键关系,并通过改编文档中的 Employee-ContactInfo 示例设计了 Users 类。
当我执行应用程序时,每次我在 Users 表中添加条目时都会插入 user_roles 类型。 user_roles 种类应该只有三个静态值。但是,当我在 Users 中输入更多记录时,这具有冗余值。
我认为我遗漏了一些非常琐碎的事情,但由于我对数据存储缺乏经验,我无法弄清楚。如果有人能指导我解决这个问题,那就太好了。
代码如下:
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Users {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private String userName;
@Persistent
private String password;
@Persistent
private String salt;
@Persistent
private Date createdDate;
@Persistent
private Key createdBy;
@Persistent
private Date lastLogin;
@Persistent
private boolean status;
@Persistent
private String authKey;
@Persistent(defaultFetchGroup="true")
private SecurityRole securityRole;
@Autowired
SecurityRepository securityRepository ;
public SecurityPrincipals(String userName, String password,SecurityRole securityRole,boolean status) {
this.securityRole = securityRole;
this.userName = userName;
this.password = password;
this.status = status;
}
//getters and setters
}
角色定义:
@PersistenceCapable(detachable="true")
public class SecurityRole {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private String securityRoleName;
@Persistent
private String securityRoleDescription;
@Persistent
private String securityRoleStatus;
@Persistent
private Date securityRoleCreatedDate;
public SecurityRole(String securityRoleName, String securityRoleDescription, String securityRoleStatus,String securityBaseType)
{
this.securityRoleName = securityRoleName;
this.securityRoleDescription = securityRoleDescription;
this.securityRoleStatus = securityRoleStatus;
this.securityBaseType = securityBaseType;
}
// getters and setters
}
Controller的相关代码:
SecurityRole securityRole = securityRepository.getSecurityRole( securityRoleName);
users = new Users(userName,password,status,securityRole);
iUserRepository.save(employeeDetails);
这里是getSecurityRole的定义:
public SecurityRole getSecurityRole(String securityRoleName)
{
PersistenceManagerFactory pmf = this.jdoTemplate.getPersistenceManagerFactory();
PersistenceManager pm = pmf.getPersistenceManager();
try {
Query query = pm.newQuery( SecurityRole.class);
query.declareImports("import java.lang.String");
query.declareParameters("String securityRoleName");
query.setFilter("this.securityRoleName == securityRoleName");
List<SecurityRole> securityRoles = (List<SecurityRole>)query.execute(new String(securityRoleName));
SecurityRole temp = null;
for(SecurityRole securityRole: securityRoles)
{
temp = securityRole;
}
return temp;
}
finally {
pm.close();
}
}
这里是iUserRepository.save()的定义:
public void save(Users user) {
jdoTemplate.makePersistent(companyDetails);
}
【问题讨论】:
-
除了您必须对代码进行的所有修改使其不可编译之外,我看不出有任何问题。也许 securityRepository.getSecurityRole 返回实际角色的副本?
-
是的,securityRepository.getSecurityRole 返回实际角色的副本。我已经添加了代码 sn-p。
-
您的代码 sn-p 没有显示任何正在制作的副本。它返回从查询返回的最后一个角色。
-
正如我所说,我看不出代码有什么问题。我认为问题可能是存储库返回了副本而不是实际角色,但事实并非如此,所以它一定是我缺少的其他东西。
标签: java google-app-engine google-cloud-datastore