0205 HandsOn.1

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

Technological layering for ORM

DAO with Spring Data

DAO with Spring over JPA and


Hands on session
Hibernate

DAO with JPA and


Hibernate

DAO with JDBC

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

Business Layer Couche DAO


Data Base
ITeamDAO
Player Team CRUD Operations Player Team
on the entity Team id_player 1 id_team
id_player : Long 0,n 1,1 id_team : Long
name_player : String name_team : String name_player name_team
n
num_team #
TeamDAOImpl

May 20 4
Interface ITeamDao
package org.horizonefele.dao;

import java.util.List;

public interface ITeamDao {

public void save(Team t);


public List<Team> findAll();
public List<Team> findByName(String name);
public Team findOne(Long idTeam);
public void update(Team t);
public void remove(Long idTeam);

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);
}

public Team findOne(Long idTeam) {


Team t = entityManager.find(Team.class, idTeam);
return t;
}

public List<Team> findAll() {


Query query = entityManager.createQuery("select t from Teams t")
return query.getResultList();
}
May 20 7
Class TeamDaoImpl (cont.)
public List<Team> findByName(String name) {
Query query = entityManager.createQuery("select t from Teams t where t.name like :x ");

query.setParameter("x", "%"+ name +"%");


return query.getResultList();
}
public void remove(Long idTeam) {
Team t = entityManager.find(Team.class, idTeam);
entityManager.remove(t);
}

public void update(Team t) {


entityManager.merge(t);
}

May 20 8
Defining the main method
package org.horizonefele;

import …
@SpringBootApplication
public class JpaSpringApplication {

public static void main(String[] args) {

ApplicationContext ctx = SpringApplication.run(JpaSpringApplication.class, args);


ITeamDao teamDao = ctx.getBean(ITeamDao.class);

teamDao.save(new Team("Avenir Sportif de Lala"));


teamDao.save(new Team("Club Olympique de Transport"));
teamDao.save(new Team("Stir Sportif Jarzouna"));

List<Team> teams = teamDao.findByName("sportif");


for(Team t : teams) {
System.out.println("Team : "+t.getName());
}
May 20 9
} }
DAO with Spring Data

May 20 10
Technological layering for ORM
Hands on session DAO with Spring Data

DAO with Spring over JPA and


Hibernate

DAO with JPA and


Hibernate

DAO with JDBC

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 …

public interface TeamRepository extends JpaRepository<Team, Long> {

@Query("select t from Team t where t.name like :x")


public List<Team> getTeamsByName(@Param("x")String name);

May 20 13
The main method
package org.horizonefele;

import …
@SpringBootApplication
public class JpaSpringApplication {

public static void main(String[] args) {

ApplicationContext ctx = SpringApplication.run(JpaSpringApplication.class, args);


TeamRepository teamDao = ctx.getBean(Teamrepository.class);

teamDao.save(new Team("Avenir Sportif de Lala"));


teamDao.save(new Team("Club Olympique de Transport"));
teamDao.save(new Team("Stir Sportif Jarzouna"));

List<Team> teams = teamDao.findAll();


for(Team t : teams) {
System.out.println("Team : "+t.getName());
}
May 20 14
} }
Adding Associations

May 20 15
Technological layering for ORM
Hands on session DAO with Spring Data

DAO with Spring over JPA and


Hibernate

DAO with JPA and


Hibernate

DAO with JDBC

May 20 16
Adding Associations – Hands on
Instructions

• Add the business entity Player


• Update the business entity Team
• Add the PlayerRepository interface
• Update the main method

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;

public interface PlayerRepository extends JpaRepository<Player, Long>{

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");

teamDao.save(t1); teamDao.save(t2); teamDao.save(t3);

playerDao.save(new Player("Tarek Dhieb",t1)); playerDao.save(new Player("Zoubeir Baya",t2));


playerDao.save(new Player("Hamadi Agrbi",t3));

List<Team> teams = teamDao.findByName("%sportif%");


for(Team t : teams) { System.out.println("Team : "+t.getName()); } } }
May 20 21

You might also like