Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Hibernate @Any-related annotations?

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.

like image 524
Henrik Paul Avatar asked Oct 20 '08 09:10

Henrik Paul


People also ask

What is @basic annotation in hibernate?

We can use the @Basic annotation to mark a basic type property: @Entity public class Course { @Basic @Id private int id; @Basic private String name; ... } In other words, the @Basic annotation on a field or a property signifies that it's a basic type and Hibernate should use the standard mapping for its persistence.

What is JPA annotations in hibernate?

JPA annotations are used in mapping java objects to the database tables, columns etc. Hibernate is the most popular implement of JPA specification and provides some additional annotations. Today we will look into JPA annotations as well as Hibernate annotations with brief code snippets.


1 Answers

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;     }  } 
like image 188
Jorge Ferreira Avatar answered Sep 22 '22 10:09

Jorge Ferreira