Understanding the @TableGenerator annotation against hibernate version

208 views Asked by At

I have a java project where I use hibernate (5.0.3.Final) in order to perform actions on my postgres database.

I have this base entry that all my other entries are extended to

@MappedSuperclass
public abstract class BaseEntry {
    private Long id;

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "itpGenerator")
    @TableGenerator(name = "itpGenerator",
            // the first 999 entries are saved for items that come with the system
            initialValue = 1000,
            allocationSize = 100
    )
    @Column(name="ID", insertable = false, updatable = false)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

example for such entry that extended to the base:

@Entity
@Table(name = "AD_ENDPOINTS")
public class ActiveDirectoryEndpointEntry extends BaseEntry {
    private String name;

    @Column(name = "FRIENDLY_NAME")
    public String getName() {
        return name;
    }
}

and I use the @TableGenerator annotation (javax.persistence.TableGenerator version 2.2) in order to generate ids for my rows staring at 1000 and increasing by one each time.

Everything worked fine, any new row of ActiveDirectoryEndpointEntry got its id, starting from 1001 and up (1002,1003) but somehow it seems like the table itpGenerator was never created. I connected to the database, looking for it and it was never there (select * from itpGenerator) and I never created it manually but again everything worked as it should.

then I upgraded my hibernate to 5.4.24.Final and somehow the table was created and displayed in my database. I couldn't find any doc that explains this behavior.

In addition, The column @Id was reseted back to the starting number, meaning that if I tried to enter a new entry for ActiveDirectoryEndpointEntry, I got an error:

ERROR: duplicate key value violates unique constraint "ad_endpoints_pkey" Detail: Key (id)=(1001) already exists.

can someone please explain it and tell me how can I prevent the reseting from happening?

Thanks in advance

0

There are 0 answers