首页 > 解决方案 > Spring Boot 使用 save 永远不会在 Postgresql 表中插入一行

问题描述

  1. 问题总结:

我无法在表格中保存新条目。在我决定尝试执行此任务之前,我遵循了一些教程,并且一切正常。我设法完美地完成了以下教程,同时还使用了 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
  1. 我试过的:

在有人说之前,我在某处读到将两个控制器放在同一个应用程序中是不好的做法,但我必须这样做,因为这是我的任务要求我做的。我试过四处研究,但我找不到其他人有同样的错误。我什至尝试指定一个明显未使用但仅更新最后一行的 ID。

我尝试使用两者(obv 不是同时使用)

@GeneratedValue(strategy = GenerationType.TABLE)

@GeneratedValue(strategy = GenerationType.AUTO)

他们都没有改变结果

  1. 代码:

应用程序属性:

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 文件插入任何内容。这项任务需要我这样做:(

标签: javapostgresqlspring-bootspring-data-jpa

解决方案


我有一个类似的案例。

我所做的是使用

@GeneratedValue(strategy = GenerationType.SEQUENCE

然后在我的 data.sql 文件中,我使用序列来添加数据。这增加了序列。

这适用于 postgreSQL:

INSERT INTO joke(id, categoryid, content )
VALUES (nextval('hibernate_sequence'), 1, 'joke');

推荐阅读