Modellierung meiner SpringBoot-Anwendung mit Hibernate und PostgreSQL als Datenbank. Brauchen Sie Hilfe, um Beziehung ManyToMany richtig herzustellen. Klasse News:ManyToMany-Beziehung Hibernate
@Entity(name = "news")
public class News implements Serializable {
private Long id;
private Date date;
private String text;
private String author;
private Set<HashTag> hashTags = new HashSet<HashTag>(0);
private byte[] image;
public News() {
}
public News(Date date, String text, String author, Set<HashTag> hashTags, byte[] image) {
this.date = date;
this.text = text;
this.author = author;
this.hashTags = hashTags;
this.image = image;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "news_id")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Temporal(TemporalType.DATE)
@DateTimeFormat(pattern = "DD/MM/YYYY")
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Lob
@Type(type = "org.hibernate.type.TextType")
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "news_hashTag",
joinColumns = @JoinColumn(name = "news_id"),
inverseJoinColumns = @JoinColumn(name = "hashtag_id"))
public Set<HashTag> getHashTags() {
return hashTags;
}
public void setHashTags(Set<HashTag> hashTags) {
this.hashTags = hashTags;
}
@Lob
public byte[] getImage() {
return image;
}
public void setImage(byte[] image) {
this.image = image;
}
Und Klasse Hashtag:
@Entity(name = "hashTag")
public class HashTag implements Serializable {
private Long id;
private String name;
private String description;
private Set<News> news = new HashSet<News>(0);
public HashTag() {
}
public HashTag(String name, String description) {
this.name = name;
this.description = description;
}
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
@Column(name = "hashtag_id")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToMany(cascade = CascadeType.ALL, mappedBy = "hashTags")
public Set<News> getNews() {
return news;
}
public void setNews(Set<News> news) {
this.news = news;
}
Wenn ich Nachrichten versuchen, speichern wie folgt aus:
Set<HashTag> hashTags = new HashSet<>();
hashTags.add(new HashTag("HashTag 1");
hashTags.add(new HashTag("HashTag 2");
News news = new News();
news.text = "Some text";
news.author = "Some author";
news.date = new Date();
news.hashTags = hashTags;
newsService.save(news);
Ich erhalte Fehler:
ERROR: null value in column "news_id" violates not-null constraint
Sie könnten versuchen 'hashTags.forEach (h -> setNews (news));' - mit anderen Worten, geben Sie den HashTags einen Verweis auf ihr Nachrichtenobjekt, das dann die news_id-Spalte füllt. –
Versucht, nichts geändert:/ – zzheads
@zzheads Was für ein Durcheinander, die Frage ist nicht mehr echt. Ich muss die Frage in den echten Zustand ändern. –