Home > Software engineering >  Inheritance with hibernate ManyToMany relation
Inheritance with hibernate ManyToMany relation

Time:09-21

I have a problem.

I am writing program which is connecting to database, where I cant change anything. I can only read data from it. So let's say I have three tables MOVIES, BOOKS, REVIEWDOCUMENT and two many to many tables MOVIES_REVIEWDOCUMENT plus BOOKS_REVIEWDOCUMENT.

Because I am using Spring Boot with Hibernate, I have written simple entities classes.

@Entity(name = "MOVIES")
@Table(name = "MOVIES")
public class Movies {

    @Id
    @Column(name = "ID")
    private Long id;
    @Column(name = "MOVIE_KEY")
    private String movieKey;
    @Column(name = "TYPE_ANIMATED")
    private String typeAnim;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "MOVIES_REVIEWDOCUMENTS",
            joinColumns = @JoinColumn(name = "MOVIE_KEY"),
            inverseJoinColumns = @JoinColumn(name = "DOCUMENT_KEY"))
    private List<ReviewDocuments> reviewDocuments;
}

@Entity(name = "BOOKS")
@Table(name = "BOOKS")
public class Books {

    @Id
    @Column(name = "ID")
    private Long id;
    @Column(name = "BOOK_KEY")
    private String bookKey;
    @Column(name = "TYPE_WRITTEN")
    private String typeWritten;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "BOOKS_REVIEWDOCUMENTS",
            joinColumns = @JoinColumn(name = "BOOK_KEY"),
            inverseJoinColumns = @JoinColumn(name = "DOCUMENT_KEY"))
    private List<ReviewDocuments> reviewDocuments;
}

@Entity(name = "REVIEWDOCUMENTS")
@Table(name = "REVIEWDOCUMENTS")
public class ReviewDocuments {

    @Id
    @Column(name = "OBJID")
    private Long objId;

    @ManyToMany(mappedBy = "reviewDocuments")
    private Set<Movies> movies;
    @ManyToMany(mappedBy = "reviewDocuments")
    private Set<Books> books;
}

And it is working pretty ok. But because as you can see MOVIES and BOOKS are almost indentical, only diffrence are those relational tables. I was wondering if I can somehow extract it to abstrac class.

Because what I need to do is to create service class which will iterate after books/movies and it's documents and do some operation on reviewDocuments. So easies way would be creating generic service for each of entities.

Example:

 public void extractData() throws IOException {
        Path tempDirectory = Files.createTempDirectory("zip_");
        tempDirectory.toFile().deleteOnExit();

        Set<Book> books = movieRepository.findByRidKey(extractionParameters.getWriteNumber());
        for (Book book :
                books) {
            for (ReviewDocuments documents :
                    book.getReviewDocuments()) {
                exportDataToFile(data);
            }

            directoryToZip(tempDirectory.toFile(), book.getId());
            FileUtils.cleanDirectory(tempDirectory.toFile());
        }
        FileUtils.deleteDirectory(tempDirectory.toFile());
    }

CodePudding user response:

I don‘t think you can use inheritance with multiple many-to-many tables.

You could however define a common interface and implement your service based on that interface.

  • Related