如何获取 JPA 实体的子关联
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;
}
我希望构建一个提供实体对象的动态框架,但我对当前的实体类型一无所知。
我正在尝试的是,如果 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;
}