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

时间:2016-01-29 21:36:15

标签: java hibernate many-to-many jhipster

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

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

3 个答案:

答案 0 :(得分:0)

您必须手动完成。 这篇文章描述了如何: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;
}
}

secound entity:

@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;
}
}

此实体描述Book和Publisher之间的关系,额外字段为published_date

答案 1 :(得分:0)

假设您有像Movie,Rater这样的实体,并且需要加入表评级。您可以编写如下的JDL脚本:

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
}

将其保存在项目文件夹的file.jdl中,打开cmd type

jhipster import-jdl file.jdl

你拥有一切

答案 2 :(得分:0)

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

entity Foo{
    ...
}

entity Bar{
    ...
}

entity FooBarAssociation{
    extraProperty1 String
    extraProperty2 String
    ...
}

relationship ManyToOne {
  FooBarAssociation{foo} to Foo{bars}
  FooBarAssociation{bar} to Bar{foos}
}