java - 表列未填充,是控制器还是连接表问题?
问题描述
拥有一个Java/Springboot/Thymeleaf/Hibernate/SQL
允许管理员发布新文章的应用程序。每篇文章都与特定主题相关联。在SQL
I 中有一个topic table
, anarticles table
和 ajoin table
包含articleIds
and 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);
}
解决方案
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.
推荐阅读
- twig - 如何在来自 Contentful 的富文本中呈现资产(图像)
- c# - 如何在 C# 的字段声明中获取自我的名称
- powershell - SharePoint SitePages WebPart 更新
- cakephp - 如何动态获取 html->link('title') 中的页面标题?
- python - 根据 IF 条件返回另一列的值
- javafx - 删除任何 HBox 子节点会在 JAVAFX 中引发 NullPointerException
- r - 如何使用具有多个输出的 R Shiny moduleServer
- angular - 将 Angular 8 迁移到 12:自定义 Webpack 配置无法使用 svg-sprite-loader 构建 SVG 精灵
- python - 无法在 TensorFlow 中加载检查点文件。(OSError:未找到文件签名)
- python - 让机器人响应对特定消息的反应 - discord.py