How to create many, many relationships with extra columns in jhipster?

Jipster does not support the creation of many, many relationships with additional fields.

What is the best way to create many different relationships with extra columns in jhispter? Should I create two one-to-many relationships with extra fields?

+4
source share
2 answers

You will need to do this manually. this post describes how: https://hellokoding.com/jpa-many-to-many-extra-columns-relationship-mapping-example-with-spring-boot-maven-and-mysql/

, @Antares42, Many-To-Many :

:

@Entity
public class Book{
private int id;
private String name;
private Set<BookPublisher> bookPublishers;

public Book() {
}

public Book(String name) {
    this.name = name;
    bookPublishers = new HashSet<>();
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@OneToMany(mappedBy = "book", cascade = CascadeType.ALL, orphanRemoval = true)
public Set<BookPublisher>   getBookPublishers() {
    return bookPublishers;
}

public void setBookPublishers(Set<BookPublisher> bookPublishers) {
    this.bookPublishers = bookPublishers;
}
}

:

@Entity
public class Publisher {
private int id;
private String name;
private Set<BookPublisher> bookPublishers;

public Publisher(){

}

public Publisher(String name){
    this.name = name;
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@OneToMany(mappedBy = "publisher")
public Set<BookPublisher> getBookPublishers() {
    return bookPublishers;
}

public void setBookPublishers(Set<BookPublisher> bookPublishers) {
    this.bookPublishers = bookPublishers;
}
}

:

@Entity
@Table(name = "book_publisher")
public class BookPublisher implements Serializable{
private Book book;
private Publisher publisher;
private Date publishedDate;

@Id
@ManyToOne
@JoinColumn(name = "book_id")
public Book getBook() {
    return book;
}

public void setBook(Book book) {
    this.book = book;
}

@Id
@ManyToOne
@JoinColumn(name = "publisher_id")
public Publisher getPublisher() {
    return publisher;
}

public void setPublisher(Publisher publisher) {
    this.publisher = publisher;
}

@Column(name = "published_date")
public Date getPublishedDate() {
    return publishedDate;
}

public void setPublishedDate(Date publishedDate) {
    this.publishedDate = publishedDate;
}
}

, _date

0

, , Movie, Rater . JDL script :

entity Movie { title String}
entity Rater { name String}
entity Rating { value Integer //the extra field}

relationship ManyToMany {
   Rating{rater(name)} to Rater,
   Rating{movie(title)} to Movie
}

.jdl , cmd

jhipster import-jdl file.jdl

0

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


All Articles