简体   繁体   English

Hibernate ManyToMany和超类映射问题

[英]Hibernate ManyToMany and superclass mapping problem

I need to create a relation in Hibernate, linking three tables: Survey, User and Group. 我需要在Hibernate中创建一个关系,链接三个表:Survey,User和Group。 The Survey can be visible to a User or to a Group, and a Group is form of several Users. 用户或组可以看到该调查,并且组是几个用户的形式。

My idea was to create a superclass for User and Group, and create a ManyToMany relationship between that superclass and Survey. 我的想法是为User和Group创建一个超类,并在该超类和Survey之间创建ManyToMany关系。

My problem is that Group, is not map to a table, but to a view, so I can't split the fields of Group among several tables -which would happen if I created a common superclass-. 我的问题是Group不是映射到表,而是映射到视图,因此我无法在多个表之间拆分Group的字段-如果创建一个公共的超类会发生这种情况。

I thought about creating a common interface, but mapping to them is not allowed. 我考虑过创建一个通用接口,但是不允许映射到它们。 I will probably end up going for a two relations solution (Survey-User and Survey-Group), but I don't like too much that approach. 我可能最终会选择两个关系解决方案(调查用户和调查组),但是我不太喜欢这种方法。

I thought as well about creating a table that would look like: 我也想过要创建一个看起来像这样的表:

  Survey Id  |  ElementId  | Type

ElementId would be the Group or UserId, and the type... the type of it. ElementId是Group或UserId,以及类型...类型。 Does anyone know how to achieve it using hibernate annotations? 有谁知道如何使用休眠注释实现它? Any other ideas? 还有其他想法吗?

Thanks a lot 非常感谢

I posted a very similar answer yesterday. 我昨天发布了一个非常相似的答案 To summarize, you can't use a mapped superclass because a mapped superclass is not an entity and can't be part of an association (which is what you want) but you can use an abstract Entity with a TABLE_PER_CLASS inheritance strategy to obtain a similar result. 总而言之,您不能使用映射的超类,因为映射的超类不是实体,并且不能成为关联的一部分(这就是您想要的),但是可以将抽象的Entity与TABLE_PER_CLASS继承策略结合使用,以获取类似的结果。

Something like this (not tested): 像这样的东西(未经测试):

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class AbstractEntity {
    @Id @GeneratedValue(strategy = GenerationType.TABLE)
    private Long id;

    @ManyToMany(mappedBy="entities")
    private Set<Survey> surveys = new HashSet<Survey>();
    ...

}

@Entity
public class User extends AbstractEntity {
    ...
}

@Entity
public class Group extends AbstractEntity {
    ...
}

@Entity
public class Survey {
    @Id @GeneratedValue
    private Long id;

    @ManyToMany
    private Set<AbstractEntity> entities = new HashSet<AbstractEntity>();

    ...
}

References 参考文献

You can use the table per concrete class inheritance strategy, hibernate will replicate all properties for each subclass, this will work with a view. 您可以使用每个具体类继承策略的表,hibernate将复制每个子类的所有属性,这将与视图一起使用。

I would also suggest the composite pattern for users/groups (which is close to your first option). 我还将建议用户/组的复合模式(接近您的第一个选择)。

http://en.wikipedia.org/wiki/Composite_pattern http://en.wikipedia.org/wiki/Composite_pattern

This is possible. 这个有可能。 Such an 'inherited properties' approach can be achieved by defining the superclass as a MappedSuperclass . 通过将超类定义为MappedSuperclass可以实现这种“继承的属性”方法。

EDIT: 编辑:

There is also some alternatives listed in section 2.2.4 in the hibernate annotations reference doc , section 2.2.4.4 covers MappedSuperclass. 在hibernate注释参考文档的2.2.4节中还列出了一些替代方法,第2.2.4.4节介绍了MappedSuperclass。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM