Interfaces with Sleep Annotations

I am wondering how I could comment on the 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 the Hierarchy interface, the folder class has a list of Hierarchyies, which is a folder or file 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 launched the website for any comment or workaround, but I cannot map the interface which is just this

 public interface Hierarchy { } 

I get output about displaying a hierarchy with a folder in the list, but I don't know how to display the class correctly

+4
source share
2 answers

You can map interfaces in Hibernate as part of the inheritance hierarchy. This is entirely possible using XML mapping, as described in chapter 9 of the Hibernate help system , among others.

Annotation-based rendering is a completely different story. I am not so familiar with this, but Java Persistence with Hibernate contains examples for this. Adapted to your case, it will 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 will use a table for a particular class with implicit polymorphism.

However, other sources suggest that support for annotations for interfaces may not yet work / stabilize:

Thus, you may need to experiment, including changing the inheritance matching strategy, perhaps turning the interface into an abstract class (if possible - since the class can extend only one base class) ...

+9
source

Little Googling appeared ...

You can use external interfaces, but you cannot display interfaces in sleep mode , you need to map classes, regardless of whether you use xml mapping or annotation mapping. hibernation is processing the life cycle of your constant objects, so it needs to know which class to instantiate, so you need to provide this information to it ... I don’t even see how you suggest it will even look like? how would you provide an implementation for a given interface for hibernation at run time instance?

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

So it seems you are out of luck.

+1
source

Source: https://habr.com/ru/post/1306142/


All Articles