Spring Data JPA cannot save entity into MySQL doc store- error: ids for this class must be manually assigned before calling save()
Hello,
My document store looks like this:
CREATE TABLE ATTACHMENT (
ATTACHMENT_ID varchar(32) GENERATED ALWAYS AS (json_unquote(json_extract(DOC,'$.attachment_id'))) STORED NOT NULL,
DOC json NOT NULL,
CONSTRAINT PK_ATTACHMENT_ATTACHMENT_ID PRIMARY KEY (ATTACHMENT_ID)
);
Note that my Attachment domain object (which will be the doc inserted) has an attachment_id field in it.
This is the entity I'm trying to insert (without the setters and getters):
@Entity(name = "Attachment")
@Table(name = "ATTACHMENT")
public class AttachmentEntity {
@Id
@Column(name = "ATTACHMENT_ID")
private String id;
@Convert(converter = JpaAttachmentConverterJson.class)
@Column(name = "doc", nullable = false)
private Attachment doc;
...
}
Here is the interface that implements JpaRepository:
public interface JpaAttachmentEntityRepository extends JpaRepository<AttachmentEntity, String> {
}
In another class I simply call the above repository's .save() method, passing in an AttachmentEntity, like so:
this.jpaAttachmentEntityRepository.save(attachmentEntity);
Here is the important part of the error message:
org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): uk.co.myapp.repository.jpa.AttachmentEntity
If I try to set the id field before calling .save then this error is returned: java.sql.SQLException: The value specified for generated column 'ATTACHMENT_ID' in table 'attachment' is not allowed.
Note: Since the ATTACHMENT_ID field is generated when a record is entered into the doc store and not auto incremented, I got rid of a @GeneratedValue annotation under @Id in the entity (keeping this did not work either).
How can I successfully insert such an entity?
Thank you in advance.
Subject
Views
Written By
Posted
Spring Data JPA cannot save entity into MySQL doc store- error: ids for this class must be manually assigned before calling save()
24795
November 15, 2016 10:54AM
2412
November 16, 2016 03:32AM
1617
November 16, 2016 06:49AM
Sorry, you can't reply to this topic. It has been closed.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion
of Oracle or any other party.