java - Spring Boot 使用 save 永远不会在 Postgresql 表中插入一行
问题描述
- 问题总结:
我无法在表格中保存新条目。在我决定尝试执行此任务之前,我遵循了一些教程,并且一切正常。我设法完美地完成了以下教程,同时还使用了 PostgreSQL https://www.callicoder.com/spring-boot-flyway-database-migration-example/
当我开始同时使用 WebController 和 RestController 时,问题就发生了。使用 controller.save() 方法不会添加新行。当我尝试运行它时,出现以下错误
ERROR: duplicate key value violates unique constraint "joke_pkey"
Detail: Key (id)=(1) already exists.
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: A different object with the same identifier value was already associated with the session : [com.example.joke4u.Joke#1]; nested exception is javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session : [com.example.joke4u.Joke#1]] with root cause
- 我试过的:
在有人说之前,我在某处读到将两个控制器放在同一个应用程序中是不好的做法,但我必须这样做,因为这是我的任务要求我做的。我试过四处研究,但我找不到其他人有同样的错误。我什至尝试指定一个明显未使用但仅更新最后一行的 ID。
我尝试使用两者(obv 不是同时使用)
@GeneratedValue(strategy = GenerationType.TABLE)
和
@GeneratedValue(strategy = GenerationType.AUTO)
他们都没有改变结果
- 代码:
应用程序属性:
spring.datasource.url=jdbc:postgresql://localhost:5432/flyway_demo
spring.datasource.username=bob
spring.datasource.password=bob123
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=create
spring.datasource.initialization-mode=always
我的具有发布功能的 Web 控制器:
@PostMapping("/post")
public String insertJoke(JokeForm jokeForm) {
int categoryid = jokeForm.getCategoryId();
String content = jokeForm.getContent();
databasController.insert(categoryid, content);
return "redirect:/";
}
我的插入函数被调用的 DBController"
public Joke insert(int categoryid, String content) {
return jokeRepository.save(new Joke(categoryid, content));
}
完整的笑话数据类:
@Entity
public class Joke {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(columnDefinition = "serial")
private long id;
@NotNull
private int categoryid;
@NotBlank
private String content;
@Column(columnDefinition = "integer default 0")
private int likes = 0;
@Column(columnDefinition = "integer default 0")
private int dislikes = 0;
public Joke() {
}
public Joke(int categoryid, String content) {
this.setCategoryid(categoryid);
this.setContent(content);
}
public Joke(long id, int categoryid, String content) {
this(categoryid, content);
this.id = id;
}
//id
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
// categoryid
public int getCategoryid() {
return this.categoryid;
}
public void setCategoryid(int categoryid) {
this.categoryid = categoryid;
}
// content
public String getContent() {
return this.content;
}
public void setContent(String content) {
this.content = content;
}
// likes
public int getLikes() {
return this.likes;
}
public void setLikes(int likes) {
this.likes = likes;
}
public void incrementLikes() {
++likes;
}
public void decrementLikes() {
--likes;
}
// dislikes
public int getDislikes() {
return this.dislikes;
}
public void setDislikes(int dislikes) {
this.dislikes = dislikes;
}
public void incrementDislikes() {
++dislikes;
}
public void decrementDislikes() {
--dislikes;
}
@Override
public String toString() {
return "{" + " id='" + getId() + "'" + ", categoryid='" + getCategoryid() + "'" + ", content='" + getContent()
+ "'" + ", likes='" + getLikes() + "'" + ", dislikes='" + getDislikes() + "'" + "}";
}}
笑话库:
@Repository
public interface JokeRepository extends JpaRepository<Joke, Integer> {
Joke findById(long id);
List<Joke> findByCategoryid(int categoryid);
}
编辑:我发现了一些好消息!我可以在我的数据库中插入任意多的行,但前提是我不使用 data.sql 文件插入任何内容。这项任务需要我这样做:(
解决方案
我有一个类似的案例。
我所做的是使用
@GeneratedValue(strategy = GenerationType.SEQUENCE
然后在我的 data.sql 文件中,我使用序列来添加数据。这增加了序列。
这适用于 postgreSQL:
INSERT INTO joke(id, categoryid, content )
VALUES (nextval('hibernate_sequence'), 1, 'joke');
推荐阅读
- xml - .xml URL,输出数据中没有任何格式
- angular - Ionic / Angular / FIrebase:“UserCredential”类型上不存在属性“emailVerified”
- php - 如果条件正确,如何插入数据
- angular - Angular 5 模板驱动的十进制输入
- sql-operations-studio - 如何获取 Azure Data Studio 的内部版本?
- python - Kivy微调器,限制显示项目的数量
- javascript - 刷新页面后如何保存用户输入
- c - C .data 初始化和 .bss 清除
- android - Toast$TN.mNextView 内存泄漏
- java - 从 EnumSet 中检索数字位值