2017-08-08 4 views
0

ここで、エンティティオブジェクトが提供される動的なフレームワークを構築しようとしていますが、現在のエンティティタイプの知識はありません。JPAエンティティの子アソシエーションを取得する方法

私がしようとしているのは、ManyToOneアソシエーションに子アソシエーションが存在し、それが異なる方法で処理されている場合です。

私はManyToOne関係を持っている子協会の名前を見つけることができますどのような方法があると私に教えてください

例:私は以下のように同様の方法が与えられます

//Parent Class 
public class Person 
{ 
    @OneToMany(cascade = CascadeType.PERSIST, mappedBy = "personName") 
    private List<FamilyName> familyNameList = null; 
} 

    // Child Class 
public class FamilyName 
{ 
    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST) 
    @JoinColumn(name = "PERSON_RID", referencedColumnName = "PERSONNAME_RID", nullable = false), 
    private PersonNameNonAggregates personName = null; 
} 

private void processEntity(Class<T> persistentClass){ 
// find child associations of the given persistent class and process 
} 

私には、子供の団体の名前を取得できることがありますか教えてください

+1

@ManyToOne注釈は実行時に保持されます。これらの注釈がある場合は、フィールド/メソッドをチェックすることができます。 – toongeorges

+0

@toongeorgesには、指定されたフィールドにManyToOneで注釈が付けられているかどうかを取得する方法があります。私に知らせてください – Maddy

+1

https://stackoverflow.com/questions/4296910/is-it-possible-to-read-the-value-of-a-annotation-in-java#4296964 – PCO

答えて

0

ここで現在のエンティティの関連付けを見つける手助けを探している人のサポートに感謝します。エンティティマネージャがメタモデルオブジェクトを持ち、現在のエンティティ属性を取得し、関連付けられているかどうかを調べる方法です

public Set<Attribute> fetchAssociates(){ 
    Set<Attribute> associations = new HashSet(); 
    Metamodel model = this.entityManager.getMetamodel(); 
     EntityType cEntity = model.entity(this.persistentClass); 
     System.out.println("Current Entity "+cEntity.getName()); 
     Set<Attribute> attributes =cEntity.getAttributes(); 
     for(Attribute att : attributes){ 
      if(att.isAssociation()){ 
       associations.add(att); 
      } 
     } 
    return associations; 
}  
関連する問題