首页 > 解决方案 > 表列未填充,是控制器还是连接表问题?

问题描述

拥有一个Java/Springboot/Thymeleaf/Hibernate/SQL允许管理员发布新文章的应用程序。每篇文章都与特定主题相关联。在SQLI 中有一个topic table, anarticles table和 ajoin table包含articleIdsand topicsIds

当我通过应用程序添加新文章时,"Topic"文章表中的列应该填充从下拉字段中选择的任何主题名称,您可以选择多个主题,因此理想情况下该列需要收集所有选定的主题名字。

您认为这是我的sql表格的问题,还是我需要在控制器中以某种方式设置选定的主题?我已经尝试在控制器中设置它,但不确定我是否正确抓住它,因为可以"topic"从主题中选择多个......有什么想法吗?

这是我的文章模型代码。

@Entity
@Table(name="Article")
@EntityListeners(AuditingEntityListener.class)
public class Article {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="ArticleId")
    private Long id;

    @LastModifiedDate
    @DateTimeFormat(pattern = "MM/dd/yyyy HH:mm a")
    private LocalDateTime lastModifiedDate;

    private String title;
    private String urlTitle;
    private String contentHtml;
    private String contentText;
    private String topic;
    @ManyToMany(fetch = FetchType.LAZY,
            cascade = {
                    CascadeType.PERSIST,
                    CascadeType.MERGE
            })
    @JoinTable(name = "Article_Topics",
            joinColumns = { @JoinColumn(name = "Article") },
            inverseJoinColumns = { @JoinColumn(name = "Topic") })
    private Set<Topic> topics= new HashSet<>();

    public Long getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getUrlTitle() {
        return urlTitle;
    }

    public void setUrlTitle(String urlTitle) {
        this.urlTitle = urlTitle;
    }

    public String getContentHtml() {
        return contentHtml;
    }

    public void setContentHtml(String contentHtml) {
        this.contentHtml = contentHtml;
    }

    public String getContentText() {
        return contentText;
    }

    public void setContentText(String contentText) {
        this.contentText = contentText;
    }

    public Set<Topic> getTopics() {
        return topics;
    }

    public void setTopics(Set<Topic> topics) {
        this.topics = topics;
    }

    public String getTopic() {
        return topic;
    }

    public void setTopic(String topic) {
        this.topic = topic;
    }

    public LocalDateTime getLastModifiedDate() {
        return lastModifiedDate;
    }

    public void setLastModifiedDate(LocalDateTime lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }
}

这是控制器的代码,当您保存新主题时:

@RequestMapping(value="/save-article")
    public String addArticle(Model model, @ModelAttribute(value="article")Article newArticle, @ModelAttribute(value="topics")Article topics){
        String newArticleName = newArticle.getTitle().replaceAll("\\s","");
        newArticle.setUrlTitle(newArticleName);

        articleRepository.save(newArticle);
        return adminHome(model);
    }

标签: javasqlhibernatespring-boot

解决方案


You should not have a topic column inside the Article table. As per your explanation, you have a "ManyToMany" scenario where one article can have many topics and one topic can be associated with many articles.

To fulfil this requirement you have already created a join table, then I don't understand why you have created the topic column again in Article table. To explain it simply, you cannot have multiple data in one field. So if one article has 3 topics, how can you put all 3 in the same column.

I recommend you to go through this link to get a proper understanding of Many to Many cases and how to handle them in hibernate.


推荐阅读