如何获取 JPA 实体的子关联



我希望构建一个动态框架,其中提供了实体对象,我对当前的实体类型一无所知。

我正在尝试的是,如果存在任何子关联与 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;
}     

最新更新