简体   繁体   English

多对多关系休眠

[英]ManyToMany relation Hibernate

Modelling my SpringBoot application, using Hibernate and PostgreSQL as database. 使用Hibernate和PostgreSQL作为数据库对SpringBoot应用程序进行建模。 Need help to properly establish relation ManyToMany. 需要帮助以正确建立关系ManyToMany。 Class News: 班级新闻:

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

And class HashTag: 和类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;
    }

When I try save news like this: 当我尝试像这样保存新闻时:

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);

I'm getting error: 我收到错误消息:

ERROR: null value in column "news_id" violates not-null constraint 错误:“ news_id”列中的空值违反了非空约束

Lets see what we have: 让我们看看我们有什么:

On the owning side of the nm relation: 在nm关系的拥有方面:

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

You do not provide the exclusion of null values because documentation specifies: 您没有提供null值的排除,因为文档指定:

boolean nullable() default true; boolean nullable()默认为true;

This is wrong for the relationtable as well as the id. 这对于关系表和ID都是错误的。 You must: 你必须:

  1. Set the ID to nullable=false . 将ID设置为nullable=false
  2. Set all JoinColumns of the JoinTable to nullable=false . 将JoinTable的所有JoinColumns设置为nullable=false

There is another problem: You make a insert and an update by newsService.save(news); 还有另一个问题:您通过newsService.save(news);进行插入和更新newsService.save(news); , you specify a Generator for the ID but have no Generator, this is ok in other databases who provide something like a nextval in Oracle. ,您可以为ID指定一个生成器,但没有生成器,在其他提供Oracle中的nextval类的数据库中也可以。 In PostGreSQL you really should use a sequence-field like this: 在PostGreSQL中,您确实应该使用如下的序列字段:

@Id
@Column(name = "hashtag_id", nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "HashTagIDGenerator")
@SequenceGenerator(name = "HashTagIDGenerator", sequenceName = "SEQ_HASHTAG_ID")

Have a good time. 玩的很开心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM