如何使用Hibernate @任何相关的注解? [英] How to use Hibernate @Any-related annotations?

查看:355
本文介绍了如何使用Hibernate @任何相关的注解?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能向我解释如何任何相关的注解( @Any @AnyMetaDef ,<$ C $在实践中C> @AnyMetaDefs 和 @ManyToAny )的工作。我有一个很难找到任何有用的文档(单独的JavaDoc并不是很有益的)这些。

我迄今收集到的他们以某种方式使引用抽象和扩展类。如果是这样的话,为什么会出现不是一个 @OneToAny 标注?而就是这个任意指的是一个单一的'任何',或多个'任何'?

一个短,实用,明实施将非常AP preciated(不必编译)。

编辑:,就像我愿意接受答覆答案,并给予信贷,因为,我发现无论Smink的和之鱼的回答信息。因为我不能接受一些答复为答案的,我会很遗憾标记既不答案。


解决方案

希望这文章带来了一些光主题:


  

有时,我们需要一个映射
  关联属性不同
  类型的实体不具有一
  共同的祖先实体 - 这样一个简单
  多态关联不做
  的工作。


例如假设它管理媒体库中的三个不同的应用程序 - 第一个应用程序管理借书,第二个DVD和第三VHSs。这些应用程序没有任何共同之处。现在,我们要发展,管理三种媒体类型和重用退出图书,DVD,VHS和实体的新应用程序。由于图书,DVD,VHS和班来自不同应用的来了,他们没有任何的祖先实体 - 共同的祖先是java.lang.Object继承。然而,我们希望有可以指任何可能的媒体类型之一借实体。<​​/ P>

要解决这种类型的引用,我们可以使用任何映射。此映射总是包括一个以上的列:一列包括实体的类型的当前映射属性是指与其他包括的实体的身份,例如,如果我们指的是书它的第一列将包括标记Book实体类型,第二个将包括具体的书的id。

  @Entity
@Table(NAME =借)
公共类借用{    @ID
    @GeneratedValue
    私人长期身份证;    @Any(metaColumn = @Column(NAME =ITEM_TYPE))
    @AnyMetaDef(idType =长,元类型=字符串,
            metaValues​​ = {
             @MetaValue(targetEntity = Book.class,值=B),
             @MetaValue(targetEntity = VHS.class,值=V),
             @MetaValue(targetEntity = DVD.class,值=D)
       })
    @JoinColumn(NAME =ITEM_ID)
    私有对象的项目;     .......
    公共对象的getItem(){
        归还物品;
    }    公共无效setItem(对象项){
        this.item =项目;
    }}

Could someone explain to me how Any-related annotations (@Any, @AnyMetaDef, @AnyMetaDefs and @ManyToAny) work in practice. I have a hard time finding any useful documentation (JavaDoc alone isn't very helpful) about these.

I have thus far gathered that they somehow enable referencing to abstract and extended classes. If this is the case, why is there not an @OneToAny annotation? And is this 'any' referring to a single 'any', or multiple 'any'?

A short, practical and illustrating example would be very much appreciated (doesn't have to compile).

Edit: as much as I would like to accept replies as answers and give credit where due, I found both Smink's and Sakana's answers informative. Because I can't accept several replies as the answer, I will unfortunately mark neither as the answer.

解决方案

Hope this article brings some light to the subject:

Sometimes we need to map an association property to different types of entities that don't have a common ancestor entity - so a plain polymorphic association doesn't do the work.

For example let's assume three different applications which manage a media library - the first application manages books borrowing, the second one DVDs, and the third VHSs. The applications have nothing in common. Now we want to develop a new application that manages all three media types and reuses the exiting Book, DVD, and VHS entities. Since Book, DVD, and VHS classes came from different applications they don't have any ancestor entity - the common ancestor is java.lang.Object. Still we would like to have one Borrow entity which can refer to any of the possible media type.

To solve this type of references we can use the any mapping. this mapping always includes more than one column: one column includes the type of the entity the current mapped property refers to and the other includes the identity of the entity, for example if we refer to a book it the first column will include a marker for the Book entity type and the second one will include the id of the specific book.

@Entity
@Table(name = "BORROW")
public class Borrow{

    @Id
    @GeneratedValue
    private Long id;

    @Any(metaColumn = @Column(name = "ITEM_TYPE"))
    @AnyMetaDef(idType = "long", metaType = "string", 
            metaValues = { 
             @MetaValue(targetEntity = Book.class, value = "B"),
             @MetaValue(targetEntity = VHS.class, value = "V"),
             @MetaValue(targetEntity = DVD.class, value = "D")
       })
    @JoinColumn(name="ITEM_ID")
    private Object item;

     .......
    public Object getItem() {
        return item;
    }

    public void setItem(Object item) {
        this.item = item;
    }

}

这篇关于如何使用Hibernate @任何相关的注解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆