Interfaces with hibernate annotations
Asked Answered
I

2

5

i am wondering how i would be able to annotate an interface

@Entity
@Table(name = "FOLDER_TABLE")
public class Folder implements Serializable, Hierarchy {

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "folder_id", updatable = false, nullable = false)
   private int fId;

   @Column(name = "folder_name")
   private String folderName;

   @OneToMany(cascade = CascadeType.ALL)
   @JoinTable(name = "FOLDER_JOIN_FILE_INFORMATION_TABLE", joinColumns = 
{ @JoinColumn(name = "folder_id") }, inverseJoinColumns = 
{ @JoinColumn(name = "file_information_id") })
    private List< Hierarchy > fileInformation = new ArrayList< Hierarchy >();
}

above and below are 2 classes that implement an interface called Hierarchy, the folder class has a list of Hierarchyies being a folder or a fileinformation class

@Entity
@Table(name = "FILE_INFORMATION_TABLE")
public class FileInformation implements Serializable, Hierarchy {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "file_information_id", updatable = false, nullable = false)
  private int ieId;
  @Column (name = "location")
  private String location;
}

I have searched the web for someway to annotate or a workaround but I cannot map the interface which is simply this

public interface Hierarchy {

}

I get a mapping exception on the List of hierarchies with a folder but I don't know how to map the class correctly.

Inexistent answered 6/4, 2010 at 16:40 Comment(0)
Q
11

You can map interfaces in Hibernate, as part of inheritance hierarchies. This is quite surely possible with XML mapping, as it is described in Chapter 9 of the Hibernate reference, among others.

Annotation based mapping is a different story though. I am not so familiar with it, but Java Persistence with Hibernate includes examples for this too. Adapted to your case, it would look like

@MappedSuperclass
public interface Hierarchy {
}

@Entity
@Table(name = "FOLDER_TABLE")
public class Folder implements Serializable, Hierarchy { ... }

@Entity
@Table(name = "FILE_INFORMATION_TABLE")
public class FileInformation implements Serializable, Hierarchy { ... }

This mapping would use a table per concrete class with implicit polymorphism.

However, other sources suggest that annotation support for interfaces may not be working / stable yet:

So you may need to experiment, including changing your inheritance mapping strategy, maybe turning the interface into an abstract class (if it's possible - since a class can only extend a single base class)...

Quinone answered 6/4, 2010 at 21:29 Comment(2)
rite so , i realised as my folders will be able to have many sub folders and fileInformations i would need to use the composite pattern to help. i found this online theresearchkitchen.com/blog/archives/57 it shows how to map composite pattern using annotationsInexistent
also all i basically want is to persist a hierarchy(tree) structure. i f someone knows a tutorial out there, im finding it quite hard to understand the hibernate documentation.Inexistent
B
1

A little Googling turned up...

You can use interfaces internally but you can't map interfaces in hibernate, you have to map classes, regardless of whether you are using xml mapping or annotation mapping. hibernate is handling lifecycle of your persistent objects so it needs to know what class to instantiate so you need to provide this information to it... I am not even sure how what you suggesting would even look like? how would you provide implementation for given interface to hibernate at runtime to instantiate?

http://forum.springsource.org/showthread.php?t=67420

So it looks like you are out of luck.

Bairn answered 6/4, 2010 at 17:5 Comment(2)
@Bairn You should have read further... the same thread includes a post in the end where the OP tells he managed to resolve the issue.Ruthven
The document docs.jboss.org/hibernate/annotations/3.5/reference/en/pdf/… on its page 30 endorses with what you said.Underestimate

© 2022 - 2024 — McMap. All rights reserved.