The following problem accures:
I start my Springboot-application and Flyway executes the following SQL-script:
CREATE TABLE speiseplan(
ID SERIAL PRIMARY KEY,
NAME varchar(50) NOT NULL,
PREIS numeric(7,1),
ART int,
WOCHENTAG integer,
CONSTRAINT fk_wochentage FOREIGN KEY(WOCHENTAG) REFERENCES wochentage(ID),
CONSTRAINT fk_speisenart FOREIGN KEY(ART) REFERENCES speisenart(ID)
);
When trying to add a new object (SpeiseplanEntity):
@Entity
@Table(name = "speiseplan")
public class SpeiseplanEntity {
@Id
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "preis")
private BigDecimal preis;
@Column(name = "art")
private String art;
protected SpeiseplanEntity(){}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getPreis() {
return preis;
}
public void setPreis(BigDecimal preis) {
this.preis = preis;
}
public String getArt() {
return art;
}
public void setArt(String art) {
this.art = art;
}
@Override
public String toString() {
return "SpeiseplanEntity{" +
"id=" + id +
", name='" + name + ''' +
", preis=" + preis +
", art='" + art + ''' +
'}';
}
}
to the Database, i have to manuelly type out the ID of the object, like so:
POST http://localhost:8080/api/speise/createSpeise
Content-Type: application/json
{
"id": 1,
"name": "Test2",
"preis": "13.5",
"art": "A"
}
If I don’t, i get the following error:
Identifier of entity '... .SpeiseplanEntity' must be manually assigned before calling 'persist()'
I expected the ID to automatically increase, so that I wouldn’t have to give an ID for every single object, that I would add in the future.
Moritz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.