0

I use Hibernate 4 and Oracle 11g. And there is a convenient feature in this duet. I can neatly point out as follows:

@Entity
@Table(name = "APPLICATION", schema = "PRODUCTION")
public class ApplicationVersionModel {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PRODUCTION.SEQUENCE_NEW")
    @SequenceGenerator(name = "PRODUCTION.SEQUENCE_NEW", schema = "PRODUCTION", sequenceName = "PRODUCTION.SEQUENCE_NEW", allocationSize = 1)
    @Column(name = "ID")
    @XmlTransient
    @Getter @Setter private Long id;

And describe the sequence in the DB:

CREATED         27.02.17
LAST_DDL_TIME   27.02.17
SEQUENCE_OWNER  PRODUCTION
SEQUENCE_NAME   SEQUENCE_NEW
MIN_VALUE       1
MAX_VALUE       9999999999999999999999999999
INCREMENT_BY    1
CYCLE_FLAG      N
ORDER_FLAG      N
CACHE_SIZE      20
LAST_NUMBER     957101
PARTITION_COUNT 
SESSION_FLAG    N
KEEP_VALUE      N

So that the combination will allow me to use the app in a multithread environment. And simultaneously accessing users will get a unique id with no threat to a collision. The documentation explicitly declares this feature.

In that sence, is there any similar functionality in pair of Hibernate and Mysql? Any suggestion is highly appreciated. Many thanks in advance.

Evgeny Mamaev
  • 1,237
  • 1
  • 14
  • 31

1 Answers1

0

If you use the sequence to generate unique ids for a single table, then MySQL's auto_increment will provide the same functionality.

The accepted answer to How to annotate MYSQL autoincrement field with JPA annotations question here in SO describes how to use MySQL's auto_increment within hybernate:

To use a MySQL AUTO_INCREMENT column, you are supposed to use an IDENTITY strategy:

@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;

Which is what you'd get when using AUTO with MySQL:

@Id @GeneratedValue(strategy=GenerationType.AUTO)
private Long id;

Which is actually equivalent to

@Id @GeneratedValue
private Long id;
Community
  • 1
  • 1
Shadow
  • 33,525
  • 10
  • 51
  • 64