GAE - 一一对一关系中删除给错误 [英] GAE - one-to-one relationship delete giving error
问题描述
我使用Eclipse开发Android与谷歌应用程序引擎。我有3个机构用户 - > UserDetails->的userPassword。每个人都有直通USER_ID一个@OneToOne关系到其他。以下是实体:
I am using Eclipse to develop Android with Google App engine. I have 3 entities User->UserDetails->UserPassword. Each one has a @OneToOne relationship to the other thru USER_ID. Following are the entities:
@Entity
public class User {
@Id
@Column(name = "USER_ID", allowsNull="false")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Key userId;
@Column(name = "USER_NAME", allowsNull="false")
private String userName;
@OneToOne(cascade = CascadeType.ALL,optional=false,orphanRemoval = true)
@JoinColumn(name = "USER_ID")
private UserDetails userDetails;
//getters & setters
}
@Entity(name="USER_DETAILS")
public class UserDetails {
@Id
@Column(name = "USER_ID", allowsNull="false")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Key userId;
@Column(name = "USER_FULLNAME", allowsNull="false")
private String userFullname;
@Column(name = "USER_IMAGEURL", allowsNull="false")
private String userImageURL;
@Column(name = "USER_PASSPHRASE", allowsNull="false")
private String userPassPhrase;
@OneToOne(cascade = CascadeType.ALL,optional=false)
@JoinColumn(name = "USER_ID")
private UserPassword userPassword;
//getters & setters
}
@Entity(name = "USER_PASSWORD")
public class UserPassword {
@Id
@Column(name = "USER_ID", allowsNull="false")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Key userId;
@Column(name = "USER_PASSWORD", allowsNull="false")
private String userPassword;
//getters & setters
}
我有一个 removeAllUsers()
在 Userendpoint.java
方法。这种方法是假设把所有的用户,迭代THRU他们和一个删除用户,为userDetails和密码之一。
I have a removeAllUsers()
method in Userendpoint.java
. This method is suppose to get all the users, iterate thru them and remove users, userDetails and passwords one by one.
public void removeAllUsers() {
log.info("UserEndpoint.removeAllUsers().....");
EntityManager mgr = getEntityManager();
User user = new User();
try {
CollectionResponse<User> allUsers = listUser(null, null);
Collection<User> users = allUsers.getItems();
Iterator<User> itr = users.iterator();
log.info("UserEndpoint.removeAllUsers()....itr.hasNext="+itr.hasNext());
while(itr.hasNext()){
user = itr.next();
user = mgr.find(User.class, user.getUserId());
//mgr.getTransaction().begin();
mgr.remove(user);
//mgr.getTransaction().commit();
log.info("UserEndpoint.removeAllUsers()....User removed");
}
} finally {
mgr.close();
}
log.info("UserEndpoint.removeAllUsers()....End");
}
我的假设是CascadeType.ALL是在用户使用时,当我删除用户,应该删除的UserDetails和密码。我也尝试过交易(行注释掉以上)。我收到以下错误:
My assumption is as CascadeType.ALL is used on User, when I delete User, it should remove UserDetails and Password. I also tried with Transaction(lines commented out above). I get the following error:
WARNING: Delete of com.bean.User@15a9289 needs delete of related object at com..bean.User.userDetails but cannot delete it direct since FK is here
Jun 12, 2014 12:04:01 PM com.google.api.server.spi.SystemService invokeServiceMethod
INFO: cause={0}
javax.persistence.PersistenceException: Illegal argument
at org.datanucleus.api.jpa.NucleusJPAHelper.getJPAExceptionForNucleusException(NucleusJPAHelper.java:298)
at org.datanucleus.api.jpa.JPAEntityManager.close(JPAEntityManager.java:197)`enter code here`
at com.cloudmobilebanking.bean.UserEndpoint.removeAllUsers(UserEndpoint.java:202)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
另外一些行我看到了下面的评论后也:
Also after some lines I see the below comment also:
Caused by: java.lang.IllegalArgumentException: cross-group transaction need to be explicitly specified, see TransactionOptions.Builder.withXGfound both Element {
type: "User"
id: 0x10780000000000
}
and Element {
type: "User"
id: 0x12b80000000000
}
我一直在使用TransactionOptions,但得到了同样的错误尝试。
I have tried using TransactionOptions but get the same error.
我也尝试删除然后密码为userDetails,然后在seqyence用户,但仍同样的错误。
I also tried deleting Password then userDetails and then User in that seqyence but still same error.
任何想法我怎么能解决这个问题?
Any ideas how I can resolve this?
推荐答案
了一段使用JPA / JDO +谷歌端点工作后,我放弃了(有多个问题)。 JPA / JDO则多为RDBMS但谷歌datasore更像是一个HashMap。我决定尝试物化这是谷歌的数据存储而设计的。我与它的经验已经好了。您需要与在讨论一起阅读下面的解决方案:<一href=\"http://stackoverflow.com/questions/24590518/gaeobjectify-parameterized-type-com-google$c$c-objectify-ref-not-supported/24936550#24936550\">GAE+Objectify - 参数化类型com.google code.objectify.Ref不支持
以获得更好的理解。
After working for sometime with JPA/JDO + google endpoints, I gave up (there were multiple issues). JPA/JDO is more for RDBMS but Google datasore is more like a HashMap. I decided to try Objectify which is designed specifically for Google datastore. My experience with it has been better. You need to read the solution below along with the discussion at: GAE+Objectify - Parameterized type com.googlecode.objectify.Ref not supported to get a better understanding.
为了删除物化的用户,是UserDetails和密码,下面是code。
In order to delete the User, UserDetails and Password in Objectify, below is the code.
@ApiMethod(NAME =deleteAllUsers,路径=delete_all_users)
@ApiMethod(name = "deleteAllUsers", path="delete_all_users")
public void deletAllUsers(){
log.info("UserEndpoint.deletAllUsers...");
List<User> users = ofy().load().type(User.class).list();
User user = null;
try{
for(int i=0; i< users.size(); i++){
user = users.get(i);
UserDetails userDetails = user.getUserDetails();
UserPassword userPassword = user.getUserPassword();
ofy().delete().entity(userDetails).now();
ofy().delete().entity(userPassword).now();
ofy().delete().entity(user).now();
}
}
catch(Exception e){
log.info("AccountEndpoint.deleteAllUsers()...exception="+e.getMessage());
}
log.info("AccountEndpoint.deletAllUsers...all users deleted");
}
这篇关于GAE - 一一对一关系中删除给错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!