Cannot use save() to insert when previously importing data using data.sql

SirBepy :

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 error

ERROR: 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_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

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

My DBController whose insert function is being called

public Joke insert(int categoryid, String content) {
    return jokeRepository.save(new Joke(categoryid, content));
}

Most of my Joke data class:

@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;
}

}

Joke Repository:

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

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=366637&siteId=1