Escolar Documentos
Profissional Documentos
Cultura Documentos
Globalcode – Open4education
Apresentação
Quem sou?
Conteúdo da Projeção
Função na Projeção
Globalcode – Open4education
Projeção
/* select
t
from
Team t */
select
team0_.id as id1_11_,
team0_.arena as arena2_11_,
team0_.city as city3_11_,
team0_.creationYear as creation4_11_,
team0_.division_id as division8_11_,
team0_.logo as logo5_11_,
team0_.name as name6_11_,
team0_.version as version7_11_
from
teams team0_
Globalcode – Open4education
Chaves
Chave candidatas
Chaves compostas
Globalcode – Open4education
Chaves e Joins
Chaves Primárias
Texto
Decimal
Indexação da junção
Globalcode – Open4education
Chaves e Joins
Recente:
@Entity
@Table(name = "teams", indexes={
@Index(name="TEAM_CITY_NAME_INDEX", unique=true,
columnList="CITY,NAME"),
@Index(name="TEAM_ID_NAME_INDEX", unique=true,
columnList="ID,NAME"),
@Index(name="TEAM_DIVISION_NAME_INDEX", unique=true,
columnList="DIVISION_ID,NAME")
})
public class Team implements Serializable {...
Globalcode – Open4education
Chaves e Joins
Hibernate:
alter table teams add constraint
TEAM_CITY_NAME_INDEX unique (city, name)
Hibernate:
alter table teams add constraint
TEAM_ID_NAME_INDEX unique (arena)
Hibernate:
alter table teams add constraint
TEAM_DIVISION_NAME_INDEX unique (division_id,
name)
Globalcode – Open4education
Views e Triggers
Views
Triggers
Como nascem?
Como lembramos?
Globalcode – Open4education
Bind Variables
Ex.:
‘Select * from Pais where Nome = ‘ + ‘Brasil’
Ex.:
‘Select * from Pais where Nome = ‘ :1
Using : ‘Brasil’
COALESCE
Globalcode – Open4education
ANSI
/* select
distinct e.playerContract,
coalesce(
( select sum(ev.value)
from GameEvent ev
where ev.playerContract = e.playerContract
and ev.eventType =
org.nba.model.GameEventType.POINTS_MADE),
0)
from
GameEvent e */
Continuação…
Globalcode – Open4education
ANSI
SELECT DISTINCT Gameevent0_.Playercontract_Id AS Col_0_0_,
Coalesce((SELECT SUM(Gameevent2_.Value)
FROM Game_Events Gameevent2_, Player_Contracts Playercont3_
WHERE Gameevent2_.Playercontract_Id = Playercont3_.Id
AND Gameevent2_.Playercontract_Id =
Gameevent0_.Playercontract_Id
AND Gameevent2_.Eventtype = 5), 0) AS Col_1_0_,
Playercont1_.Id AS Id1_6_, Playercont1_.Finishdate AS
Finishda2_6_, Playercont1_.Player_Id AS Player_I5_6_,
Playercont1_.Startdate AS Startdat3_6_, Playercont1_.Team_Id AS
Team_Id6_6_, Playercont1_.Version AS Version4_6_
FROM Game_Events Gameevent0_
INNER JOIN Player_Contracts Playercont1_
ON Gameevent0_.Playercontract_Id = Playercont1_.Id
Globalcode – Open4education
Função ‘ANSI’ #SQN
/* select FUNCTION('DATE_FORMAT', b.date, '%d/%m/%Y'),
b.awayTeamName,
b.awayPoints,
b.homePoints,
b.homeTeamName
from
BoxScore b */
SELECT Date_Format(Boxscore0_.Date, '%d/%m/%Y') as Col_0_0_,
Boxscore0_.Awayteamname AS Col_1_0_,
Boxscore0_.Awaypoints AS Col_2_0_,
Boxscore0_.Homepoints AS Col_3_0_,
Boxscore0_.Hometeamname AS Col_4_0_
FROM Boxscoreview Boxscore0_
Globalcode – Open4education
Exemplo Real
Tabela1: idGlobalTabela1, idTabela1, ...
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumns({
@JoinColumn(name="idGlobalTabela2", insertable=false,
updatable=false),
@JoinColumn(name="idTabela2", insertable=false,
updatable=false) })
Tabela2 tabela2;
Continuação...
Globalcode – Open4education
Exemplo Real
...
@OneToMany(fetch=FetchType.LAZY, mappedBy="tabela2")
collection<TabelaDeLigacao> registros;
....
-- Selecionar em Tabela2
Continuação...
Globalcode – Open4education
Exemplo Real
CriteriaBuilder criteriaBuilder=entityManager.getCriteriaBuilder();
CriteriaQuery<Tabela2> criteriaQuery =
criteriaBuilder.createQuery(Tabela2.class);
predicates.add(criteriaBuilder.equal(join.get(TabelaDeLigacao_.tabel
aDeLigacaoPK).get(TabelaDeLigacaoPK_.idGlobalTabela1),
valorIdGlocalTabela ));
Continuação...
Globalcode – Open4education
Exemplo Real
predicates.add(criteriaBuilder.equal(join.get(TabelaDeLigacao_.t
abelaDeLigacaoPK).get(TabelaDeLigacaoPK_.idTabela1),
valorIdTabela2));
criteriaQuery.select(root);
criteriaQuery.where(criteriaBuilder.and(predicates.toArray(new
Predicate[]{})));
criteriaQuery.orderBy(criteriaBuilder.asc(join.get(TabelaDeLigac
ao_.tabela2).get(Tabela2_.nmTabela2)));
TypedQuery<Tabela2> typedQuery =
entityManager.createQuery(criteriaQuery);
Continuação...
Globalcode – Open4education
Exemplo Real
SELECT Tabela2.Idglobaltabela2, Tabela2.Idtabela2, Tabela2. Nmtabela2
ON Tabela2.Id_Tabela2 = Tabeladeligacao.Id_Tabela2
Erro:
criteriaQuery.orderBy(criteriaBuilder.asc(join.get(TabelaDeLigac
ao_.tabela2).get(Tabela2_.nmTabela2)));
Correção:
criteriaQuery.orderBy(criteriaBuilder.asc(root.get(Tabela2_.nmTa
bela2)));
Continuação ...
Globalcode – Open4education
Exemplo Real
SELECT Tabela2.Idglobaltabela2, Tabela2.Idtabela2,
Tabela2.Nmtabela2
ON Tabela2.Id_Tabela2 = Tabeladeligacao.Id_Tabela2
AND Tabela2.Id_Global_Tabela2 =
Tabeladeligacao.Id_Global_Tabela2
Globalcode – Open4education
Encerramento
Twitter: @capin79
LinkedIn: https://br.linkedin.com/in/capin
fernando.franquini@gmail.com Globalcode – Open4education
Encerramento
Dúvidas?
Muito Obrigado!
Twitter: @capin79
LinkedIn: https://br.linkedin.com/in/capin
fernando.franquini@gmail.com Globalcode – Open4education