In Springboot Java, I have these codes package com.vms.model.keys; import…
Private research annotation
Sign in to add a private research label or note.
- 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;
code@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 {
code@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<Account> authorizedAccounts; @ManyToMany(mappedBy = "forms") private Set<Workflow> workflows;}
codepublic 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); } - Assistant
The issue you're facing is that the
FormCompositeKeyid is not being auto-generated as expected. This is because the@GeneratedValueannotation is used with theGenerationType.IDENTITYstrategy, which is typically used for auto-incrementing primary keys in databases that support this feature (like MySQL, PostgreSQL, etc.).However, in your case, the
FormCompositeKeyis an embeddable class, not an entity, and it doesn't have its own table in the database. Instead, it's embedded within theFormentity. Therefore, the auto-incrementing behavior won't work as expected. - User
I changed the GenerationType to Auto, however, the id still does not autogenerate
- Assistant
When you change the
GenerationTypetoAUTO, you're delegating the responsibility of generating the unique identifier to the underlying database. However, sinceFormCompositeKeyis an embeddable class and not an entity, it doesn't have its own table in the database. Therefore, the auto-generation feature won't work as expected.To resolve this issue, you can manually set the
idvalue for eachFormCompositeKeyinstance before saving theFormentity. Here's an updated version of yourcreateFormmethod:java
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.