Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
<version>5.1.10</version>
</dependency>

<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.1</version>
</dependency>

</dependencies>
</project>
7 changes: 7 additions & 0 deletions src/main/java/br/com/akato/hibernate/Endereco.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ public Endereco(String logradouro,Long numero){
this.numero = numero;
}

public Endereco(String logradouro,Long numero, Pessoa pessoa){
this.logradouro = logradouro;
this.numero = numero;
this.pessoa = pessoa;
}

public Endereco(){
}



Expand Down
42 changes: 31 additions & 11 deletions src/main/java/br/com/akato/hibernate/Pessoa.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,68 @@
import javax.persistence.Table;

@Entity
@Table(name="pessoa")
@Table(name = "pessoa")
public class Pessoa {
public Pessoa() {

}

@Id
@GeneratedValue
@Column(name="pessoa_id")
@Column(name = "pessoa_id")
private Long id;

private String nome;
private Long cpf;
@OneToMany(mappedBy="pessoa")

@OneToMany(mappedBy = "pessoa")
private Set<Endereco> enderecos;



public Pessoa(String nome,Long cpf){

public Pessoa(String nome, Long cpf) {
this.nome = nome;
this.cpf = cpf;
this.enderecos = new HashSet<Endereco>();
}


// apenas para o exemplo de CGLIB
public Pessoa(Long id) {
this.id = id;
}

//
public String getNome() {
return this.nome;
}

public void setNome(String nome) {
this.nome = nome;
}

public Long getCpf() {
return cpf;
}

public void setCpf(Long cpf) {
this.cpf = cpf;
}

public Set<Endereco> getEnderecos() {
return enderecos;
}

public void setEnderecos(Set<Endereco> enderecos) {
this.enderecos = enderecos;
}

public void addEndereco(Endereco endereco) {
this.enderecos.add(endereco);
}
public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

}
2 changes: 1 addition & 1 deletion src/main/java/br/com/akato/hibernate/PessoaDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public PessoaDAO(Session session){

public void salva(Pessoa p){
this.session.save(p);
this.salvaEndereco(p);; //adicionado agora
this.salvaEndereco(p);
}

private void salvaEndereco(Pessoa p){
Expand Down
55 changes: 42 additions & 13 deletions src/main/java/br/com/akato/hibernate/TestaPessoaDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,55 @@

public class TestaPessoaDAO {
public static void main(String[]Args){
criarContatosNaBase();
executaProxyamentoParaAcessarObjetoNaoCarregado();
lancaLazyException();
}

static void criarContatosNaBase(){
Session session = new HibernateUtil().getSession();
Pessoa pessoa = new Pessoa("Arthur Kato",38742425875L);
Endereco endereco = new Endereco("El.Grajau",292L);
PessoaDAO pessoadao = new PessoaDAO(session);
Pessoa pessoa = new Pessoa("Renato De Melo",31180069279l);
Endereco endereco = new Endereco("Al. Rio Negro",291l,pessoa);
Endereco enderecoB = new Endereco("Al. Cauaxi",721l,pessoa);
pessoa.addEndereco(endereco);
PessoaDAO dao = new PessoaDAO(session);
pessoa.addEndereco(enderecoB);
session.beginTransaction();
dao.salva(pessoa);
pessoadao.salva(pessoa);
session.getTransaction().commit();
System.out.println(" Foi adicionado a pessoa : " + pessoa.getNome());

}
static void executaProxyamentoParaAcessarObjetoNaoCarregado(){
Session session = new HibernateUtil().getSession();
System.out.println("Sessao Aberta");
PessoaDAO pessoadao = new PessoaDAO(session);
Pessoa pessoa = pessoadao.busca(3l);
Object enderecoProxy = pessoa.getEnderecos();
System.out.println("O objeto de persistencia com inicializacao `tardia` esta sendo proxyado");
org.hibernate.proxy.HibernateProxy objeto = (org.hibernate.proxy.HibernateProxy) pessoa;
System.out.println("Para o o objet Pessoa foi gerado um objeto Proxy" + objeto.getClass());
org.hibernate.collection.internal.PersistentSet persistentSet = (org.hibernate.collection.internal.PersistentSet) enderecoProxy;
System.out.println("O Set de Enderecos foi proxiado pela classe : " + persistentSet.getClass() + " , valor : "+ persistentSet.toString());
session.close();


System.out.println("Sessao Fechada");
System.out.println(""+pessoa.getNome()+":");
for(Endereco endereco:pessoa.getEnderecos()){
System.out.println(" Endereco de Id : " + endereco.getId() +", : " + endereco.getLogradouro() + endereco.getNumero());
}
}
static void lancaLazyException(){
Session session = new HibernateUtil().getSession();
PessoaDAO pessoadao = new PessoaDAO(session);
Pessoa pessoa = pessoadao.busca(3l);
session.close();
System.out.println("-------------------------------");
System.out.println("Sessao Fechada");
System.out.println(""+pessoa.getNome()+":");
for(Endereco endereco:pessoa.getEnderecos()){
System.out.println(" Endereco de Id : " + endereco.getId() +", : " + endereco.getLogradouro() + endereco.getNumero());
}

}
//System.out.println(" Foi adicionada a pessoa : " + p.getNome());
}


//pessoa.setNome("Arthur Kato");
//pessoa.setCpf(38742425875L);
//
}

18 changes: 18 additions & 0 deletions src/main/java/br/com/akato/hibernate/proxy/MyFakeList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package br.com.akato.hibernate.proxy;

public class MyFakeList <T>{
private Object[] objetos = new Object[10];
private int size = 0;

public void add(T t){
this.objetos[size] = t;
size++;
}
public int getSize(){
return this.size;
}
@SuppressWarnings("unchecked")
public T get(int ref){
return (T) this.objetos[ref];
}
}
25 changes: 25 additions & 0 deletions src/main/java/br/com/akato/hibernate/proxy/MyInterceptor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package br.com.akato.hibernate.proxy;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class MyInterceptor implements MethodInterceptor {
public MyInterceptor(){}

public MyInterceptor(MyFakeList fakeList){
this();
this.fakeList = fakeList;

}

private MyFakeList fakeList;

public Object intercept(Object object, Method method, Object[]args,MethodProxy methodProxy) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{
System.out.println("--------------Meu Proxy em Ação--------------------");
System.out.println("Excutando método interceptado : " + method.getName());
return method.invoke(fakeList, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package br.com.akato.hibernate.proxy;

import net.sf.cglib.proxy.Enhancer;
import br.com.akato.hibernate.Pessoa;

public class MyProxyCGLIBService<T> {
public MyFakeList<T> criarListaFake(){
final MyFakeList<T> exemplo = new MyFakeList<T>();
MyInterceptor c = new MyInterceptor(exemplo);
MyFakeList objetoProxiado = (MyFakeList) Enhancer.create(MyFakeList.class,c);
return objetoProxiado;
}

public static void main(String[] args) {
MyFakeList<Pessoa> list = new MyProxyCGLIBService<Pessoa>().criarListaFake();
list.add(new Pessoa(1l));
list.add(new Pessoa(2l));
list.add(new Pessoa(3l));
int qtdeItens = list.getSize();
Pessoa p = list.get(2);
System.out.println(String.format("Id: %d ", p.getId()));
System.out.println(String.format("Qtde Itens na lista: %d ", qtdeItens));
}
}
Binary file modified target/classes/br/com/akato/hibernate/Endereco.class
Binary file not shown.
Binary file modified target/classes/br/com/akato/hibernate/Pessoa.class
Binary file not shown.
Binary file modified target/classes/br/com/akato/hibernate/TestaPessoaDAO.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.