Wednesday, 8 May 2019

Hibernate

Best tutorila for Hibernate:
---------------------------------
https://www.java4s.com/hibernate

https://www.java4s.com/hibernate/hibernate-many-to-many-mapping-using-annotations/ 

good one: https://vladmihalcea.com/the-best-way-to-map-the-discriminatorcolumn-with-jpa-and-hibernate/

Hibernate:
----------
1. Table per class Hierarachy
- only one Table for all classes
- SINGLE TABLE


2. Table per subclass Hierarchy
-for Each Class one Table wil be created
-Subclass can used save base calss data with referce key

3. Table per concreate class Hierarch
- for Each Subclass one Table will be created
- base class's key can be used to save the subclass details

One to many:
-------------
@JoinTable(name="categories_items",joinColumns=@JoinColumn(name="cat_id_fk",referencedColumnName="catid"),
inverseJoinColumns=@JoinColumn(name="item_id_fk",referencedColumnName="itemid"))

onetoMany
--------------
-FK will be JoinColumn









Descrimator Column and Value: (Inheritance concept)
--------------------------------------
@Table(name = "ANIMAL")
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING, name = "TYPE")
public class Animal {
}
Type is column in Animal Table that would be Reptile or Bird Type
@Entity
@DiscriminatorValue("REPTILE")
public class Reptile extends Animal {
@Entity
@DiscriminatorValue("BIRD")
public class Bird extends Animal {

}

No comments:

Post a Comment