How to use EmbeddedId with GeneratedValue in terms of Spring

Hello.

Not quite sure if this forum can help in questions, which have a Spring origin, but I’m willing to give it a try.

I’m trying to achieve a composite key functionality, which is a regular Integer (field “id”) and a Many-to-one (field “secondId”).

The thing I’m tripping over for the last 3 days now is to automatically generate a value for the “id” field (“secondId” generates quite well).

The structure is as follows:

@Entity
@Data
class Main implements Serializable{

@EmbeddedId
@Builder.Default
private CompositeKey key = new CompositeKey();

@MapsId("secondId")
@JoinColumn
private SecondEntity secondEntity;

}

with the CompositeKey looking like:

@Embeddable
class CompositeKey implements Serializable {

@GeneratedValue(tried all strategies... this block isn't even resolved as I see)
private Integer id;

private Integer secondId;

}

From this point I’m using spring-data’s CrudRepository in order to make CRUD operations:

repository.save(formedMainEntityWithNoIdsProvided);

I get a sql error with “id” being null (“secondId” is generated and resolved, as I can see from the postgres’ console).

Running out of ideas how to make this work. Help!