[英]How to fetch child association for an JPA entity
我正在尋找一個動態框架,其中提供了和實體對象,而我對當前的實體類型一無所知。
我正在嘗試的是,如果存在與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
}
讓我知道我可以獲取任何兒童協會名稱嗎
伙計們感謝那些尋求幫助以查找當前實體關聯的人的支持,這是實體管理器具有元模型對象的一種方式,您可以檢索當前實體屬性,以及它是否為關聯
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.