HIBERNATE
HIBERNATE
2020-2021
abdelhafid el-abbassi
• Intro
• Setting up
• Hibernate CRUD
• Mapping OneToOne
• UNI
• BI
• Mapping ManyToOne OneToMany
• UNI
• BI
• Mapping ManyToMany
• Advanced
• (Eager vs Lazy)
• Cascade
abdelhafid el-abbassi
INTRO
abdelhafid el-abbassi
• Intro
• ORM
• Low-level & minimazes JDBC code
abdelhafid el-abbassi
• ORM (Object-Relationel Mapping)
abdelhafid
THE Id of el-abbassi
the personne
• Retrieving Objects
abdelhafid el-abbassi
• HQL (Hibenate Query Language)
abdelhafid el-abbassi
Setting up
abdelhafid el-abbassi
• https://dev.mysql.com/downloads/connector/j/
abdelhafid el-abbassi
• https://hibernate.org/orm/
abdelhafid el-abbassi
1- Configuration file
abdelhafid el-abbassi
2-Annotation java
abdelhafid el-abbassi
Hibernate CRUD
abdelhafid el-abbassi
Session & SessionFactory
abdelhafid el-abbassi
SessionFactory
SessionFactory is an interface available in org.hibernate
package
• SessionFactory is thread safe so multiple threads can access
the SessionFactory at the same time.
• SessionFactory is Immutable. Once we create SessionFactory
we can not modify it(If you look SessionFactory methods we
don’t have any setter kind of method)
• SessionFactory is created at the time of application startup, it
reads all information from the configuration
file(hibernate.cfg.xml file).
• We should have one SessionFactory for one database
configuration.
abdelhafid el-abbassi
Session In Hibernate.
• Session is an interface available in org.hibernate package
which provides different API to communicate java
application to hibernate.
abdelhafid el-abbassi
1- Mapping the
object
abdelhafid el-abbassi
2-Insert
abdelhafid el-abbassi
3-Read
abdelhafid el-abbassi
4-Update
abdelhafid el-abbassi
5-Delete
abdelhafid el-abbassi
Mapping OneToOne
UNI
BI
abdelhafid el-abbassi
Mapping OneToOne
UNI
@Entity @Entity
public class Personne { public class Voiture {
code @Id
@Id @GeneratedValue(strategy =
@GeneratedValue(strategy = GenerationType.IDENTITY)
GenerationType.IDENTITY) 1..1 private int id;
private int id; @Column
@Column 1..1 private String modele;
private String name; @Column
@Column private Date annee;
private String lastname;
@OneToOne(cascade =
CascadeType.ALL)
@JoinColumn(name = "id_V")
private Voiture voiture;
//code
abdelhafid el-abbassi
Mapping OneToOne
BI
@Entity
@Entity
public class Personne {
public class Voiture {
@Id
@Id
@GeneratedValue(strategy =
@GeneratedValue(strategy =
GenerationType.IDENTITY)
GenerationType.IDENTITY)
1..1 private int id;
private int id;
@Column
@Column
private String name; 1..1 private String modele;
@Column
@Column
private Date annee;
private String lastname;
@OneToOne(mappedBy =
@OneToOne(cascade =
"voiture",cascade =
CascadeType.ALL)
CascadeType.ALL)
@JoinColumn(name = "id_V")
private Personne personne;
private Voiture voiture;
//code
abdelhafid el-abbassi
Mapping ManyToOne
OneToMany
UNI
BI
abdelhafid el-abbassi
ManyToOne OneToMany UNI
@Entity
public class Video { @Entity
@Id public class Commentaire {
@GeneratedValue(strategy = @Id
@GeneratedValue(strategy =
GenerationType.IDENTITY) GenerationType.IDENTITY)
private int id; private int id;
@Column @Column
private String lien; private String lib;
@Column
private String titre; @Column
private String prop;
@OneToMany(cascade =
CascadeType.ALL) //code…
@JoinColumn(name = "id_V")
private List<Commentaire>
commentaires;
abdelhafid el-abbassi
//code…