应用程序属性:spring.datasource.url=jdbc:postgresql://localhost:5432/flyway_demospring.datasource.username=bobspring.datasource.password=bob123spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialectspring.jpa.hibernate.ddl-auto=创建spring.datasource.initialization-mode=always我的具有 post 功能的 Web 控制器:@PostMapping("/post")公共字符串 insertJoke(JokeForm jokeForm) {int categoryid = jokeForm.getCategoryId();字符串内容 = jokeForm.getContent();databasController.insert(categoryid, content);返回重定向:/";}正在调用插入函数的我的 DBControllerpublic Joke insert(int categoryid, String content) {return jokeRepository.save(new Joke(categoryid, content));}我的大部分笑话数据类:@Entity公开课笑话{@Id@GeneratedValue(策略 = GenerationType.AUTO)@Column(columnDefinition = "serial")私人长ID;@NotNull@Column(name = "category_id_FK")私人长类别Id;@NotBlank私有字符串内容;@Column(columnDefinition = "整数默认 0")私人 int 喜欢 = 0;@Column(columnDefinition = "整数默认 0")私人 int 不喜欢 = 0;公共笑话(){}公共笑话(长类别ID,字符串内容){this.setCategoryid(categoryid);this.setContent(内容);}//id公共长 getId() {返回 this.id;}//id公共无效setId(长ID){this.id = id;}//类别标识公共长 getCategoryid() {返回 this.categoryId;}public void setCategoryid(long categoryid) {this.categoryId = categoryid;}//内容公共字符串 getContent() {返回 this.content;}公共无效集内容(字符串内容){this.content = 内容;}//喜欢公共 int getLikes() {返回 this.likes;}公共无效 setLikes(int likes) {this.likes = 喜欢;}//不喜欢公共 int getDislikes() {返回 this.dislikes;}public void setDislikes(int dislikes) {this.dislikes = 不喜欢;}}笑话库:@Repository公共接口 JokeRepository 扩展 JpaRepository{笑话 findById(long id);列表<笑话>findByCategoryid(int categoryid);} 解决方案 看来您只需将 GenerationType.AUTO 更改为 GenerationType.IDENTITY.这背后的原因是序列,如果您使用 AUTO,可能会不同步.因为然后 hibernate 使用它自己的序列而不是 postgres 在使用串行时创建的序列.I'm terribly sorry if I can't start another post which is connected to my previous one but my question is somewhat different.I noticed that I really can save new data in my database as long as I never added data to the database by using the line spring.datasource.initialization-mode=always in my application.properties and made a data.sql file with a few insert statements. Once I insert the data using that file, I can access the data and show it to the user, but I can't create any new data because I get the following errorERROR: duplicate key value violates unique constraint "joke_pkey"Detail: Key (id)=(1) already exists.Does anyone know how to help me with this? I'm doing an interview task and I am meant to first import data using the data.sql file and then later add some more data.The post with my code is here:Spring Boot using save never inserts a row inside of a Postgresql table EDIT - someone recommended adding my code here directly and saying what I've tried.I have tried to initialize the database with the application properties the way they are, then restarting the app but without the last line, and setting the spring.jpa.hibernate.ddl-auto to none. But even so, it didn't work. I genuinely expected it to work like that. Because if the table is empty and I fill it in using the functions I created, everything works like a charm, even after restarting the server (id keep the ring.jpa.hibernate.ddl-auto to none again to keep the data from being deleted)I have also tried simply changing the GenerationType.AUTO to GenerationType.TABLE strategy in my Joke class, but that didn't seem to change anything either.application.properties : spring.datasource.url=jdbc:postgresql://localhost:5432/flyway_demospring.datasource.username=bobspring.datasource.password=bob123spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialectspring.jpa.hibernate.ddl-auto=createspring.datasource.initialization-mode=alwaysMy Web Controller that has the post function:@PostMapping("/post")public String insertJoke(JokeForm jokeForm) { int categoryid = jokeForm.getCategoryId(); String content = jokeForm.getContent(); databasController.insert(categoryid, content); return "redirect:/";}My DBController whose insert function is being calledpublic Joke insert(int categoryid, String content) { return jokeRepository.save(new Joke(categoryid, content));}Most of my Joke data class: @Entitypublic class Joke {@Id@GeneratedValue(strategy = GenerationType.AUTO)@Column(columnDefinition = "serial")private Long id;@NotNull@Column(name = "category_id_FK")private long categoryId;@NotBlankprivate String content;@Column(columnDefinition = "integer default 0")private int likes = 0;@Column(columnDefinition = "integer default 0")private int dislikes = 0;public Joke() {}public Joke(long categoryid, String content) { this.setCategoryid(categoryid); this.setContent(content);}// idpublic Long getId() { return this.id;}// idpublic void setId(Long id) { this.id = id;}// categoryidpublic long getCategoryid() { return this.categoryId;}public void setCategoryid(long categoryid) { this.categoryId = categoryid;}// contentpublic String getContent() { return this.content;}public void setContent(String content) { this.content = content;}// likespublic int getLikes() { return this.likes;}public void setLikes(int likes) { this.likes = likes;}// dislikespublic int getDislikes() { return this.dislikes;}public void setDislikes(int dislikes) { this.dislikes = dislikes;}}Joke Repository:@Repositorypublic interface JokeRepository extends JpaRepository<Joke, Integer> { Joke findById(long id); List<Joke> findByCategoryid(int categoryid);} 解决方案 It seems that all you need to do is change GenerationType.AUTO to GenerationType.IDENTITY.Reason behind this is the sequence, which might be out of sync if you use AUTO. Because then hibernate uses its own sequence instead of the one postgres creates when using serial. 这篇关于之前使用data.sql导入数据时不能使用save()插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-22 22:19