mirror of
https://gitlab.com/tutorial-java-rafa-munoz/tutorial-java-2024-25/tutorialjava2024-25.git
synced 2025-11-09 18:03:09 +01:00
feat(ch 8 & 9): added
This commit is contained in:
210
src/tutorialJava/modelosBasesDeDatosComunesJPA/Controlador.java
Normal file
210
src/tutorialJava/modelosBasesDeDatosComunesJPA/Controlador.java
Normal file
@@ -0,0 +1,210 @@
|
||||
package tutorialJava.modelosBasesDeDatosComunesJPA;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.NoResultException;
|
||||
import javax.persistence.Persistence;
|
||||
import javax.persistence.Query;
|
||||
import javax.persistence.TypedQuery;
|
||||
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.Entidad;
|
||||
|
||||
public class Controlador {
|
||||
|
||||
private static EntityManagerFactory entityManagerFactory;
|
||||
|
||||
private String nombreEntidadControlada = "";
|
||||
private Class entidadControlada = null;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param nombreEntidad
|
||||
*/
|
||||
public Controlador(Class entidadControlada, String unidadPersistencia) {
|
||||
if (entityManagerFactory == null) {
|
||||
entityManagerFactory = Persistence.createEntityManagerFactory(unidadPersistencia);
|
||||
}
|
||||
this.entidadControlada = entidadControlada;
|
||||
this.nombreEntidadControlada = this.entidadControlada.getName();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static EntityManagerFactory getEntityManagerFactory() {
|
||||
return entityManagerFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
public Entidad find(int id) {
|
||||
try {
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
Entidad entidad = (Entidad) em.find(this.entidadControlada, id);
|
||||
em.close();
|
||||
return entidad;
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void deleteAll() {
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
em.createQuery("DELETE FROM " + this.nombreEntidadControlada + " o").executeUpdate();
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void persist(Entidad e) {
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
em.persist(e);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void merge(Entidad e) {
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
em.getTransaction().begin();
|
||||
em.merge(e);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param e
|
||||
*/
|
||||
public void save(Entidad e) {
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
if (e.getId() != 0) {
|
||||
merge(e);
|
||||
}
|
||||
else {
|
||||
persist(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void remove(Entidad e) {
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
Entidad actual = null;
|
||||
em.getTransaction().begin();
|
||||
if (!em.contains(e)) {
|
||||
actual = em.merge(e);
|
||||
}
|
||||
em.remove(actual);
|
||||
em.getTransaction().commit();
|
||||
em.close();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Entidad findFirst () {
|
||||
try {
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
Query q = em.createQuery("SELECT e FROM " + this.nombreEntidadControlada + " e order by e.id", Entidad.class);
|
||||
Entidad resultado = (Entidad) q.setMaxResults(1).getSingleResult();
|
||||
em.close();
|
||||
return resultado;
|
||||
}
|
||||
catch (NoResultException nrEx) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Entidad findLast () {
|
||||
try {
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
Query q = em.createQuery("SELECT e FROM " + this.nombreEntidadControlada + " e order by e.id desc", Entidad.class);
|
||||
Entidad resultado = (Entidad) q.setMaxResults(1).getSingleResult();
|
||||
em.close();
|
||||
return resultado;
|
||||
}
|
||||
catch (NoResultException nrEx) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Entidad findNext (Entidad e) {
|
||||
try {
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
Query q = em.createQuery("SELECT e FROM " + this.nombreEntidadControlada + " e where e.id > :idActual order by e.id", Entidad.class);
|
||||
q.setParameter("idActual", e.getId());
|
||||
Entidad resultado = (Entidad) q.setMaxResults(1).getSingleResult();
|
||||
em.close();
|
||||
return resultado;
|
||||
}
|
||||
catch (NoResultException nrEx) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Entidad findPrevious (Entidad e) {
|
||||
try {
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
Query q = em.createQuery("SELECT e FROM " + this.nombreEntidadControlada + " e where e.id < :idActual order by e.id desc", Entidad.class);
|
||||
q.setParameter("idActual", e.getId());
|
||||
Entidad resultado = (Entidad) q.setMaxResults(1).getSingleResult();
|
||||
em.close();
|
||||
return resultado;
|
||||
}
|
||||
catch (NoResultException nrEx) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public List<Entidad> findAll () {
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
|
||||
TypedQuery<Entidad> q = em.createQuery("SELECT e FROM " + this.nombreEntidadControlada + " e", this.entidadControlada);
|
||||
|
||||
List<Entidad> entidades = (List<Entidad>) q.getResultList();
|
||||
|
||||
em.close();
|
||||
return entidades;
|
||||
}
|
||||
|
||||
}
|
||||
10
src/tutorialJava/modelosBasesDeDatosComunesJPA/Entidad.java
Normal file
10
src/tutorialJava/modelosBasesDeDatosComunesJPA/Entidad.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package tutorialJava.modelosBasesDeDatosComunesJPA;
|
||||
|
||||
public abstract class Entidad {
|
||||
|
||||
public Entidad () {
|
||||
super();
|
||||
}
|
||||
|
||||
public abstract int getId();
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package tutorialJava.modelosBasesDeDatosComunesJPA.evaluacionCentroEducativo;
|
||||
|
||||
import java.io.Serializable;
|
||||
import javax.persistence.*;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* The persistent class for the curso database table.
|
||||
*
|
||||
*/
|
||||
@Entity
|
||||
@Table(name="curso")
|
||||
@NamedQuery(name="Curso.findAll", query="SELECT c FROM Curso c")
|
||||
public class Curso extends tutorialJava.modelosBasesDeDatosComunesJPA.Entidad implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.AUTO)
|
||||
private int id;
|
||||
|
||||
private String descripcion;
|
||||
|
||||
//bi-directional many-to-one association to Materia
|
||||
@OneToMany(mappedBy="curso")
|
||||
private List<Materia> materias;
|
||||
|
||||
public Curso() {
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getDescripcion() {
|
||||
return this.descripcion;
|
||||
}
|
||||
|
||||
public void setDescripcion(String descripcion) {
|
||||
this.descripcion = descripcion;
|
||||
}
|
||||
|
||||
public List<Materia> getMaterias() {
|
||||
return this.materias;
|
||||
}
|
||||
|
||||
public void setMaterias(List<Materia> materias) {
|
||||
this.materias = materias;
|
||||
}
|
||||
|
||||
public Materia addMateria(Materia materia) {
|
||||
getMaterias().add(materia);
|
||||
materia.setCurso(this);
|
||||
|
||||
return materia;
|
||||
}
|
||||
|
||||
public Materia removeMateria(Materia materia) {
|
||||
getMaterias().remove(materia);
|
||||
materia.setCurso(null);
|
||||
|
||||
return materia;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return descripcion;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
package tutorialJava.modelosBasesDeDatosComunesJPA.evaluacionCentroEducativo;
|
||||
|
||||
import java.io.Serializable;
|
||||
import javax.persistence.*;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* The persistent class for the estudiante database table.
|
||||
*
|
||||
*/
|
||||
@Entity
|
||||
@NamedQuery(name="Estudiante.findAll", query="SELECT e FROM Estudiante e")
|
||||
public class Estudiante extends tutorialJava.modelosBasesDeDatosComunesJPA.Entidad implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.AUTO)
|
||||
private int id;
|
||||
|
||||
@Column(name="apellido1")
|
||||
private String primerApellido;
|
||||
|
||||
@Column(name="apellido2")
|
||||
private String segundoApellido;
|
||||
|
||||
private String direccion;
|
||||
|
||||
private String dni;
|
||||
|
||||
private String email;
|
||||
|
||||
@Lob
|
||||
private byte[] imagen;
|
||||
|
||||
private String nombre;
|
||||
|
||||
private String telefono;
|
||||
|
||||
//bi-directional many-to-one association to TipologiaSexo
|
||||
@ManyToOne
|
||||
@JoinColumn(name="idTipologiaSexo")
|
||||
private TipologiaSexo tipologiaSexo;
|
||||
|
||||
//bi-directional many-to-one association to ValoracionMateria
|
||||
@OneToMany(mappedBy="estudiante")
|
||||
private List<ValoracionMateria> valoracionMaterias;
|
||||
|
||||
public Estudiante() {
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getPrimerApellido() {
|
||||
return this.primerApellido;
|
||||
}
|
||||
|
||||
public void setPrimerApellido(String primerApellido) {
|
||||
this.primerApellido = primerApellido;
|
||||
}
|
||||
|
||||
public String getSegundoApellido() {
|
||||
return this.segundoApellido;
|
||||
}
|
||||
|
||||
public void setSegundoApellido(String segundoApellido) {
|
||||
this.segundoApellido = segundoApellido;
|
||||
}
|
||||
|
||||
public String getDireccion() {
|
||||
return this.direccion;
|
||||
}
|
||||
|
||||
public void setDireccion(String direccion) {
|
||||
this.direccion = direccion;
|
||||
}
|
||||
|
||||
public String getDni() {
|
||||
return this.dni;
|
||||
}
|
||||
|
||||
public void setDni(String dni) {
|
||||
this.dni = dni;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return this.email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public byte[] getImagen() {
|
||||
return this.imagen;
|
||||
}
|
||||
|
||||
public void setImagen(byte[] imagen) {
|
||||
this.imagen = imagen;
|
||||
}
|
||||
|
||||
public String getNombre() {
|
||||
return this.nombre;
|
||||
}
|
||||
|
||||
public void setNombre(String nombre) {
|
||||
this.nombre = nombre;
|
||||
}
|
||||
|
||||
public String getTelefono() {
|
||||
return this.telefono;
|
||||
}
|
||||
|
||||
public void setTelefono(String telefono) {
|
||||
this.telefono = telefono;
|
||||
}
|
||||
|
||||
public TipologiaSexo getTipologiaSexo() {
|
||||
return this.tipologiaSexo;
|
||||
}
|
||||
|
||||
public void setTipologiaSexo(TipologiaSexo tipologiaSexo) {
|
||||
this.tipologiaSexo = tipologiaSexo;
|
||||
}
|
||||
|
||||
public List<ValoracionMateria> getValoracionMaterias() {
|
||||
return this.valoracionMaterias;
|
||||
}
|
||||
|
||||
public void setValoracionMaterias(List<ValoracionMateria> valoracionMaterias) {
|
||||
this.valoracionMaterias = valoracionMaterias;
|
||||
}
|
||||
|
||||
public ValoracionMateria addValoracionMateria(ValoracionMateria valoracionMateria) {
|
||||
getValoracionMaterias().add(valoracionMateria);
|
||||
valoracionMateria.setEstudiante(this);
|
||||
|
||||
return valoracionMateria;
|
||||
}
|
||||
|
||||
public ValoracionMateria removeValoracionMateria(ValoracionMateria valoracionMateria) {
|
||||
getValoracionMaterias().remove(valoracionMateria);
|
||||
valoracionMateria.setEstudiante(null);
|
||||
|
||||
return valoracionMateria;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return nombre + " " + primerApellido + " " + segundoApellido;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package tutorialJava.modelosBasesDeDatosComunesJPA.evaluacionCentroEducativo;
|
||||
|
||||
import java.io.Serializable;
|
||||
import javax.persistence.*;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* The persistent class for the materia database table.
|
||||
*
|
||||
*/
|
||||
@Entity
|
||||
@NamedQuery(name="Materia.findAll", query="SELECT m FROM Materia m")
|
||||
public class Materia extends tutorialJava.modelosBasesDeDatosComunesJPA.Entidad implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.AUTO)
|
||||
private int id;
|
||||
|
||||
private String acronimo;
|
||||
|
||||
private String nombre;
|
||||
|
||||
//bi-directional many-to-one association to Curso
|
||||
@ManyToOne
|
||||
private Curso curso;
|
||||
|
||||
//bi-directional many-to-one association to ValoracionMateria
|
||||
@OneToMany(mappedBy="materia")
|
||||
private List<ValoracionMateria> valoracionMaterias;
|
||||
|
||||
public Materia() {
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getAcronimo() {
|
||||
return this.acronimo;
|
||||
}
|
||||
|
||||
public void setAcronimo(String acronimo) {
|
||||
this.acronimo = acronimo;
|
||||
}
|
||||
|
||||
public String getNombre() {
|
||||
return this.nombre;
|
||||
}
|
||||
|
||||
public void setNombre(String nombre) {
|
||||
this.nombre = nombre;
|
||||
}
|
||||
|
||||
public Curso getCurso() {
|
||||
return this.curso;
|
||||
}
|
||||
|
||||
public void setCurso(Curso curso) {
|
||||
this.curso = curso;
|
||||
}
|
||||
|
||||
public List<ValoracionMateria> getValoracionMaterias() {
|
||||
return this.valoracionMaterias;
|
||||
}
|
||||
|
||||
public void setValoracionMaterias(List<ValoracionMateria> valoracionMaterias) {
|
||||
this.valoracionMaterias = valoracionMaterias;
|
||||
}
|
||||
|
||||
public ValoracionMateria addValoracionMateria(ValoracionMateria valoracionMateria) {
|
||||
getValoracionMaterias().add(valoracionMateria);
|
||||
valoracionMateria.setMateria(this);
|
||||
|
||||
return valoracionMateria;
|
||||
}
|
||||
|
||||
public ValoracionMateria removeValoracionMateria(ValoracionMateria valoracionMateria) {
|
||||
getValoracionMaterias().remove(valoracionMateria);
|
||||
valoracionMateria.setMateria(null);
|
||||
|
||||
return valoracionMateria;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return nombre;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
package tutorialJava.modelosBasesDeDatosComunesJPA.evaluacionCentroEducativo;
|
||||
|
||||
import java.io.Serializable;
|
||||
import javax.persistence.*;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonAppend;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* The persistent class for the profesor database table.
|
||||
*
|
||||
*/
|
||||
@JsonAppend(
|
||||
attrs = {
|
||||
@JsonAppend.Attr(value = "idTipologiaSexo")
|
||||
}
|
||||
)
|
||||
|
||||
@Entity
|
||||
@NamedQuery(name="Profesor.findAll", query="SELECT p FROM Profesor p")
|
||||
public class Profesor extends tutorialJava.modelosBasesDeDatosComunesJPA.Entidad implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.AUTO)
|
||||
private int id;
|
||||
|
||||
@Column(name="apellido1")
|
||||
private String primerApellido;
|
||||
|
||||
@Column(name="apellido2")
|
||||
private String segundoApellido;
|
||||
|
||||
private String direccion;
|
||||
|
||||
private String dni;
|
||||
|
||||
private String email;
|
||||
|
||||
@Lob
|
||||
private byte[] imagen;
|
||||
|
||||
private String nombre;
|
||||
|
||||
private String telefono;
|
||||
|
||||
//bi-directional many-to-one association to TipologiaSexo
|
||||
@ManyToOne
|
||||
@JoinColumn(name="idTipologiaSexo")
|
||||
private TipologiaSexo tipologiaSexo;
|
||||
|
||||
//bi-directional many-to-one association to ValoracionMateria
|
||||
@JsonIgnore
|
||||
@OneToMany(mappedBy="profesor")
|
||||
private List<ValoracionMateria> valoracionMaterias;
|
||||
|
||||
public Profesor() {
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getPrimerApellido() {
|
||||
return this.primerApellido;
|
||||
}
|
||||
|
||||
public void setPrimerApellido(String primerApellido) {
|
||||
this.primerApellido = primerApellido;
|
||||
}
|
||||
|
||||
public String getSegundoApellido() {
|
||||
return this.segundoApellido;
|
||||
}
|
||||
|
||||
public void setSegundoApellido(String segundoApellido) {
|
||||
this.segundoApellido = segundoApellido;
|
||||
}
|
||||
|
||||
public String getDireccion() {
|
||||
return this.direccion;
|
||||
}
|
||||
|
||||
public void setDireccion(String direccion) {
|
||||
this.direccion = direccion;
|
||||
}
|
||||
|
||||
public String getDni() {
|
||||
return this.dni;
|
||||
}
|
||||
|
||||
public void setDni(String dni) {
|
||||
this.dni = dni;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return this.email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public byte[] getImagen() {
|
||||
return this.imagen;
|
||||
}
|
||||
|
||||
public void setImagen(byte[] imagen) {
|
||||
this.imagen = imagen;
|
||||
}
|
||||
|
||||
public String getNombre() {
|
||||
return this.nombre;
|
||||
}
|
||||
|
||||
public void setNombre(String nombre) {
|
||||
this.nombre = nombre;
|
||||
}
|
||||
|
||||
public String getTelefono() {
|
||||
return this.telefono;
|
||||
}
|
||||
|
||||
public void setTelefono(String telefono) {
|
||||
this.telefono = telefono;
|
||||
}
|
||||
|
||||
public TipologiaSexo getTipologiaSexo() {
|
||||
return this.tipologiaSexo;
|
||||
}
|
||||
|
||||
public void setTipologiaSexo(TipologiaSexo tipologiaSexo) {
|
||||
this.tipologiaSexo = tipologiaSexo;
|
||||
}
|
||||
|
||||
public List<ValoracionMateria> getValoracionMaterias() {
|
||||
return this.valoracionMaterias;
|
||||
}
|
||||
|
||||
public void setValoracionMaterias(List<ValoracionMateria> valoracionMaterias) {
|
||||
this.valoracionMaterias = valoracionMaterias;
|
||||
}
|
||||
|
||||
public ValoracionMateria addValoracionMateria(ValoracionMateria valoracionMateria) {
|
||||
getValoracionMaterias().add(valoracionMateria);
|
||||
valoracionMateria.setProfesor(this);
|
||||
|
||||
return valoracionMateria;
|
||||
}
|
||||
|
||||
public ValoracionMateria removeValoracionMateria(ValoracionMateria valoracionMateria) {
|
||||
getValoracionMaterias().remove(valoracionMateria);
|
||||
valoracionMateria.setProfesor(null);
|
||||
|
||||
return valoracionMateria;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return nombre + " " + primerApellido + " " + segundoApellido;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package tutorialJava.modelosBasesDeDatosComunesJPA.evaluacionCentroEducativo;
|
||||
|
||||
import java.io.Serializable;
|
||||
import javax.persistence.*;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* The persistent class for the tipologiasexo database table.
|
||||
*
|
||||
*/
|
||||
@Entity
|
||||
@NamedQuery(name="TipologiaSexo.findAll", query="SELECT t FROM TipologiaSexo t")
|
||||
public class TipologiaSexo extends tutorialJava.modelosBasesDeDatosComunesJPA.Entidad implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.AUTO)
|
||||
private int id;
|
||||
|
||||
private String descripcion;
|
||||
|
||||
//bi-directional many-to-one association to Estudiante
|
||||
@JsonIgnore
|
||||
@OneToMany(mappedBy="tipologiaSexo")
|
||||
private List<Estudiante> estudiantes;
|
||||
|
||||
//bi-directional many-to-one association to Profesor
|
||||
@JsonIgnore
|
||||
@OneToMany(mappedBy="tipologiaSexo")
|
||||
private List<Profesor> profesors;
|
||||
|
||||
public TipologiaSexo() {
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getDescripcion() {
|
||||
return this.descripcion;
|
||||
}
|
||||
|
||||
public void setDescripcion(String descripcion) {
|
||||
this.descripcion = descripcion;
|
||||
}
|
||||
|
||||
public List<Estudiante> getEstudiantes() {
|
||||
return this.estudiantes;
|
||||
}
|
||||
|
||||
public void setEstudiantes(List<Estudiante> estudiantes) {
|
||||
this.estudiantes = estudiantes;
|
||||
}
|
||||
|
||||
public Estudiante addEstudiante(Estudiante estudiante) {
|
||||
getEstudiantes().add(estudiante);
|
||||
estudiante.setTipologiaSexo(this);
|
||||
|
||||
return estudiante;
|
||||
}
|
||||
|
||||
public Estudiante removeEstudiante(Estudiante estudiante) {
|
||||
getEstudiantes().remove(estudiante);
|
||||
estudiante.setTipologiaSexo(null);
|
||||
|
||||
return estudiante;
|
||||
}
|
||||
|
||||
public List<Profesor> getProfesors() {
|
||||
return this.profesors;
|
||||
}
|
||||
|
||||
public void setProfesors(List<Profesor> profesors) {
|
||||
this.profesors = profesors;
|
||||
}
|
||||
|
||||
public Profesor addProfesor(Profesor profesor) {
|
||||
getProfesors().add(profesor);
|
||||
profesor.setTipologiaSexo(this);
|
||||
|
||||
return profesor;
|
||||
}
|
||||
|
||||
public Profesor removeProfesor(Profesor profesor) {
|
||||
getProfesors().remove(profesor);
|
||||
profesor.setTipologiaSexo(null);
|
||||
|
||||
return profesor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return descripcion;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package tutorialJava.modelosBasesDeDatosComunesJPA.evaluacionCentroEducativo;
|
||||
|
||||
import java.io.Serializable;
|
||||
import javax.persistence.*;
|
||||
import java.util.Date;
|
||||
|
||||
|
||||
/**
|
||||
* The persistent class for the valoracionmateria database table.
|
||||
*
|
||||
*/
|
||||
@Entity
|
||||
@NamedQuery(name="ValoracionMateria.findAll", query="SELECT v FROM ValoracionMateria v")
|
||||
public class ValoracionMateria extends tutorialJava.modelosBasesDeDatosComunesJPA.Entidad implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.AUTO)
|
||||
private int id;
|
||||
|
||||
@Temporal(TemporalType.DATE)
|
||||
private Date fecha;
|
||||
|
||||
private float valoracion;
|
||||
|
||||
//bi-directional many-to-one association to Estudiante
|
||||
@ManyToOne
|
||||
@JoinColumn(name="idEstudiante")
|
||||
private Estudiante estudiante;
|
||||
|
||||
//bi-directional many-to-one association to Materia
|
||||
@ManyToOne
|
||||
@JoinColumn(name="idMateria")
|
||||
private Materia materia;
|
||||
|
||||
//bi-directional many-to-one association to Profesor
|
||||
@ManyToOne
|
||||
@JoinColumn(name="idProfesor")
|
||||
private Profesor profesor;
|
||||
|
||||
public ValoracionMateria() {
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Date getFecha() {
|
||||
return this.fecha;
|
||||
}
|
||||
|
||||
public void setFecha(Date fecha) {
|
||||
this.fecha = fecha;
|
||||
}
|
||||
|
||||
public float getValoracion() {
|
||||
return this.valoracion;
|
||||
}
|
||||
|
||||
public void setValoracion(float valoracion) {
|
||||
this.valoracion = valoracion;
|
||||
}
|
||||
|
||||
public Estudiante getEstudiante() {
|
||||
return this.estudiante;
|
||||
}
|
||||
|
||||
public void setEstudiante(Estudiante estudiante) {
|
||||
this.estudiante = estudiante;
|
||||
}
|
||||
|
||||
public Materia getMateria() {
|
||||
return this.materia;
|
||||
}
|
||||
|
||||
public void setMateria(Materia materia) {
|
||||
this.materia = materia;
|
||||
}
|
||||
|
||||
public Profesor getProfesor() {
|
||||
return this.profesor;
|
||||
}
|
||||
|
||||
public void setProfesor(Profesor profesor) {
|
||||
this.profesor = profesor;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package tutorialJava.modelosBasesDeDatosComunesJPA.evaluacionCentroEducativo.controladores;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.NoResultException;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.Controlador;
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.Entidad;
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.evaluacionCentroEducativo.Curso;
|
||||
|
||||
public class CursoControlador extends Controlador {
|
||||
|
||||
// instancia del singleton
|
||||
private static CursoControlador instancia = null;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public CursoControlador() {
|
||||
super(Curso.class, "EvaluacionCentroEducativo");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static CursoControlador getInstancia() {
|
||||
if (instancia == null) {
|
||||
instancia = new CursoControlador();
|
||||
}
|
||||
return instancia;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Curso findFirst() {
|
||||
return (Curso) super.findFirst();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Curso findLast() {
|
||||
return (Curso) super.findLast();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Curso findNext(Entidad e) {
|
||||
return (Curso) super.findNext(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Curso findPrevious(Entidad e) {
|
||||
return (Curso) super.findPrevious(e);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public List<Curso> findAllCursos () {
|
||||
List<Curso> entities = new ArrayList<Curso>();
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
try {
|
||||
Query q = em.createNativeQuery("SELECT * FROM curso", Curso.class);
|
||||
entities = (List<Curso>) q.getResultList();
|
||||
}
|
||||
catch (NoResultException nrEx) {
|
||||
}
|
||||
em.close();
|
||||
return entities;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package tutorialJava.modelosBasesDeDatosComunesJPA.evaluacionCentroEducativo.controladores;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.Controlador;
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.Entidad;
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.evaluacionCentroEducativo.Estudiante;
|
||||
|
||||
public class EstudianteControlador extends Controlador {
|
||||
|
||||
// instancia del singleton
|
||||
private static EstudianteControlador instancia = null;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public EstudianteControlador() {
|
||||
super(Estudiante.class, "EvaluacionCentroEducativo");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static EstudianteControlador getInstancia() {
|
||||
if (instancia == null) {
|
||||
instancia = new EstudianteControlador();
|
||||
}
|
||||
return instancia;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Estudiante findFirst() {
|
||||
return (Estudiante) super.findFirst();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Estudiante findLast() {
|
||||
return (Estudiante) super.findLast();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Estudiante findNext(Entidad e) {
|
||||
return (Estudiante) super.findNext(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Estudiante findPrevious(Entidad e) {
|
||||
return (Estudiante) super.findPrevious(e);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public List<Estudiante> findAllEstudiantes() {
|
||||
List<Estudiante> resultado = new ArrayList<Estudiante>();
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
|
||||
try {
|
||||
Query q = em.createNativeQuery("Select * from estudiante", Estudiante.class);
|
||||
resultado = (List<Estudiante>) q.getResultList();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
em.close();
|
||||
return resultado;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package tutorialJava.modelosBasesDeDatosComunesJPA.evaluacionCentroEducativo.controladores;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.NoResultException;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.Controlador;
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.Entidad;
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.evaluacionCentroEducativo.Curso;
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.evaluacionCentroEducativo.Materia;
|
||||
|
||||
public class MateriaControlador extends Controlador {
|
||||
|
||||
// instancia del singleton
|
||||
private static MateriaControlador instancia = null;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public MateriaControlador() {
|
||||
super(Materia.class, "EvaluacionCentroEducativo");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static MateriaControlador getInstancia() {
|
||||
if (instancia == null) {
|
||||
instancia = new MateriaControlador();
|
||||
}
|
||||
return instancia;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Materia findFirst() {
|
||||
return (Materia) super.findFirst();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Materia findLast() {
|
||||
return (Materia) super.findLast();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Materia findNext(Entidad e) {
|
||||
return (Materia) super.findNext(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Materia findPrevious(Entidad e) {
|
||||
return (Materia) super.findPrevious(e);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public List<Materia> findAllMaterias () {
|
||||
List<Materia> entities = new ArrayList<Materia>();
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
try {
|
||||
Query q = em.createNativeQuery("SELECT * FROM materia", Materia.class);
|
||||
entities = (List<Materia>) q.getResultList();
|
||||
}
|
||||
catch (NoResultException nrEx) {
|
||||
}
|
||||
em.close();
|
||||
return entities;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package tutorialJava.modelosBasesDeDatosComunesJPA.evaluacionCentroEducativo.controladores;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.NoResultException;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.Controlador;
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.Entidad;
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.evaluacionCentroEducativo.Profesor;
|
||||
|
||||
public class ProfesorControlador extends Controlador {
|
||||
|
||||
// instancia del singleton
|
||||
private static ProfesorControlador instancia = null;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public ProfesorControlador() {
|
||||
super(Profesor.class, "EvaluacionCentroEducativo");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static ProfesorControlador getInstancia() {
|
||||
if (instancia == null) {
|
||||
instancia = new ProfesorControlador();
|
||||
}
|
||||
return instancia;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Profesor findFirst() {
|
||||
return (Profesor) super.findFirst();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Profesor findLast() {
|
||||
return (Profesor) super.findLast();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Profesor findNext(Entidad e) {
|
||||
return (Profesor) super.findNext(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Profesor findPrevious(Entidad e) {
|
||||
return (Profesor) super.findPrevious(e);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public List<Profesor> findAllProfesores () {
|
||||
List<Profesor> entities = new ArrayList<Profesor>();
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
try {
|
||||
Query q = em.createNativeQuery("SELECT * FROM profesor", Profesor.class);
|
||||
entities = (List<Profesor>) q.getResultList();
|
||||
}
|
||||
catch (NoResultException nrEx) {
|
||||
}
|
||||
em.close();
|
||||
return entities;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package tutorialJava.modelosBasesDeDatosComunesJPA.evaluacionCentroEducativo.controladores;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.NoResultException;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.Controlador;
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.evaluacionCentroEducativo.TipologiaSexo;
|
||||
|
||||
public class TipologiaSexoControlador extends Controlador {
|
||||
|
||||
// instancia del singleton
|
||||
private static TipologiaSexoControlador instancia = null;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public TipologiaSexoControlador() {
|
||||
super(TipologiaSexo.class, "EvaluacionCentroEducativo");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static TipologiaSexoControlador getInstancia() {
|
||||
if (instancia == null) {
|
||||
instancia = new TipologiaSexoControlador();
|
||||
}
|
||||
return instancia;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public List<TipologiaSexo> findAllTipologiasSexo () {
|
||||
List<TipologiaSexo> entities = new ArrayList<TipologiaSexo>();
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
try {
|
||||
Query q = em.createNativeQuery("SELECT * FROM tipologiasexo", TipologiaSexo.class);
|
||||
entities = (List<TipologiaSexo>) q.getResultList();
|
||||
}
|
||||
catch (NoResultException nrEx) {
|
||||
}
|
||||
em.close();
|
||||
return entities;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package tutorialJava.modelosBasesDeDatosComunesJPA.evaluacionCentroEducativo.controladores;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.Controlador;
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.Entidad;
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.evaluacionCentroEducativo.Estudiante;
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.evaluacionCentroEducativo.Materia;
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.evaluacionCentroEducativo.Profesor;
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.evaluacionCentroEducativo.ValoracionMateria;
|
||||
|
||||
public class ValoracionMateriaControlador extends Controlador {
|
||||
|
||||
// instancia del singleton
|
||||
private static ValoracionMateriaControlador instancia = null;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public ValoracionMateriaControlador() {
|
||||
super(ValoracionMateria.class, "EvaluacionCentroEducativo");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static ValoracionMateriaControlador getInstancia() {
|
||||
if (instancia == null) {
|
||||
instancia = new ValoracionMateriaControlador();
|
||||
}
|
||||
return instancia;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ValoracionMateria findFirst() {
|
||||
return (ValoracionMateria) super.findFirst();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ValoracionMateria findLast() {
|
||||
return (ValoracionMateria) super.findLast();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ValoracionMateria findNext(Entidad e) {
|
||||
return (ValoracionMateria) super.findNext(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ValoracionMateria findPrevious(Entidad e) {
|
||||
return (ValoracionMateria) super.findPrevious(e);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param profesor
|
||||
* @param materia
|
||||
* @param estudiante
|
||||
* @return
|
||||
*/
|
||||
public ValoracionMateria findByEstudianteAndProfesorAndMateria (Profesor profesor, Materia materia, Estudiante estudiante) {
|
||||
ValoracionMateria resultado = null;
|
||||
EntityManager em = this.getEntityManagerFactory().createEntityManager();
|
||||
try {
|
||||
Query q = em.createNativeQuery("Select * from valoracionmateria where idProfesor = ? and "
|
||||
+ "idMateria = ? and idEstudiante = ?", ValoracionMateria.class);
|
||||
q.setParameter(1, profesor.getId());
|
||||
q.setParameter(2, materia.getId());
|
||||
q.setParameter(3, estudiante.getId());
|
||||
resultado = (ValoracionMateria) q.getSingleResult();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
}
|
||||
em.close();
|
||||
return resultado;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package tutorialJava.modelosBasesDeDatosComunesJPA.evaluacionCentroEducativo.pojos;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonUnwrapped;
|
||||
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.evaluacionCentroEducativo.Profesor;
|
||||
|
||||
public class ProfesorPOJO {
|
||||
@JsonUnwrapped
|
||||
private Profesor profesor;
|
||||
private int idTipologiaSexo = 0;
|
||||
/**
|
||||
* @param profesor
|
||||
* @param idTipologiaSexo
|
||||
*/
|
||||
public ProfesorPOJO(Profesor profesor) {
|
||||
super();
|
||||
this.profesor = profesor;
|
||||
|
||||
if (profesor.getTipologiaSexo() != null) {
|
||||
this.idTipologiaSexo = profesor.getTipologiaSexo().getId();
|
||||
}
|
||||
}
|
||||
public Profesor getProfesor() {
|
||||
return profesor;
|
||||
}
|
||||
public int getIdTipologiaSexo() {
|
||||
return idTipologiaSexo;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package tutorialJava.modelosBasesDeDatosComunesJPA.poblacionPorProvincias;
|
||||
|
||||
import java.io.Serializable;
|
||||
import javax.persistence.*;
|
||||
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.Entidad;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
|
||||
/**
|
||||
* The persistent class for the persona database table.
|
||||
*
|
||||
*/
|
||||
@Entity
|
||||
@Table(name="persona")
|
||||
@NamedQuery(name="Persona.findAll", query="SELECT p FROM Persona p")
|
||||
public class Persona extends Entidad implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.AUTO)
|
||||
private int id;
|
||||
|
||||
private boolean activo;
|
||||
|
||||
@Column(name="apellido1")
|
||||
private String primerApellido;
|
||||
|
||||
@Column(name="apellido2")
|
||||
private String segundoApellido;
|
||||
|
||||
private int edad;
|
||||
|
||||
@Temporal(TemporalType.DATE)
|
||||
private Date fechaNacimiento;
|
||||
|
||||
private String nombre;
|
||||
|
||||
//bi-directional many-to-one association to Provincia
|
||||
@ManyToOne
|
||||
@JoinColumn(name="idProvincia")
|
||||
private Provincia provincia;
|
||||
|
||||
public Persona() {
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public boolean getActivo() {
|
||||
return this.activo;
|
||||
}
|
||||
|
||||
public void setActivo(boolean activo) {
|
||||
this.activo = activo;
|
||||
}
|
||||
|
||||
public String getPrimerApellido() {
|
||||
return this.primerApellido;
|
||||
}
|
||||
|
||||
public void setPrimerApellido(String primerApellido) {
|
||||
this.primerApellido = primerApellido;
|
||||
}
|
||||
|
||||
public String getSegundoApellido() {
|
||||
return this.segundoApellido;
|
||||
}
|
||||
|
||||
public void setSegundoApellido(String segundoApellido) {
|
||||
this.segundoApellido = segundoApellido;
|
||||
}
|
||||
|
||||
public int getEdad() {
|
||||
return this.edad;
|
||||
}
|
||||
|
||||
public void setEdad(int edad) {
|
||||
this.edad = edad;
|
||||
}
|
||||
|
||||
public Date getFechaNacimiento() {
|
||||
return this.fechaNacimiento;
|
||||
}
|
||||
|
||||
public void setFechaNacimiento(Date fechaNacimiento) {
|
||||
this.fechaNacimiento = fechaNacimiento;
|
||||
}
|
||||
|
||||
public String getNombre() {
|
||||
return this.nombre;
|
||||
}
|
||||
|
||||
public void setNombre(String nombre) {
|
||||
this.nombre = nombre;
|
||||
}
|
||||
|
||||
public Provincia getProvincia() {
|
||||
return this.provincia;
|
||||
}
|
||||
|
||||
public void setProvincia(Provincia provincia) {
|
||||
this.provincia = provincia;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package tutorialJava.modelosBasesDeDatosComunesJPA.poblacionPorProvincias;
|
||||
|
||||
import java.io.Serializable;
|
||||
import javax.persistence.*;
|
||||
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.Entidad;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* The persistent class for the provincia database table.
|
||||
*
|
||||
*/
|
||||
@Entity
|
||||
@Table(name="provincia")
|
||||
@NamedQuery(name="Provincia.findAll", query="SELECT p FROM Provincia p")
|
||||
public class Provincia extends Entidad implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.AUTO)
|
||||
private int id;
|
||||
|
||||
private String descripcion;
|
||||
|
||||
//bi-directional many-to-one association to Persona
|
||||
@OneToMany(mappedBy="provincia")
|
||||
private List<Persona> personas;
|
||||
|
||||
public Provincia() {
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getDescripcion() {
|
||||
return this.descripcion;
|
||||
}
|
||||
|
||||
public void setDescripcion(String descripcion) {
|
||||
this.descripcion = descripcion;
|
||||
}
|
||||
|
||||
public List<Persona> getPersonas() {
|
||||
return this.personas;
|
||||
}
|
||||
|
||||
public void setPersonas(List<Persona> personas) {
|
||||
this.personas = personas;
|
||||
}
|
||||
|
||||
public Persona addPersona(Persona persona) {
|
||||
getPersonas().add(persona);
|
||||
persona.setProvincia(this);
|
||||
|
||||
return persona;
|
||||
}
|
||||
|
||||
public Persona removePersona(Persona persona) {
|
||||
getPersonas().remove(persona);
|
||||
persona.setProvincia(null);
|
||||
|
||||
return persona;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.descripcion;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
package tutorialJava.modelosBasesDeDatosComunesJPA.poblacionPorProvincias.controladores;
|
||||
|
||||
import java.time.ZoneId;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.NoResultException;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import tutorialJava.Utils;
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.Controlador;
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.poblacionPorProvincias.Persona;
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.poblacionPorProvincias.Provincia;
|
||||
|
||||
|
||||
|
||||
public class PersonaControlador extends Controlador {
|
||||
|
||||
private static PersonaControlador controller = null;
|
||||
|
||||
|
||||
|
||||
public PersonaControlador() {
|
||||
super(Persona.class, "PoblacionProvincial");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static PersonaControlador getControlador () {
|
||||
if (controller == null) {
|
||||
controller = new PersonaControlador();
|
||||
}
|
||||
return controller;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public Provincia find (int id) {
|
||||
return (Provincia) super.find(id);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public List<Persona> findAllPersonas () {
|
||||
List<Persona> entities = new ArrayList<Persona>();
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
try {
|
||||
Query q = em.createNativeQuery("SELECT * FROM persona", Persona.class);
|
||||
entities = (List<Persona>) q.getResultList();
|
||||
}
|
||||
catch (NoResultException nrEx) {
|
||||
}
|
||||
em.close();
|
||||
return entities;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Arrays para generaci<63>n de personas al azar
|
||||
private static String nombres[] = new String[] {"Eva", "Juan", "Carmen", "Pablo", "Rafa", "Pilar", "Pedro",
|
||||
"Lola", "Casimiro", "Gertrudis", "Eustaquio", "Gerarda", "Nepomunosio", "Argimira", "Ascensio", "Baltasara", "Baudilio", "Bernabea"};
|
||||
private static String apellidos[] = new String[] {"Gonzalez", "Lopez", "Gutierrez", "Ruiz", "Jurado", "Carrasco", "Flores",
|
||||
"Sanchez", "Bose", "Martin", "Martinez", "Santos", "Pozo", "Quijano", "Romero", "Pisano", "Cuevas", "Sanz"};
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static void inicializaDatosEnTabla () {
|
||||
int personasPorProvincia = 100;
|
||||
// Para cada provincia, eliminar<61> a las personas creadas y crear<61> nuevas
|
||||
PersonaControlador.getControlador().deleteAll();
|
||||
List<Provincia> provincias = ProvinciaControlador.getControlador().findAllProvincias();
|
||||
for (Provincia provincia : provincias) {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
Persona persona = new Persona();
|
||||
persona.setNombre(getStringAlAzar(nombres));
|
||||
persona.setPrimerApellido(getStringAlAzar(apellidos));
|
||||
persona.setSegundoApellido(getStringAlAzar(apellidos));
|
||||
persona.setProvincia(provincia);
|
||||
persona.setFechaNacimiento(getFechaAzar(1900, 2019));
|
||||
persona.setEdad(calculaEdad(persona.getFechaNacimiento()));
|
||||
persona.setActivo( (Utils.obtenerNumeroAzar(0, 100) <= 50)? true : false );
|
||||
PersonaControlador.getControlador().persist(persona);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param strings
|
||||
* @return
|
||||
*/
|
||||
private static String getStringAlAzar (String strings[]) {
|
||||
return strings[Utils.obtenerNumeroAzar(0, strings.length - 1)];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private static Date getFechaAzar (int anioMin, int anioMax) {
|
||||
Calendar calendar = new GregorianCalendar(Utils.obtenerNumeroAzar(anioMin, anioMax) /*a<>o*/, Utils.obtenerNumeroAzar(0, 11) /*mes*/, Utils.obtenerNumeroAzar(1, 31) /*d<>a*/);
|
||||
|
||||
return calendar.getTime();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private static int calculaEdad (Date fechaNacimiento) {
|
||||
return (int) ChronoUnit.YEARS.between(fechaNacimiento.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(), new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package tutorialJava.modelosBasesDeDatosComunesJPA.poblacionPorProvincias.controladores;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.NoResultException;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.Controlador;
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.poblacionPorProvincias.Provincia;
|
||||
|
||||
|
||||
public class ProvinciaControlador extends Controlador {
|
||||
|
||||
private static ProvinciaControlador controller = null;
|
||||
|
||||
|
||||
|
||||
public ProvinciaControlador() {
|
||||
super(Provincia.class, "PoblacionProvincial");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static ProvinciaControlador getControlador () {
|
||||
if (controller == null) {
|
||||
controller = new ProvinciaControlador();
|
||||
}
|
||||
return controller;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public Provincia find (int id) {
|
||||
return (Provincia) super.find(id);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public List<Provincia> findAllProvincias () {
|
||||
List<Provincia> entities = new ArrayList<Provincia>();
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
try {
|
||||
Query q = em.createNativeQuery("SELECT * FROM provincia", Provincia.class);
|
||||
entities = (List<Provincia>) q.getResultList();
|
||||
}
|
||||
catch (NoResultException nrEx) {
|
||||
}
|
||||
em.close();
|
||||
return entities;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static void inicializaDatosEnTabla () {
|
||||
String nombresProvincias[] = new String[] {"<EFBFBD>lava", "Albacete", "Alicante", "Almer<EFBFBD>a", "Asturias", "<EFBFBD>vila", "Badajoz",
|
||||
"Barcelona", "Burgos", "C<EFBFBD>ceres", "C<EFBFBD>diz", "Cantabria", "Castell<EFBFBD>n", "Ciudad Real", "C<EFBFBD>rdoba", "Cuenca", "Gerona",
|
||||
"Granada", "Guadalajara", "Guip<EFBFBD>zcoa", "Huelva", "Huesca", "Islas Baleares", "Ja<EFBFBD>n", "La Coru<72>a", "La Rioja",
|
||||
"Las Palmas", "Le<EFBFBD>n", "L<EFBFBD>rida", "Lugo", "Madrid", "M<EFBFBD>laga", "Murcia", "Navarra", "Orense", "Palencia", "Pontevedra",
|
||||
"Salamanca", "Segovia", "Sevilla", "Soria", "Tarragona", "Tenerife", "Teruel", "Toledo", "Valencia", "Valladolid",
|
||||
"Vizcaya", "Zamora", "Zaragoza"};
|
||||
Provincia entidades[] = new Provincia[nombresProvincias.length];
|
||||
// Tenemos 50 provincias, si no se llega a tener todas, se eliminan los datos de la BBDD y se crea nuevamente
|
||||
if (ProvinciaControlador.getControlador().findAll().size() != 50) {
|
||||
// Elimino los datos de personas y despu<70>s los de provincias
|
||||
PersonaControlador.getControlador().deleteAll();
|
||||
ProvinciaControlador.getControlador().deleteAll();
|
||||
|
||||
// Recorro el array de Strings, dando de alta entidades en la tabla
|
||||
for (String nombreProvincia : nombresProvincias) {
|
||||
Provincia provincia = new Provincia();
|
||||
provincia.setDescripcion(nombreProvincia);
|
||||
ProvinciaControlador.getControlador().persist(provincia);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
package tutorialJava.modelosBasesDeDatosComunesJPA.ventaDeCoches;
|
||||
|
||||
import java.io.Serializable;
|
||||
import javax.persistence.*;
|
||||
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.Entidad;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* The persistent class for the cliente database table.
|
||||
*
|
||||
*/
|
||||
@Entity
|
||||
@NamedQuery(name="Cliente.findAll", query="SELECT c FROM Cliente c")
|
||||
public class Cliente extends Entidad implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.AUTO)
|
||||
private int id;
|
||||
|
||||
private boolean activo;
|
||||
|
||||
private String apellidos;
|
||||
|
||||
private String dniNie;
|
||||
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
private Date fechaNac;
|
||||
|
||||
private String localidad;
|
||||
|
||||
private String nombre;
|
||||
|
||||
//bi-directional many-to-one association to Venta
|
||||
@OneToMany(mappedBy="cliente")
|
||||
private List<Venta> ventas;
|
||||
|
||||
public Cliente() {
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public boolean getActivo() {
|
||||
return this.activo;
|
||||
}
|
||||
|
||||
public void setActivo(boolean activo) {
|
||||
this.activo = activo;
|
||||
}
|
||||
|
||||
public String getApellidos() {
|
||||
return this.apellidos;
|
||||
}
|
||||
|
||||
public void setApellidos(String apellidos) {
|
||||
this.apellidos = apellidos;
|
||||
}
|
||||
|
||||
public String getDniNie() {
|
||||
return this.dniNie;
|
||||
}
|
||||
|
||||
public void setDniNie(String dniNie) {
|
||||
this.dniNie = dniNie;
|
||||
}
|
||||
|
||||
public Date getFechaNac() {
|
||||
return this.fechaNac;
|
||||
}
|
||||
|
||||
public void setFechaNac(Date fechaNac) {
|
||||
this.fechaNac = fechaNac;
|
||||
}
|
||||
|
||||
public String getLocalidad() {
|
||||
return this.localidad;
|
||||
}
|
||||
|
||||
public void setLocalidad(String localidad) {
|
||||
this.localidad = localidad;
|
||||
}
|
||||
|
||||
public String getNombre() {
|
||||
return this.nombre;
|
||||
}
|
||||
|
||||
public void setNombre(String nombre) {
|
||||
this.nombre = nombre;
|
||||
}
|
||||
|
||||
public List<Venta> getVentas() {
|
||||
return this.ventas;
|
||||
}
|
||||
|
||||
public void setVentas(List<Venta> ventas) {
|
||||
this.ventas = ventas;
|
||||
}
|
||||
|
||||
public Venta addVenta(Venta venta) {
|
||||
getVentas().add(venta);
|
||||
venta.setCliente(this);
|
||||
|
||||
return venta;
|
||||
}
|
||||
|
||||
public Venta removeVenta(Venta venta) {
|
||||
getVentas().remove(venta);
|
||||
venta.setCliente(null);
|
||||
|
||||
return venta;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.nombre + " " + this.apellidos;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package tutorialJava.modelosBasesDeDatosComunesJPA.ventaDeCoches;
|
||||
|
||||
import java.io.Serializable;
|
||||
import javax.persistence.*;
|
||||
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.Entidad;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* The persistent class for the coche database table.
|
||||
*
|
||||
*/
|
||||
@Entity
|
||||
@NamedQuery(name="Coche.findAll", query="SELECT c FROM Coche c")
|
||||
public class Coche extends Entidad implements Serializable {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.AUTO)
|
||||
private int id;
|
||||
|
||||
private String bastidor;
|
||||
|
||||
private String color;
|
||||
|
||||
private String modelo;
|
||||
|
||||
//bi-directional many-to-one association to Fabricante
|
||||
@ManyToOne(fetch=FetchType.LAZY)
|
||||
@JoinColumn(name="idfabricante")
|
||||
private Fabricante fabricante;
|
||||
|
||||
//bi-directional many-to-one association to Venta
|
||||
@OneToMany(mappedBy="coche")
|
||||
private List<Venta> ventas;
|
||||
|
||||
public Coche() {
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getBastidor() {
|
||||
return this.bastidor;
|
||||
}
|
||||
|
||||
public void setBastidor(String bastidor) {
|
||||
this.bastidor = bastidor;
|
||||
}
|
||||
|
||||
public String getColor() {
|
||||
return this.color;
|
||||
}
|
||||
|
||||
public void setColor(String color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public String getModelo() {
|
||||
return this.modelo;
|
||||
}
|
||||
|
||||
public void setModelo(String modelo) {
|
||||
this.modelo = modelo;
|
||||
}
|
||||
|
||||
public Fabricante getFabricante() {
|
||||
return this.fabricante;
|
||||
}
|
||||
|
||||
public void setFabricante(Fabricante fabricante) {
|
||||
this.fabricante = fabricante;
|
||||
}
|
||||
|
||||
public List<Venta> getVentas() {
|
||||
return this.ventas;
|
||||
}
|
||||
|
||||
public void setVentas(List<Venta> ventas) {
|
||||
this.ventas = ventas;
|
||||
}
|
||||
|
||||
public Venta addVenta(Venta venta) {
|
||||
getVentas().add(venta);
|
||||
venta.setCoche(this);
|
||||
|
||||
return venta;
|
||||
}
|
||||
|
||||
public Venta removeVenta(Venta venta) {
|
||||
getVentas().remove(venta);
|
||||
venta.setCoche(null);
|
||||
|
||||
return venta;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package tutorialJava.modelosBasesDeDatosComunesJPA.ventaDeCoches;
|
||||
|
||||
import java.io.Serializable;
|
||||
import javax.persistence.*;
|
||||
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.Entidad;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* The persistent class for the concesionario database table.
|
||||
*
|
||||
*/
|
||||
@Entity
|
||||
@NamedQuery(name="Concesionario.findAll", query="SELECT c FROM Concesionario c")
|
||||
public class Concesionario extends Entidad implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.AUTO)
|
||||
private int id;
|
||||
|
||||
private String cif;
|
||||
|
||||
private String localidad;
|
||||
|
||||
private String nombre;
|
||||
|
||||
//bi-directional many-to-one association to Venta
|
||||
@OneToMany(mappedBy="concesionario")
|
||||
private List<Venta> ventas;
|
||||
|
||||
public Concesionario() {
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getCif() {
|
||||
return this.cif;
|
||||
}
|
||||
|
||||
public void setCif(String cif) {
|
||||
this.cif = cif;
|
||||
}
|
||||
|
||||
public String getLocalidad() {
|
||||
return this.localidad;
|
||||
}
|
||||
|
||||
public void setLocalidad(String localidad) {
|
||||
this.localidad = localidad;
|
||||
}
|
||||
|
||||
public String getNombre() {
|
||||
return this.nombre;
|
||||
}
|
||||
|
||||
public void setNombre(String nombre) {
|
||||
this.nombre = nombre;
|
||||
}
|
||||
|
||||
public List<Venta> getVentas() {
|
||||
return this.ventas;
|
||||
}
|
||||
|
||||
public void setVentas(List<Venta> ventas) {
|
||||
this.ventas = ventas;
|
||||
}
|
||||
|
||||
public Venta addVenta(Venta venta) {
|
||||
getVentas().add(venta);
|
||||
venta.setConcesionario(this);
|
||||
|
||||
return venta;
|
||||
}
|
||||
|
||||
public Venta removeVenta(Venta venta) {
|
||||
getVentas().remove(venta);
|
||||
venta.setConcesionario(null);
|
||||
|
||||
return venta;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package tutorialJava.modelosBasesDeDatosComunesJPA.ventaDeCoches;
|
||||
|
||||
import java.io.Serializable;
|
||||
import javax.persistence.*;
|
||||
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.Entidad;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* The persistent class for the fabricante database table.
|
||||
*
|
||||
*/
|
||||
@Entity
|
||||
@NamedQuery(name="Fabricante.findAll", query="SELECT f FROM Fabricante f")
|
||||
public class Fabricante extends Entidad implements Serializable {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.AUTO)
|
||||
private int id;
|
||||
|
||||
private String cif;
|
||||
|
||||
private String nombre;
|
||||
|
||||
//bi-directional many-to-one association to Coche
|
||||
@OneToMany(mappedBy="fabricante")
|
||||
private List<Coche> coches;
|
||||
|
||||
public Fabricante() {
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getCif() {
|
||||
return this.cif;
|
||||
}
|
||||
|
||||
public void setCif(String cif) {
|
||||
this.cif = cif;
|
||||
}
|
||||
|
||||
public String getNombre() {
|
||||
return this.nombre;
|
||||
}
|
||||
|
||||
public void setNombre(String nombre) {
|
||||
this.nombre = nombre;
|
||||
}
|
||||
|
||||
public List<Coche> getCoches() {
|
||||
return this.coches;
|
||||
}
|
||||
|
||||
public void setCoches(List<Coche> coches) {
|
||||
this.coches = coches;
|
||||
}
|
||||
|
||||
public Coche addCoch(Coche coch) {
|
||||
getCoches().add(coch);
|
||||
coch.setFabricante(this);
|
||||
|
||||
return coch;
|
||||
}
|
||||
|
||||
public Coche removeCoch(Coche coch) {
|
||||
getCoches().remove(coch);
|
||||
coch.setFabricante(null);
|
||||
|
||||
return coch;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return nombre;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
Fabricante f = (Fabricante) obj;
|
||||
if (this.id == f.id) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package tutorialJava.modelosBasesDeDatosComunesJPA.ventaDeCoches;
|
||||
|
||||
import java.io.Serializable;
|
||||
import javax.persistence.*;
|
||||
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.Entidad;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
|
||||
/**
|
||||
* The persistent class for the venta database table.
|
||||
*
|
||||
*/
|
||||
@Entity
|
||||
@NamedQuery(name="Venta.findAll", query="SELECT v FROM Venta v")
|
||||
public class Venta extends Entidad implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy=GenerationType.AUTO)
|
||||
private int id;
|
||||
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
private Date fecha;
|
||||
|
||||
private float precioVenta;
|
||||
|
||||
//bi-directional many-to-one association to Cliente
|
||||
@ManyToOne(fetch=FetchType.LAZY)
|
||||
@JoinColumn(name="idCliente")
|
||||
private Cliente cliente;
|
||||
|
||||
//bi-directional many-to-one association to Coche
|
||||
@ManyToOne(fetch=FetchType.LAZY)
|
||||
@JoinColumn(name="idCoche")
|
||||
private Coche coche;
|
||||
|
||||
//bi-directional many-to-one association to Concesionario
|
||||
@ManyToOne(fetch=FetchType.LAZY)
|
||||
@JoinColumn(name="idConcesionario")
|
||||
private Concesionario concesionario;
|
||||
|
||||
public Venta() {
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Date getFecha() {
|
||||
return this.fecha;
|
||||
}
|
||||
|
||||
public void setFecha(Date fecha) {
|
||||
this.fecha = fecha;
|
||||
}
|
||||
|
||||
public float getPrecioVenta() {
|
||||
return this.precioVenta;
|
||||
}
|
||||
|
||||
public void setPrecioVenta(float precioVenta) {
|
||||
this.precioVenta = precioVenta;
|
||||
}
|
||||
|
||||
public Cliente getCliente() {
|
||||
return this.cliente;
|
||||
}
|
||||
|
||||
public void setCliente(Cliente cliente) {
|
||||
this.cliente = cliente;
|
||||
}
|
||||
|
||||
public Coche getCoche() {
|
||||
return this.coche;
|
||||
}
|
||||
|
||||
public void setCoche(Coche coche) {
|
||||
this.coche = coche;
|
||||
}
|
||||
|
||||
public Concesionario getConcesionario() {
|
||||
return this.concesionario;
|
||||
}
|
||||
|
||||
public void setConcesionario(Concesionario concesionario) {
|
||||
this.concesionario = concesionario;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package tutorialJava.modelosBasesDeDatosComunesJPA.ventaDeCoches.controladores;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.NoResultException;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.Controlador;
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.ventaDeCoches.Cliente;
|
||||
|
||||
|
||||
public class ClienteControlador extends Controlador {
|
||||
|
||||
private static ClienteControlador controlador = null;
|
||||
|
||||
public ClienteControlador () {
|
||||
super(Cliente.class, "VentaDeCoches");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static ClienteControlador getControlador () {
|
||||
if (controlador == null) {
|
||||
controlador = new ClienteControlador();
|
||||
}
|
||||
return controlador;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public Cliente find (int id) {
|
||||
return (Cliente) super.find(id);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public List<Cliente> findAllClientes () {
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
Query q = em.createQuery("SELECT c FROM Cliente c", Cliente.class);
|
||||
List<Cliente> resultado = (List<Cliente>) q.getResultList();
|
||||
em.close();
|
||||
return resultado;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static String toString (Cliente cliente) {
|
||||
return cliente.getNombre() + " " + cliente.getApellidos();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
package tutorialJava.modelosBasesDeDatosComunesJPA.ventaDeCoches.controladores;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.NoResultException;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.Controlador;
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.ventaDeCoches.Coche;
|
||||
|
||||
public class CocheControlador extends Controlador {
|
||||
|
||||
private static CocheControlador controlador = null;
|
||||
|
||||
public CocheControlador () {
|
||||
super(Coche.class, "VentaDeCoches");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static CocheControlador getControlador () {
|
||||
if (controlador == null) {
|
||||
controlador = new CocheControlador();
|
||||
}
|
||||
return controlador;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public Coche find (int id) {
|
||||
return (Coche) super.find(id);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Coche findFirst () {
|
||||
try {
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
Query q = em.createQuery("SELECT c FROM Coche c order by c.id", Coche.class);
|
||||
Coche resultado = (Coche) q.setMaxResults(1).getSingleResult();
|
||||
em.close();
|
||||
return resultado;
|
||||
}
|
||||
catch (NoResultException nrEx) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Coche findLast () {
|
||||
try {
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
Query q = em.createQuery("SELECT c FROM Coche c order by c.id desc", Coche.class);
|
||||
Coche resultado = (Coche) q.setMaxResults(1).getSingleResult();
|
||||
em.close();
|
||||
return resultado;
|
||||
}
|
||||
catch (NoResultException nrEx) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Coche findNext (Coche c) {
|
||||
try {
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
Query q = em.createQuery("SELECT c FROM Coche c where c.id > :idActual order by c.id", Coche.class);
|
||||
q.setParameter("idActual", c.getId());
|
||||
Coche resultado = (Coche) q.setMaxResults(1).getSingleResult();
|
||||
em.close();
|
||||
return resultado;
|
||||
}
|
||||
catch (NoResultException nrEx) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Coche findPrevious (Coche c) {
|
||||
try {
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
Query q = em.createQuery("SELECT c FROM Coche c where c.id < :idActual order by c.id desc", Coche.class);
|
||||
q.setParameter("idActual", c.getId());
|
||||
Coche resultado = (Coche) q.setMaxResults(1).getSingleResult();
|
||||
em.close();
|
||||
return resultado;
|
||||
}
|
||||
catch (NoResultException nrEx) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param coche
|
||||
* @return
|
||||
*/
|
||||
public boolean exists(Coche coche) {
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
|
||||
boolean ok = true;
|
||||
try {
|
||||
Query q = em.createNativeQuery("SELECT * FROM tutorialjavacoches.coche where id = ?", Coche.class);
|
||||
q.setParameter(1, coche.getId());
|
||||
coche = (Coche) q.getSingleResult();
|
||||
}
|
||||
catch (NoResultException ex) {
|
||||
ok = false;
|
||||
}
|
||||
em.close();
|
||||
return ok;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public List<Coche> findAllCoches () {
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
Query q = em.createQuery("SELECT c FROM Coche c", Coche.class);
|
||||
List<Coche> resultado = (List<Coche>) q.getResultList();
|
||||
em.close();
|
||||
return resultado;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static String toString (Coche coche) {
|
||||
return coche.getFabricante() + " " + coche.getModelo() + " - " + coche.getBastidor();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
package tutorialJava.modelosBasesDeDatosComunesJPA.ventaDeCoches.controladores;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.NoResultException;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.Controlador;
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.ventaDeCoches.Concesionario;
|
||||
|
||||
|
||||
public class ConcesionarioControlador extends Controlador {
|
||||
|
||||
private static ConcesionarioControlador controlador = null;
|
||||
|
||||
public ConcesionarioControlador () {
|
||||
super(Concesionario.class, "VentaDeCoches");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static ConcesionarioControlador getControlador () {
|
||||
if (controlador == null) {
|
||||
controlador = new ConcesionarioControlador();
|
||||
}
|
||||
return controlador;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public Concesionario find (int id) {
|
||||
return (Concesionario) super.find(id);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Concesionario findFirst () {
|
||||
try {
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
Query q = em.createQuery("SELECT c FROM Concesionario c order by c.id", Concesionario.class);
|
||||
Concesionario resultado = (Concesionario) q.setMaxResults(1).getSingleResult();
|
||||
em.close();
|
||||
return resultado;
|
||||
}
|
||||
catch (NoResultException nrEx) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Concesionario findLast () {
|
||||
try {
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
Query q = em.createQuery("SELECT c FROM Concesionario c order by c.id desc", Concesionario.class);
|
||||
Concesionario resultado = (Concesionario) q.setMaxResults(1).getSingleResult();
|
||||
em.close();
|
||||
return resultado;
|
||||
}
|
||||
catch (NoResultException nrEx) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Concesionario findNext (Concesionario c) {
|
||||
try {
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
Query q = em.createQuery("SELECT c FROM Concesionario c where c.id > :idActual order by c.id", Concesionario.class);
|
||||
q.setParameter("idActual", c.getId());
|
||||
Concesionario resultado = (Concesionario) q.setMaxResults(1).getSingleResult();
|
||||
em.close();
|
||||
return resultado;
|
||||
}
|
||||
catch (NoResultException nrEx) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Concesionario findPrevious (Concesionario c) {
|
||||
try {
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
Query q = em.createQuery("SELECT c FROM Concesionario c where c.id < :idActual order by c.id desc", Concesionario.class);
|
||||
q.setParameter("idActual", c.getId());
|
||||
Concesionario resultado = (Concesionario) q.setMaxResults(1).getSingleResult();
|
||||
em.close();
|
||||
return resultado;
|
||||
}
|
||||
catch (NoResultException nrEx) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public List<Concesionario> findAllConcesionarios () {
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
Query q = em.createQuery("SELECT c FROM Concesionario c", Concesionario.class);
|
||||
List<Concesionario> resultado = (List<Concesionario>) q.getResultList();
|
||||
em.close();
|
||||
return resultado;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static String toString (Concesionario concesionario) {
|
||||
return "Id: " + concesionario.getId() + " - Nombre: " + concesionario.getNombre() +
|
||||
" - CIF: " + concesionario.getCif() + " - Localidad: " + concesionario.getLocalidad();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
package tutorialJava.modelosBasesDeDatosComunesJPA.ventaDeCoches.controladores;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.NoResultException;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.Controlador;
|
||||
import tutorialJava.modelosBasesDeDatosComunesJPA.ventaDeCoches.Fabricante;
|
||||
|
||||
public class FabricanteControlador extends Controlador {
|
||||
|
||||
private static FabricanteControlador controlador = null;
|
||||
|
||||
public FabricanteControlador () {
|
||||
super(Fabricante.class, "VentaDeCoches");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static FabricanteControlador getControlador () {
|
||||
if (controlador == null) {
|
||||
controlador = new FabricanteControlador();
|
||||
}
|
||||
return controlador;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public Fabricante find (int id) {
|
||||
return (Fabricante) super.find(id);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Fabricante findFirst () {
|
||||
try {
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
Query q = em.createQuery("SELECT c FROM Fabricante c order by c.id", Fabricante.class);
|
||||
Fabricante resultado = (Fabricante) q.setMaxResults(1).getSingleResult();
|
||||
em.close();
|
||||
return resultado;
|
||||
}
|
||||
catch (NoResultException nrEx) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Fabricante findLast () {
|
||||
try {
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
Query q = em.createQuery("SELECT c FROM Fabricante c order by c.id desc", Fabricante.class);
|
||||
Fabricante resultado = (Fabricante) q.setMaxResults(1).getSingleResult();
|
||||
em.close();
|
||||
return resultado;
|
||||
}
|
||||
catch (NoResultException nrEx) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Fabricante findNext (Fabricante c) {
|
||||
try {
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
Query q = em.createQuery("SELECT c FROM Fabricante c where c.id > :idActual order by c.id", Fabricante.class);
|
||||
q.setParameter("idActual", c.getId());
|
||||
Fabricante resultado = (Fabricante) q.setMaxResults(1).getSingleResult();
|
||||
em.close();
|
||||
return resultado;
|
||||
}
|
||||
catch (NoResultException nrEx) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Fabricante findPrevious (Fabricante c) {
|
||||
try {
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
Query q = em.createQuery("SELECT c FROM Fabricante c where c.id < :idActual order by c.id desc", Fabricante.class);
|
||||
q.setParameter("idActual", c.getId());
|
||||
Fabricante resultado = (Fabricante) q.setMaxResults(1).getSingleResult();
|
||||
em.close();
|
||||
return resultado;
|
||||
}
|
||||
catch (NoResultException nrEx) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param coche
|
||||
* @return
|
||||
*/
|
||||
public boolean exists(Fabricante fab) {
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
|
||||
boolean ok = true;
|
||||
try {
|
||||
Query q = em.createNativeQuery("SELECT * FROM tutorialjavacoches.coche where id = ?", Fabricante.class);
|
||||
q.setParameter(1, fab.getId());
|
||||
fab = (Fabricante) q.getSingleResult();
|
||||
}
|
||||
catch (NoResultException ex) {
|
||||
ok = false;
|
||||
}
|
||||
em.close();
|
||||
return ok;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public List<Fabricante> findAllFabricantes () {
|
||||
EntityManager em = getEntityManagerFactory().createEntityManager();
|
||||
Query q = em.createQuery("SELECT c FROM Fabricante c", Fabricante.class);
|
||||
List<Fabricante> resultado = (List<Fabricante>) q.getResultList();
|
||||
em.close();
|
||||
return resultado;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static String toString (Fabricante fabricante) {
|
||||
return fabricante.getNombre() + " " + fabricante.getCif();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user