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

问题描述

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

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

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

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


解决方案

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

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 @任何相关的注解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 16:53