In Springboot Java, I have these codes package com.vms.model.keys; import…
- User
In Springboot Java, I have these codes
package com.vms.model.keys;
import jakarta.persistence.Column; import jakarta.persistence.Embeddable; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data @Builder @NoArgsConstructor @AllArgsConstructor @Embeddable public class FormCompositeKey implements Serializable { @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(nullable = false) private long id;
@Column(nullable = false) private int revisionNo; }
package com.vms.model;
import com.vms.model.keys.FormCompositeKey; import jakarta.persistence.*; import lombok.*;
import java.util.ArrayList; import java.util.List; import java.util.Set;
@Data @Builder @NoArgsConstructor @AllArgsConstructor @Entity @Table(name = "form") public class Form {
@EmbeddedId private FormCompositeKey id;
@Column(nullable = false) private String name;
@Column(nullable = false) private String description;
@Column(name = "is_final", nullable = false) private boolean isFinal;
@ManyToMany @JoinTable(name = "form_account", joinColumns = { @JoinColumn(name = "form_id", referencedColumnName = "id"), @JoinColumn(name = "form_revisionNo", referencedColumnName = "revisionNo") }, inverseJoinColumns = @JoinColumn(name = "account_id")) private List authorizedAccounts;
@ManyToMany(mappedBy = "forms") private Set workflows; }
public void createForm(){ FormCompositeKey fck = new FormCompositeKey(); fck.setRevisionNo(0); Form form = Form.builder() .id(fck) .name("Untitled Form") .description("") .isFinal(false) .authorizedAccounts(new ArrayList<>()) .workflows(new HashSet<>()) .build(); formRepository.save(form); }
The FormCompositeKey id is not auto generating, every time i create a form, the id remains at 0
- Assistant
In the current code, the
idfield in theFormCompositeKeyclass is not generating a value because it has been annotated with@GeneratedValue(strategy = GenerationType.IDENTITY)which only works with primary keys that are single fields and are mapped to auto-increment columns in the database.Since
FormCompositeKeyis an embedded ID, you can try using the@GeneratedValue(strategy = GenerationType.AUTO)annotation instead. This strategy allows the persistence provider to choose the appropriate strategy for the underlying database.If you still face issues, you can try explicitly setting the value of the
idfield in theFormCompositeKeyclass before setting it in theFormobject. For example:scssFormCompositeKey fck = new FormCompositeKey(); fck.setId(1L); // Set a non-zero value fck.setRevisionNo(0);This should set a non-zero value for the
idfield in theFormCompositeKeyobject. - User
I changed the GenerationType to Auto, however, the id still does not autogenerate
- Assistant
If changing the
GenerationTypetoGenerationType.AUTOdid not solve the issue, then it is likely that the issue is caused by the fact thatFormCompositeKeyis an embedded ID.In this case, you need to use a different strategy to generate the
idfield. One such strategy is to use a sequence generator. Here's how you can modify your code to use a sequence generator:- Define a sequence generator in your
FormCompositeKeyclass:
less - Define a sequence generator in your