# Autoincrementable mediante Tabla

Referencia Libro

![](/files/-MlIIHljbqB1eRfJpE17)

Se usa mediante la creación de una tabla que manejara los autoincrementable para cada tabla.

```sql

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

```java
/*
 * 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

```java
/*
 * 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

```java
   People people = new People();   
//            em.getTransaction().begin();

           people.setDESCRIPCION(JsfUtil.generateUniqueID());
            people.setFECHA(DateUtil.fechaHoraActual());
           
            peopleFacade.create(people);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://avbravo-2.gitbook.io/developmentcookbook/jpa/autoincrementable-mediante-tabla.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
