DEV Community

Edgar Rios Navarro
Edgar Rios Navarro

Posted on

Mapping with SCHEMA

Una mala práctica en la configuración de aplicaciones, es usar el owner del esquema para acceder a la base de datos.
Es recomendable crear una cuenta de usuario y otorgar los permisos necesarios y específicos.

Pongamos como ejemplo:

  • ESQUEMA: REGIONALIZACION
  • USUARIO: MSPRODUCTOSWOW
  • PERMISOS:
-- Oracle
GRANT SELECT, INSERT, UPDATE 
ON REGIONALIZACION.LGT_PROD_LOCAL_OFERTA_AUX 
TO MSPRODUCTOSWOW
Enter fullscreen mode Exit fullscreen mode

Empleando las anotaciones de Jakarta Persistence, definimos el mapping de nuestra Entity: name/schema.

import jakarta.persistence.*;

@Table(name = "LGT_PROD_LOCAL_OFERTA_AUX", schema = "REGIONALIZACION")
public record WOWProduct(@EmbeddedId WOWProductId id,
                         float porcDctoOferta,
                         LocalDateTime fecIniVigOferta,
                         LocalDateTime fecFinVigOferta,
                         String usuCreaProdLocOfe,
                         LocalDateTime fecCreaProdLocOfe) {
}
Enter fullscreen mode Exit fullscreen mode

Nótese que podemos usar record, incluso para definir una llave compuesta.

@Embeddable
record WOWProductId(
        @Column(name = "COD_GRUPO_CIA") String codGrupoCia,
        @Column(name = "COD_LOCAL") String codLocal,
        @Column(name = "COD_OFERTA") String codOferta,
        @Column(name = "COD_PROD") String codProd) {
}
Enter fullscreen mode Exit fullscreen mode

Ejecutamos nuestro proyecto basado en #Micronaut:
Image description

Y al listar los productos:
Image description


Documentación

https://micronaut-projects.github.io/micronaut-data/latest/guide/#sqlAnnotations
https://github.com/edgargs/mn-jdbc-schema

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more