anchor.diff: -= Jpa and spring data kata
  += JPA/Hibernate camp
  +:docInfo1
  +:numbered:
  +:icons: font
  +:pagenums:
  +:imagesdir: images
  +:source-highlighter: coderay
  +
  +:image-link: https://pbs.twimg.com/profile_images/425289501980639233/tUWf7KiC.jpeg
  +
  +ifndef::sourcedir[:sourcedir: ./src/main/java/]
   
   #Sample demo to show how jpa work in effiecient way
   
  
positive.code:      - OGM - support for NoSql database operation. Reusing Hibernate core engine to persistence and mapping nosql data.
       - Envers - audit logging and keeping multiple version. Allow to show history operation is also similar to version control system like svn or git
    
  * Problem of subtypes
  
  * Simple entity
  
negative.code:   - no argument constructor
    - getter and setter for all interested fields
    - 
    
  Providing an ID for Persistence
  
   ID element has attributes such as column, type, and generator that you use to generate the identifier.
   
  •	 Database sequence
  •	 Native generator
  •	 Increment generator
  •	 Hilo generator
  
  Some of these strategies are available only for specified databases; for instance, the sequence strategy is
  not supported in MYSQL, but it is provided by Oracle.
  
  Sequence:
  [source,java]
  ----
  @Id  @GeneratedValue(strategy = GenerationType.SEQUENCE)
  public Long id;
  ----
  
  Enhanced Sequence Style Generator
  
  Optimization allows you to query a bunch of identifiers at a time and store them in local memory.
  Once they have been exhausted, the database can be queried for the next group of identifiers, which avoids
  making a call to the database for every new identifier.
  Use the GenericGenerator tag for these enhanced generators:
   
  [source,java]
  ----
  @GeneratedValue( generator="IdGen")
  @GenericGenerator(strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",name="IdGen",
  parameters = {
  @Parameter(name = "sequence_name", value = "seq_id_gen"),
  @Parameter(name = "optimizer", value = "pooled"),
  @Parameter(name = "initial_value", value = "1000"),
  @Parameter(name = "increment_size", value = "10")
  })
  ----
  
  The initial value is set to 1000 and the increment size is 10. The optimizer
  values can be one of the following:
  •	 none does not perform any optimizations and hits the database for every new
  identifier
  •	 hilo generates the identifiers according to the hi/lo algorithm
  •	 pooled stores the high value in the series as the nextval
  •	 pooled-lo stores the low value in the series as the nextval
  
  
  [source,java]
  ---- 
  @Id
  @GeneratedValue( generator="IdGen")
  @GenericGenerator(strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",name="IdGen",
  parameters = {
  @Parameter(name = "sequence_name", value = "seq_id_gen"),
  @Parameter(name = "optimizer", value = "pooled"),
  @Parameter(name = "initial_value", value = "1000"),
  @Parameter(name = "increment_size", value = "10")
  })
  ----
  
  
  
  Increment reads the maximum primary key column value from the table and increments the value by one.
  [source,java]
  ----
  @Id  @GeneratedValue(generator = "increment")
  public Long id;
  ----   
     
  Hilo Generator uses the hi/lo algorithm to generate the identifiers that are unique to a particular database.
  It retrieves the high value from a global source (by default, the hibernate_unique_key table and next_hi
  column), and the low value from a local source. The max_lo value option is provided to define how many low
  values are added before a high value is fetched
  [source,java]
  ----
  @Id
  @GeneratedValue(generator = "hilo")
  public Long id;
  ----
  
  The hilo generator is of type long. This generator should not be used with a user-supplied connection.
  The high value must be fetched in a separate transaction from the Session transaction, so the generator must
  be able to obtain a new connection and commit it. Hence, this implementation cannot be used when the user
  is supplying connections. In that case, a SequenceHiLoGenerator is a better choice (where supported).
  The hilo generator is used for batch operations
  
  
  Enhanced Table Generator
  This generator defines a table that holds the increment values, and the same table can be used by multiple
  applications for the increment values. Each application is distinguished by the key value.
  [source,java]
  ----
  @GeneratedValue( generator="TableIdGen")
  @GenericGenerator(strategy="org.hibernate.id.enhanced.TableGenerator",name="TableIdGen",
  parameters = {
  @Parameter(name = "table_name", value = "enhanced_hibernate_sequences"),
  @Parameter(name = "segment_value", value = "id"),
  @Parameter(name = "optimizer", value = "pooled"),
  @Parameter(name = "initial_value", value = "1000"),
  @Parameter(name = "increment_size", value = "10")
  })
  ----
  
  Creating a Composite Key
   
  [source,java]
  ----
  public class EmployeeId implements Serializable {
  Long department;
  Long idCard;
  
   
  @Entity
  @IdClass(EmployeeId.class)
  public class Employee {
  @Id
  Long department;
  @Id
  Long idCard;
  ----
  
  
  Composite ID as a Property Using a Component Type
  [source,java]
  ----
  @Embeddable
  public class ISBN implements Serializable {
  @Column(name = "group_number")
  int group;
  int publisher;
  int titleRef;
  int checkDigit;
  }
  
  
  @Entity
  public class Book {
  @Id
  ISBN id;
  @Column
  String title;
  ----
  
  == O mnie
  * programista
  * blog link:http://przewidywalna-java.blogspot.com[]
  * image:{image-link} [role='img-circle']
  
  
  
  