[英]Hibernate annotation to a transient collection field
I would like a collection field to use a query, but I cannot find a way to do it. 我希望使用收集字段来查询,但是我找不到解决方法。 There is a @Formula
annotation, but that doesn't work with collections. 有一个@Formula
批注,但不适用于集合。 You might ask why I just don't use @ManyToMany
or similar annotation. 您可能会问,为什么我不使用@ManyToMany
或类似的注释。 The thing is, I have a table (say, called Relationships) with columns similar to this: 问题是,我有一个表(例如,称为“关系”),其中的列与此类似:
ID | ChildType | ChildID | ParentType | ParentID |
Now, the ChildType and ParentType can be one of many classes in my application, therefore I cannot (I don't want to) use @ManyToMany
mapping, since it will create another table for each class. 现在,ChildType和ParentType可以是我的应用程序中许多类之一,因此我不能(我不想)使用@ManyToMany
映射,因为它将为每个类创建另一个表。 What I would like is, to have a collection field called relationships in, say, class A, that would select all rows (relationships) from Relationship table, where ChildID = A.id and ChildType = 'A'
. 我想要的是在类A中有一个称为“关系”的收集字段,该字段将从“关系”表中选择所有行(“关系”), where ChildID = A.id and ChildType = 'A'
。 Is that possible? 那可能吗?
Update: Looks like I haven't made myself clear enough. 更新:看来我还不够清楚。 Ok, say, I have a class Relationship where ParentType
and ChildType
are just class names of parent and child as a String: 好吧,说,我有一个类Relationship,其中ParentType
和ChildType
只是作为字符串的parent和child的类名称:
@Entity
public class Relationship {
@Id
@GeneratedValue
private Long id;
@Enumerated(EnumType.STRING)
private ParentType parentType;
private Long parentId;
@Enumerated(EnumType.STRING)
private ParentType childType;
private Long childId;
}
Then I have all kinds of classes that have a field relationships
, say: 然后,我有各种具有字段relationships
的类,例如:
@Entity
public class A {
@Id
@GeneratedValue
private Long id;
private Set<Relationship> relationships = new HashSet<Relationship>();
}
@Entity
public class B {
@Id
@GeneratedValue
private Long id;
private Set<Relationship> relationships = new HashSet<Relationship>();
}
@Entity
public class C {
@Id
@GeneratedValue
private Long id;
private Set<Relationship> relationships = new HashSet<Relationship>();
}
Now I want Hibernate to query the Relationships
table to find the relationships for each class, A
, B
, and C
whenever I get an instance bean of corresponding class. 现在,我希望Hibernate每当获得对应类的实例bean时,就可以查询Relationships
表来查找每个类A
, B
和C
的关系。 Something like this: 像这样:
@Query("(select relationship from Relationship relationship where relationship.childId = id and relationship.childType = 'A')")
private Set<Relationship> relationships = new ArrayList<Relationship>();
Considering relation_ship is your Relationship table and above described table is parent_child 考虑Relation_ship是您的Relationship表,而上述表是parent_child
SQLQuery query = session.createSQLQuery(
"SELECT r
FROM relation_ship r
INNER JOIN
parent_child p
ON r.id = p.ChildID and
p.ChildType := ChildType").setParameter("ChildType","A");
List<Relationship> = query.list();
I did a workaround for now, but I would still like to know if there is a way to add an annotation in my Entity class instead. 我现在做了一个解决方法,但是我仍然想知道是否有一种方法可以在我的Entity类中添加注释。
Workaround is as follows: Add a method in the Relationship's repository interface, that extends JpaRepository
. 解决方法如下:在Relationship的存储库界面中添加一个方法,该方法扩展了JpaRepository
。
public List<Relationship> findByChildIdAndChildType(Long childId,
String childType);
Then, in every service class for A, B, C entities, add relationship resource, and edit find
and findAll
or whatever methods by adding code that sets the relationships field. 然后,在A,B,C实体的每个服务类中,添加关系资源,并通过添加设置关系字段的代码来编辑find
和findAll
或任何方法。
@Service
@Transactional
public class AService implements IAService {
@Resource
private IARepository aRepository;
@Resource
IRelationshipRepository relationshipRepository;
@Override
public A findOne(Long id) {
A a = aRepository.findOne(id);
List<Relationship> relationships = new ArrayList<Relationship>();
relationships =
relationshipRepository.findByChildIdAndChildType(id, 'A');
a.setRelationships(relationships);
return a;
}
// other fields and methods omitted..
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.