Sj17
Sj17

Reputation: 11

@GeneratedValue doesn't support with not Id column after upgrading to springboot 3.2.4

I was upgrading springboot 3.1 to 3.2.4 which uses hibernate 6.4.4.Final.

I am getting the below error while building the mvn project-

Error creating bean with name 'entityManagerFactory' defined in class path resource [/JpaConfiguration.class]: Property 'id2' is annotated @GeneratedValue but is not part of an identifier

entity -

  @Id
    @Min(100000000)
    @Max(9999999999L)
    private Long id1;

 

    @Column(name = "id2")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id2;

Just want to know is there any workaround for the problem without using custom generator

Upvotes: 1

Views: 2125

Answers (1)

Junaid Khan
Junaid Khan

Reputation: 19

Remove @GeneratedValue from id2: You can simply remove the @GeneratedValue annotation from id2.

@Column(name = "id2")
private Long id2;

Change the strategy for id2: If you intend id2 to have generated values but not as a primary key, you can use a different strategy such as GenerationType.AUTO. However, this might not be suitable for all scenarios and could lead to potential issues with uniqueness.

@Column(name = "id2")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id2;

Choose the option that best fits your requirements and the semantics of your entity model.

Upvotes: -1

Related Questions