RMI Architecture and Hibernate Implementation Guide

RMI Architecture

Controller

1) Controller instance

2) BusinessDelegate Server. Create controller, in the Controller setServer(new BusinessDelegate());, in the methods add BusinessException and Remote Excp.

Business Delegate

1) Class implements IAppRemote

2) IAppRemote BusinessService.

setBusinessService( (IAppRemote) Naming.lookup(IAppRemote.URL_SERVICIO) );
} catch (Exception e) {
throw new BusinessException(e.getMessage());

BusinessException

BusinessException extends Exceptions implements Serializable. BusinessException(string message) super(message).

IAppRemote

IAppRemote extends Remote, IAlumnoRemote, etc.

private static final String URL_SERVICIO=”localhost/parcial”

private static final Integer PORT_SERVICIO=1099

InterfacesCLASES

public Integer cantidadAlu() throws RemoteExc, BusinnesExp

ObjetoRemoto (RMI folder)

ObjetoRemoto extends UnicastRemoteObjetc implements IAppRemote

ObjetoRemoto() throws RemoteException.

Server

1) Server instance

2) ObjetoRemoto objetoRemoto

server (iniciar())

public void iniciar() {
try{
this.setObjetoRemoto(new ObjetoRemoto());
LocateRegistry.createRegistry(IAppRemote.PORT_SERVICIO);
Naming.bind(IAppRemote.URL_SERVICIO,getObjetoRemoto());
System.out.println(“Fijate en :” + IAppRemote.URL_SERVICIO + “puerto: ” + IAppRemote.PORT_SERVICIO);

}catch (Exception e)

DTO

SERIALIZABLES!!! Only the attributes that I will show get and set

Hibernate Implementation

DAO

Instance, only the methods. (Database–>entity–>Dao—>Business Object—>Controller—>DTO.

Session session = HibernateUtil.getSessionFactory().openSession();
Query query = session.createQuery(“FROM AlumnoEntity a WHERE a.idAlumno = :id”);
query.setParameter(“id”, id);

//query.list(),query.UniqueResult()

HQL

  • Cartesian product: FROM Usuario u, Permiso WHERE u.id = 1
  • Inner join: FROM Usuario u inner join u.permisos as p WITH p.estatus = 1
  • SELECT dir FROM Usuario as u inner join u.direccion as dir
  • SELECT COUNT(*) FROM Ciclo
  • SELECT c.nombre FROM Ciclo c WHERE nombre=’Desarrollo de aplicaciones Web’
  • SELECT tb FROM TiposBasicos tb WHERE dateDate=’2012-07-25′
  • SELECT tb FROM TiposBasicos tb WHERE inte BETWEEN 1 AND 10
  • SELECT tb FROM TiposBasicos tb WHERE dataDate IS NULL
  • Subquery: SELECT c.nombre,c.horas FROM Ciclo c WHERE c.horas > (SELECT AVG(c2.horas) FROM Ciclo c2)

Persistence

OneToOne

  • Unidir: @OneToOne
  • @JoinColumn(name=”the id of the other class”)
  • Bidirectional: same, but to the other class you add @OneToOne(mappedby=”the name of the class in the other”)

ManyToOne (can be uni and bidirectional)

  • @ManyToOne
  • @JoinColumn(name=”ID other class”)
  • Departamento departamentos

OneToMany (bi-directional with MTO)

  • @OneToMany(mappedby=”departamentos”)
  • List personas

ManyToMany (there is no join column)

  • Choose a class A: @ManyToMany—>list listaB
  • Class B: @ManyToMany(mappedby=”listaB”)—>list listaA

ManyToMany with table in the middle

@ManyToMany
@JoinTable(name=”EMP_PROJ”,
joinColumns=@JoinColumn(name=”EMP_ID”),
inverseJoinColumns=@JoinColumn(name=”PROJ_ID”))
private Collection projects;

AEntity

@OneToMany(cascade=CascadeType.ALL, mappedBy=”key.entidadA”)
private List listaAB;

BEntity

@OneToMany(cascade=CascadeType.ALL, mappedBy=”key.entidadB”)
private List listaAB;

ABEntity

@EmbeddedId
private ABEntityKey key;

ABEntityKey

@Embeddable
public class ABEntityKey implements Serializable
@ManyToOne(cascade=CascadeType.ALL, optional=false)
private AEntity entidadA;
@ManyToOne(cascade=CascadeType.ALL, optional=false)
private BEntity entidadB;

Hibernate Configuration

hibernate.dialect org.hibernate.dialect.SQLServerDialect
hibernate.connection.driver_class net.sourceforge.jtds.jdbc.Driver
hibernate.connection.url jdbc:jtds:sqlserver://bd
hibernate.connection.username ad5997n_08
hibernate.connection.password ad5997n_08
hibernate.connection.pool_size 2
hibernate.connection.autocommit true
hibernate.cache.use_query_cache true
hibernate.cache.provider_class org.hibernate.cache.HashtableCacheProvider
hibernate.format_sql true
hibernate.show_sql true
#hibernate.hbm2ddl.auto create-drop
hibernate.hbm2ddl.auto update (if the database does not exist)
#hibernate.hbm2ddl.auto validate (if the database exists)

Controllers

The controllers like AlumnoController is a class with INSTANCE and then the methods! Business Objects arrive from the controllers and DTOs leave!!!

ServerMain (main folder on server)

public class ServerMain {

public static void main(String[] args) {
Server.getInstancia();
}
}
——————————————————–

public Alumno(AlumnoDTO alumno) { (DO THE SAME FOR ALUMNOENTITY)

setNombre(alumno.getNombre());

setEstado(alumno.getEstado());

}

HIBERNATE.UTIL (GOES IN AN HBT FOLDER)

package ejemplo.hbt;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import ejemplo.entities.ABEntity;
import ejemplo.entities.AEntity;
import ejemplo.entities.AlumnoEntity;
import ejemplo.entities.BEntity;

public class HibernateUtil{

private static final SessionFactory sessionFactory;

static {
try {
AnnotationConfiguration config = new AnnotationConfiguration();

config.addAnnotatedClass(AlumnoEntity.class);
config.addAnnotatedClass(AEntity.class);
config.addAnnotatedClass(BEntity.class);
config.addAnnotatedClass(ABEntity.class);

sessionFactory = config.buildSessionFactory();
}
catch (Throwable ex) {
System.err.println(“Initial SessionFactory creation failed.” + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory(){
return sessionFactory;
}
} ——————————————TO DTO

public AlumnoDTO toDto(){

AlumnoDTO dto = new AlumnoDTO();

return dto;

}