I’m trying to insert an entry into my database with a one-to-one relationship to another entity. I’m using spring data jpa and postgres.
My code looks as follows:
@MappedSuperclass
@Getter
@Setter
@ToString
public abstract class Post {
@Id
@GeneratedValue
private Long id;
private Long botId;
private String description;
private boolean hasBeenUsed;
@Column(length = 1000)
private String imageUrl;
@CreationTimestamp
@Column(nullable = false, updatable = false)
private LocalDateTime createdAt;
}
@Entity
@Getter
@Setter
@ToString
public class PollPost extends Post {
@OneToOne(cascade = CascadeType.ALL)
private PollPostImageTextData pollImageTextData;
}
@Entity
@Getter
@Setter
@ToString
public class PollPostImageTextData {
@Id
@GeneratedValue
private Long id;
private String question;
private String answerA;
private String answerB;
private String correctAnswer;
}
If I now try to create a PollPost as follows:
protected PollPost createPollPost(long botId) throws IOException {
System.out.println("Generating Poll Post...");
String textData = "{n "question": "sample question?",n "answerA": "sample answerA",n "answerB": "samepleAnswerB",n "correctAnswer": "answerB"n}";
String description = "sample desc";
ObjectMapper mapper = new ObjectMapper();
PollPostImageTextData pollImageTextData = new PollPostImageTextData();
try {
System.out.println("Generating image text data...");
pollImageTextData = mapper.readValue(textData, PollPostImageTextData.class);
System.out.println(pollImageTextData);
} catch (Exception e) {
e.printStackTrace();
}
if (pollImageTextData == null) {
throw new IllegalStateException("Poll Image Text Data must not be null");
}
String imageUrl = "test url";
PollPost post = new PollPost();
post.setPollImageTextData(pollImageTextData);
post.setDescription(description);
post.setHasBeenUsed(false);
post.setBotId(botId);
post.setImageUrl(imageUrl);
System.out.println("Saving PollPost with PollImageTextData ID: " + (pollImageTextData.getId() != null ? pollImageTextData.getId() : "null"));
pollPostRepository.save(post);
return post;
}
I get the error: org.springframework.dao.DataIntegrityViolationException: could not execute statement [ERROR: insert or update on table "poll_post" violates foreign key constraint "fk1db3khk0i1jc6mehnod8fy4r" Detail: Key (poll_image_text_data_id)=(852) is not present in table "poll_image_text_data".
I’m not sure why this occurs, since the @OneToOne(cascade = CascadeType.ALL)
annotation of the PollImageTextData should create the necessary entry in the database. Thanks in advance.
Binaryenchanter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1