Unknown Id.generator exception

Most of the time, we will want to use database sequencing for our table primary key. In order to do so, we know that we need to add the generator attribute in the @GeneratedValue annotation on our entity. The @GeneratedValue annotation allows us to define a strategy for our primary key.

The following is the code snippet that we add in our entity to set database sequencing for our primary key:

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "accountSequence")
private Integer id;

Here, we thought that accountSequence was the database sequence name provided to the generator; however, when the application runs, it gives an exception. To solve this exception, we annotate our entity with @SequenceGenerator and give the name as accountSequence, and the database sequence name that Hibernate needs to use. The following shows how to set the @SequenceGenerator annotation:

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "accountSequence")
@SequenceGenerator(name = "accountSequence", sequenceName = "account_seq", initialValue = 100000)
private Long accountId;

We saw common problems faced during implementation. Now, let's see how to tune Hibernate to achieve high performance.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset