2011-02-05 33 views
2

我想在JPE中使用GAE-J中的事务。如何使用JPA在特定实体组中创建对象? (Google AppEngine Java)

没有JPA它应该是:

Entity child= new Entity("Child", "ParentKey"); 

,但如何使用JPA办呢?

@Entity 
public class Parent{ 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Key id; 
    private String text; 
} 

@Entity 
public class Child{ 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Key id; 
    private Parent parent; 
    private String text; 
} 

尝试...

Parent parent = new Parent(); 
em.persist(parent); 
em.refresh(parent); 

Child child = new Child(); 
child.setParent(parent); 
em.persist(child); 

这不起作用:

org.datanucleus.store.appengine.DatastoreRelationFieldManager$ChildWithoutParentException: 
Detected attempt to establish Child(130007) as the parent of Parent(132001) but the entity identified by Child(132001) has already been persisted without a parent. A parent cannot be established or changed once an object has been persisted. 

这听起来有点后到前... 我是一个傻瓜?还是有一个简单的方法?

谢谢!

回答

2

凯...我的第一次尝试只是一个小错误。

这应该工作:

@Entity 
public class Parent{ 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Key id; 
    private String text; 
    @OneToMany(targetEntity=Child.class, mappedBy="parent", fetch=FetchType.LAZY) 
    private Set<Child> children = new HashSet<Child>(); 
} 

@Entity 
public class Child{ 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Key id; 
    @ManyToOne(fetch=FetchType.LAZY, targetEntity=Parent.class) 
    private Parent parent; 
    private String text; 
} 
+0

这2类导致增强的错误我。 谷歌AppEngine上增强:显示java.lang.NullPointerException 谷歌AppEngine上增强:通过在引起org.datanucleus.api.jpa.metadata.JPAAnnotationReader.newMetaDataForMember(JPAAnnotationReader.java:1995) 谷歌AppEngine上增强:在org.datanucleus.api .jpa.metadata.JPAAnnotationReader.processMemberAnnotations(JPAAnnotationReader.java:1126) – Boris 2013-02-09 10:04:37

相关问题