不能使用保存()之前使用data.sql导入数据时插入

SirBepy:

我非常抱歉,如果我不能启动连接到我以前的一个另一篇文章,但我的问题是有些不同。

我注意到,我真的可以在我的数据库,只要保存新的数据,因为我从来没有使用线数据添加到数据库spring.datasource.initialization-mode=always中我application.properties,并提出了data.sql文件与几个插入语句。有一次,我插入使用该文件,我可以访问数据并显示给用户,而是因为我碰到下面的错误,我不能创造任何新数据的数据

ERROR: duplicate key value violates unique constraint "joke_pkey"
Detail: Key (id)=(1) already exists.

没有人知道如何帮助我?我做一个采访任务,我打算使用data.sql文件首次进口数据,然后在以后添加更多的数据。

我的代码后是在这里:春天开机使用save从来没有插入PostgreSQL表的行内

编辑 -有人在这里推荐直接将我的代码,说什么我已经试过。

我曾尝试与应用程序属性初始化数据库会是这样的,然后重新启动应用程序,但没有最后一行,并设置spring.jpa.hibernate.ddl-autonone但即便如此,也没有工作。我真的希望它这样的工作。因为如果表是空的,我甚至重新启动服务器后,填充在使用我创造的功能,一切工作就像一个魅力,(ID保持ring.jpa.hibernate.ddl-autonone再次被删除保持数据)

我也曾尝试简单的改变GenerationType.AUTO,以GenerationType.TABLE战略我的笑话类,但似乎任何改变都没有。

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

我的网络控制器,具有POST功能:

@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.AUTO)
@Column(columnDefinition = "serial")
private Long id;

@NotNull
@Column(name = "category_id_FK")
private long 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(long categoryid, String content) {
    this.setCategoryid(categoryid);
    this.setContent(content);
}

// id
public Long getId() {
    return this.id;
}

// id
public void setId(Long id) {
    this.id = id;
}

// categoryid
public long getCategoryid() {
    return this.categoryId;
}

public void setCategoryid(long 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;
}

// dislikes
public int getDislikes() {
    return this.dislikes;
}

public void setDislikes(int dislikes) {
    this.dislikes = dislikes;
}

}

笑话库:

@Repository
public interface JokeRepository extends JpaRepository<Joke, Integer> {
   Joke findById(long id);
   List<Joke> findByCategoryid(int categoryid);
}
phaen:

看来,所有你需要做的是改变GenerationType.AUTO到GenerationType.IDENTITY。

这背后的原因是序列,如果使用AUTO这可能是不同步。因为那时Hibernate使用它自己的序列,而不是一个Postgres的使用串行时造成的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章