Professional Documents
Culture Documents
0205 HandsOn.1
0205 HandsOn.1
0205 HandsOn.1
May 20 1
ORM with Spring, JPA and Hibernate –
Hands on Instructions
• Create a spring boot project
• Add the application.properties file
• Create the business entity Team
• Add a first version of the main method
• Create the ITeamDao interface
• Create the TeamDaoImpl
• Define the main method
May 20 2
Configuration file application.properties
• You need first to create the database db_sport on your MySQL DBMS.
spring.datasource.url = jdbc:mysql://localhost:3306/db_sport?
useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimez
one=UTC
spring.datasource.username = root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql= true
spring.jpa.hibernate.ddl-auto= create
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
#spring.main.banner-mode=off
May 20 3
Defining DAO interfaces
Facade
Main method
May 20 4
Interface ITeamDao
package org.horizonefele.dao;
import java.util.List;
May 20 5
Class TeamDaoImpl
package org.horizonefele.dao;
import …
@Repository
@Transactional
public class TeamDaoImpl implements ITeamDao {
@PersistenceContext
private EntityManager entityManager;
@Override
public void save(Team t) {
entityManager.persist(t);
}
//similar to classes with JPA and Hibernate BUT without transactions
// spring takes care of this; one of the practical use of IoC
May 20 6
Class TeamDaoImpl (cont.)
public void save(Team t) {
entityManager.persist(t);
}
May 20 8
Defining the main method
package org.horizonefele;
import …
@SpringBootApplication
public class JpaSpringApplication {
May 20 10
Technological layering for ORM
Hands on session DAO with Spring Data
May 20 11
ORM with Spring Data – Hands on
instructions
• Create another spring boot project (as we did in the previous part)
• Add the application.properties file
• Add the business entity Team
• Add just the interface TeamRepository
• Define the main method
May 20 12
Interface TeamRepository
package org.horizonefele.dao;
import …
May 20 13
The main method
package org.horizonefele;
import …
@SpringBootApplication
public class JpaSpringApplication {
May 20 15
Technological layering for ORM
Hands on session DAO with Spring Data
May 20 16
Adding Associations – Hands on
Instructions
May 20 17
Business entity Player
package org.horizonefele.entities;
import …
@Entity
public class Player implements Serializable {
@Id @GeneratedValue
private Long idPlayer;
@Column(length=100)
private String name;
@ManyToOne
@JoinColumn(name="idTeam")
private Team myteam;
//constructors
//getters and setters
May 20 18
Business entity Team updated
package org.horizonefele.entities;
import …
@Entity
public class Team implements Serializable {
@Id @GeneratedValue
private Long idTeam;
@Column(length=100)
private String name;
@OneToMany(mappedBy="myteam")
private Collection<Player> players;
//Constructors
//Getters and Setters
May 20 19
Interface PlayerRepository
package org.horizonefele.dao;
import org.horizonefele.entities.Player;
import org.springframework.data.jpa.repository.JpaRepository;
May 20 20
The main method
package org.horizonefele;
import …
@SpringBootApplication
public class JpaSpring1Application {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(JpaSpring1Application.class, args);
TeamRepository teamDao = ctx.getBean(TeamRepository.class);
PlayerRepository playerDao = ctx.getBean(PlayerRepository.class);
Team t1 = new Team("Avenir Sportif de Lala"); Team t2 = new Team("Club Olympique de Transport");
Team t3 = new Team("Stir Sportif Jarzouna");