Autoincrementable mediante Tabla
Last updated
Was this helpful?
Last updated
Was this helpful?
Referencia Libro
Se usa mediante la creaciĆ³n de una tabla que manejara los autoincrementable para cada tabla.
CREATE TABLE id_gen (
gen_name VARCHAR(80),
gen_val INTEGER,
CONSTRAINT pk_id_gen
PRIMARY KEY (gen_name)
);
INSERT INTO id_gen (gen_name, gen_val) VALUES ('PEOPLE_GEN', 0);
INSERT INTO id_gen (gen_name, gen_val) VALUES ('HISTORIAL_GEN', 0);
create table PEOPLE(
PEOPLEID NUMBER NOT NULL,
DESCRIPCION VARCHAR(100) ,
"FECHA" DATE,
CONSTRAINT PEOPLEPK PRIMARY KEY (PEOPLEID)
);
Definimos la entidad
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.peopleinmotion.horizonreinicioremotoejb.entity;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
/**
*
* @author avbravo
*/
@Entity
@Table(name = "PEOPLE")
@NamedQueries({
@NamedQuery(name = "PEOPLE.findAll", query = "SELECT p FROM People p"),
@NamedQuery(name = "PEOPLE.findByPEOPLEId", query = "SELECT p FROM People p WHERE p.PEOPLEID = :PEOPLEID"),
@NamedQuery(name = "PEOPLE.findByDescripcion", query = "SELECT p FROM People p WHERE p.DESCRIPCION = :DESCRIPCION")})
public class People implements Serializable {
private static final long serialVersionUID = 1L;
// @Max(value=?) @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
@TableGenerator(name = "PEOPLE_GEN",
table = "ID_GEN",
pkColumnName = "GEN_NAME",
valueColumnName = "GEN_VAL",
pkColumnValue = "PEOPLE_GEN",
initialValue = 10000,
allocationSize = 100)
@GeneratedValue(generator = "PEOPLE_GEN")
@Id
@Basic(optional = false)
@NotNull
@Column(name = "PEOPLEID")
private Integer PEOPLEID;
@Column(name = "DESCRIPCION")
private String DESCRIPCION;
@Column(name="FECHA")
@Temporal(TemporalType.TIMESTAMP)
private Date FECHA;
public People() {
}
public People(Integer PEOPLEID) {
this.PEOPLEID = PEOPLEID;
}
public Date getFECHA() {
return FECHA;
}
public void setFECHA(Date FECHA) {
this.FECHA = FECHA;
}
public Integer getPEOPLEID() {
return PEOPLEID;
}
public void setPEOPLEID(Integer PEOPLEID) {
this.PEOPLEID = PEOPLEID;
}
public String getDESCRIPCION() {
return DESCRIPCION;
}
public void setDESCRIPCION(String DESCRIPCION) {
this.DESCRIPCION = DESCRIPCION;
}
@Override
public int hashCode() {
int hash = 0;
hash += (PEOPLEID != null ? PEOPLEID.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// PEOPLEx: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof People)) {
return false;
}
People other = (People) object;
if ((this.PEOPLEID == null && other.PEOPLEID != null) || (this.PEOPLEID != null && !this.PEOPLEID.equals(other.PEOPLEID))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.peopleinmotion.mavenproject1.PEOPLE[ PEOPLEID=" + PEOPLEID + " ]";
}
}
Definir el repositorio
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.peopleinmotion.horizonreinicioremotoejb.repository;
import com.peopleinmotion.horizonreinicioremotoejb.entity.People;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
/**
*
* @author avbravo
*/
@Stateless
public class PeopleFacade extends AbstractFacade<People> {
@PersistenceContext(unitName = "com.people-inmotion_horizonreinicioremotoejb_ejb_1.0-SNAPSHOTPU")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
public PeopleFacade() {
super(People.class);
}
public List<People> findByDescripcion(String descripcion) {
Query query = em.createNamedQuery("PEOPLE.findByDescripcion");
return query.setParameter("descripcion",descripcion).getResultList();
}
}
En el controller
People people = new People();
// em.getTransaction().begin();
people.setDESCRIPCION(JsfUtil.generateUniqueID());
people.setFECHA(DateUtil.fechaHoraActual());
peopleFacade.create(people);