使用保存的Spring Boot永远不会在Postgresql表内插入行 [英] Spring Boot using save never inserts a row inside of a Postgresql table

查看:110
本文介绍了使用保存的Spring Boot永远不会在Postgresql表内插入行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 问题摘要:

我无法在表中保存新条目。在决定尝试执行此任务之前,我遵循了一些教程,并且一切正常。我设法使用PostgreSQL https:/来完美地完成以下教程/www.callicoder.com/spring-boot-flyway-database-migration-example/

I cannot save a new entry inside of my table. I followed a few tutorials before I decided to try doing this task, and everything worked. I managed to do the following tutorial perfectly while also using PostgreSQL https://www.callicoder.com/spring-boot-flyway-database-migration-example/

当我同时使用WebController和RestController时发生了问题。使用controller.save()方法不会添加新行。
当我尝试运行它时,出现以下错误

The problem happened when I started using both a WebController and RestController. Using the controller.save() method isn't adding a new row. When I try to run it I get the following error

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,但它只是更新了最后一行。

Before anyone says it, I read somewhere that its bad practice to have both controllers in the same app but I have to do it this way because that is what my task requires me to do. I've tried researching around but I couldn't find anyone else with the same error. I even tried specifying an ID that is clearly not in use but that just updates the last row.

我尝试了使用这两个ID(obv不能同时使用时间)

I tried using both (obv not both at the same time)

@GeneratedValue(strategy = GenerationType.TABLE)

@GeneratedValue(strategy = GenerationType.AUTO)

它们都不改变结果


  1. 代码:

application.properties:

application.properties :

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控制器:

My 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:/";
}

我的DBController的插入函数被称为

My DBController whose insert function is being called"

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

I found some good news! I can insert into my database, as many rows as I want, but only if I don't insert anything using a data.sql file. The task requires me to do so though :(

推荐答案

有人帮助我找到了答案。我非常感谢。解决方案在这里
以前使用data.sql导入数据时,不能使用save()插入

Someone helped me get to my answer. I am so incredibly thankful. The solution is here. Cannot use save() to insert when previously importing data using data.sql

如果您不想去那里,只想修复它。对我来说,它只是更改了GenerationType,而不是将其更改为AUTO,TABLE或SEQUENCE,而是更改为IDENTITY。

If you dont want to go there and just want to fix it. What fixed it for me was just changing the GenerationType, not to AUTO or TABLE or SEQUENCE, but to IDENTITY.

这篇关于使用保存的Spring Boot永远不会在Postgresql表内插入行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆