Skip to content

Instantly share code, notes, and snippets.

@robertoschwald
Last active August 11, 2017 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertoschwald/6ef15d49369b0d1b5607cb52295b1cb9 to your computer and use it in GitHub Desktop.
Save robertoschwald/6ef15d49369b0d1b5607cb52295b1cb9 to your computer and use it in GitHub Desktop.
TABLE_PER_CLASS and MySQL / Galera PK problem possible workaround
// Due to MySQL's lack of sequences, Hibernate 5.x would use TABLE identity if
// setting strategy = @GeneratedValue(strategy = GenerationType.AUTO).
// This currently does not work with Galera Cluster, as it needs a PK
// which is not defined by the default hibernate sequence table "hibernate_sequence",
// so DDL will fail and the application does not work as expected.
// One Trick would be to define our own TableGenerator which holds a PK like below.
// Disadvantages:
// - performance (generally a problem when using GenerationType.TABLE)
// - not the best option for a DB agnostic application, when other DB types support SEQUENCE.
@javax.persistence.TableGenerator(
name="ONE_TIME_SEQUENCE",
table="one_time_ticket_sequence",
pkColumnName = "key",
valueColumnName = "hi"
pkColumnValue="EXAMPLE",
allocationSize=30
)
@Entity
public class SampleEntity {
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator=ONE_TIME_SEQUENCE)
@Column(name = "ID")
private Long id;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment