# Dos campos como campo llave

En algunas ocasiones necesitamos validar en base a dos campos secundarios para restringir la creacion y se cuenta con un campo autoincrementable.

![](/files/-Lc1X8hHw5oEB5Q91Tbo)

## ![](/files/-Lc1X8hJo1_zeAlw7paX)

## En el list

![](/files/-Lc1X8hLj7Jua9BDZWj5)

## En el método isNew()

Solo se valida si esta vacio.

```java
 @Override
    public String isNew() {
        try {
            writable = true;
            if (JsfUtil.isVacio(modelo.getDescripcion())) {
                writable = false;
                return "";
            }
            String idsecond = modelo.getDescripcion();
            modelo = new Modelo();
            modelo.setDescripcion(idsecond);
            modeloSelected = new Modelo();

        } catch (Exception e) {
            JsfUtil.errorMessage("isNew()" + e.getLocalizedMessage());
        }
        return "";
    }// </editor-fold>
```

## En el mètodo save()

Verificar por los dos atributos secundarios.

```java
@Override
    public String save() {
        try {

            Document doc = new Document("descripcion", modelo.getDescripcion()).append("grupoarticulo.idgrupoarticulo", modelo.getGrupoarticulo().getIdgrupoarticulo());
            List<Modelo> list = modeloRepository.findBy(doc);

            if (!list.isEmpty()) {
                writable = false;

                JsfUtil.warningMessage(rf.getAppMessage("warning.idexist"));
                return "";
            }

            Integer identity = autoincrementableStoreejbServices.getContador("modelo");
            modelo.setIdmodelo(identity);

            //Lo datos del usuario
            modelo.setUserInfo(userInfoServices.generateListUserinfo(loginController.getUsername(), "create"));
            if (modeloRepository.save(modelo)) {
                revisionHistoryStoreejbRepository.save(revisionHistoryServices.getRevisionHistory(modelo.getIdmodelo().toString(), loginController.getUsername(),
                        "create", "modelo", modeloRepository.toDocument(modelo).toString()));

                JsfUtil.successMessage(rf.getAppMessage("info.save"));
                reset();
            } else {
                JsfUtil.successMessage("save() " + modeloRepository.getException().toString());
            }

        } catch (Exception e) {
            JsfUtil.errorMessage("save()" + e.getLocalizedMessage());
        }
        return "";
    }// </editor-fold>
```

## Nota:

ver ModeloController.java


---

# 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/trucosjakartaee/overview/formularios/dos-campos-como-campo-llave.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.
