2011-02-05 1 views
2

JPAで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つのクラスは私のためにエンハンサーエラーを引き起こします。 GoogleのAppEngineのエンハンサー:java.lang.NullPointerExceptionが GoogleのAppEngineのエンハンサー:によって引き起こさorg.datanucleus.api.jpa.metadata.JPAAnnotationReader.newMetaDataForMember(JPAAnnotationReader.java:1995) GoogleのAppEngineのエンハンサーで:org.datanucleus.apiで.jpa.metadata.JPAAnnotationReader.processMemberAnnotations(JPAAnnotationReader.java:1126) – Boris

+0

JPA 2を設定するだけで、今すぐ動作します。しかし、これはバック協会なしではこれが不可能だとは言わないのですか? – Boris

関連する問題