如何在jhipster中创建与多列相关的多对多关系?

Vít*_*ega 7 java many-to-many hibernate jhipster

jhipster不支持使用额外字段创建多对多关系.

在jhispter中创建多对多额外列的最佳方法是什么?我应该与额外的字段创建两个一对多的关系吗?

Jes*_*sta 9

使用 JHipster 域语言 (JDL),可以使用关联实体和两个多对一关系轻松实现持有额外属性(列)的 @ManytoMany。见下文:

entity Foo{
    ...
}

entity Bar{
    ...
}

entity FooBarAssociation{
    extraProperty1 String
    extraProperty2 String
    ...
}

relationship ManyToOne {
  FooBarAssociation{foo} to Foo{bars}
  FooBarAssociation{bar} to Bar{foos}
}
Run Code Online (Sandbox Code Playgroud)


nav*_*eni 0

您必须手动执行此操作。这篇文章描述了如何: https: //hellokoding.com/jpa-many-to-many-extra-columns-relationship-mapping-example-with-spring-boot-maven-and-mysql/

一般来说,正如@Antares42所说,您应该为多对多表创建一个实体,如下所示:

第一个实体:

@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;
}
}
Run Code Online (Sandbox Code Playgroud)

第二个实体:

@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;
}
}
Run Code Online (Sandbox Code Playgroud)

连接表实体:

@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;
}
}
Run Code Online (Sandbox Code Playgroud)

该实体描述了Book和Publisher之间的关系,额外的字段是published_date