Spring Boot and Derby

Viewed 41

i would like to ask you for hint about SpringBoot and Derby. I have developed one small private project in Spring Boot and as databe i use Derby. Unfortunatelly I have one problem with the saving a new record in database.

Entity for the table site_overview

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "site_overview")
public class SiteOverview {
   @Id
   @Column(updatable = false)
   protected Integer id;

   @OneToOne
   @JoinColumn(name = "ownerid", referencedColumnName = "id")
   protected User siteOwner;

   protected Date dateofcreation;
}

and Service for this table:

@Service
public class SiteOverviewService {

  public SiteOverviewDTO addNewSite(SiteOverview newSite) {
    SiteOverview savedEntity = siteOverviewRepository.save(newSite);
    return siteOverviewMapper.toDTO(savedEntity);
 }
}

If I call this method, ti is received following error message:

org.apache.derby.iapi.error.StandardException: Attempt to modify an identity column 'ID'.

It is of course logical, because I try to change the column with ID and this column has to be generated from DerbyDB. The question is >

How I can save the new record and say, please save all variable without ID?

thank you very much!!!

1 Answers

I have found this. In DB there is Column ID as

Id INT NOT NULL GENERATED ALWAYS AS IDENTITY

therefore in the class has to added:

@Id
@Column(updatable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected Integer id;

I hope that it is help you :)

Related