I'm assuming here you are using Datastore identity, ie the entity is
annotated with @PersistenceCapable(identityType=IdentityType.DATASTORE).
In which case, as Stephen says, you can either use the DN PM obtained from
IsisJdoSupport service [1]:
isisJdoSupport.getJdoPersistenceManager().getObjectId(someDomainObject)
or just:
JdoHelper.getObjectId(someDomainObject).
This will return DataNucleus' internal representation of the identity.
There is a caveat though: formally speaking, the class is
JDO-implementation dependent, and not part of the API [2]. In practice DN
hasn't changed this implementation since DN v1.0, so it is very stable, and
(internally) Apache Isis does have a dependency on this DN implementation.
So, you can extract the id by parsing the toString() of the object; it'll
be something like "123[OID]com.mycompany.SomeDomainObject"
~~~
Alternatively, in Isis 1.13.0 you can use the Persistable_datanucleusIdLong
mixin [3], eg
new Persistable_datanucleusIdLong(someDomainObject).$$()
~~~
If you do things "properly" (at least so far as DN goes), then you can
either use application-defined identity
(@PersistenceCapable(identityType=IdentityType.APPLICATION) [4] or you can
define your own implementation for DN datastore identity using a DN plug-in
point [5]
~~~
Finally, note that you should use repositoryService#persistAndFlush()
rather than repositoryService#persist() [6]. The former will perform the
creation immediately (so you can obtain the Id) whereas the latter will
only queue up the creation of the command until the action completes (the
HTH
Dan
[1] http://isis.apache.org/guides/rgsvc.html#_rgsvc_api_IsisJdoSupport
[2]
http://www.datanucleus.org/products/datanucleus/jdo/datastore_identity.html
[3]
http://isis.apache.org/guides/rgcms.html#_rgcms_classes_mixins_Persistable
[4]
http://www.datanucleus.org/products/datanucleus/jdo/application_identity.html
[5]
http://www.datanucleus.org/products/datanucleus/extensions/datastoreidentity.html
[6]
http://isis.apache.org/guides/rgsvc.html#__code_persistandflush_code_code_removeandflush_code
On 12 August 2016 at 10:21, Stephen Cameron <[email protected]>
wrote:
> PersistenceManager has a method getObjectId(Object), but maybe that class
> is redundant in 1.13?
>
> On Fri, Aug 12, 2016 at 7:31 AM, José Alejandro Manaure Martinez <
> [email protected]> wrote:
>
> > Hi Dan
> > How I can get the ID of a record right after creating it?
> >
> > Here you are the code used:
> >
> > public Solicitud CrearSolicitud(
> > final @ParameterLayout(named="Correo Contacto") String correoContacto,
> > final @ParameterLayout(named="Descripcion") String descripcion,
> > final @ParameterLayout(named="Fecha Creacion") LocalDateTime
> > fechaCreacion,
> > final @ParameterLayout(named="Fecha Inicio Creacion") LocalDateTime
> > fechaInicioCreacion,
> > final @ParameterLayout(named="Telefono Contacto") String
> > telefonoContacto,
> > final
> > @Parameter(optionality=Optionality.OPTIONAL)@ParameterLayout(named="
> > Extencion
> > Contacto") String extension,
> > final
> > @Parameter(optionality=Optionality.OPTIONAL)@
> ParameterLayout(named="Nombre
> > Solicitante") String nombreSolicitante,
> > final
> > @Parameter(optionality=Optionality.OPTIONAL)@
> ParameterLayout(named="Numero
> > Documento Solicitante") String numDocSolicitante,
> > final @ParameterLayout(named="Nombre Beneficiario") String
> > nombreBeneficiario,
> > final @ParameterLayout(named="Numero Documento Beneficiario") String
> > numDocBeneficiario,
> > final @ParameterLayout(named="Tipo Persona") TipoPersona
> > tipoPerBeneficiario,
> > final @ParameterLayout(named="Sucursal") Sucursal sucursal,
> > final @ParameterLayout(named="Compania") Compania compania,
> > final @ParameterLayout(named="Tipo Documento Beneficiario")
> > TiposDocumento tiposDocBenef,
> > final
> > @Parameter(optionality=Optionality.OPTIONAL)@ParameterLayout(named="Tipo
> > Documento Solicitante") TiposDocumento tiposDocSolicitante,
> > final @ParameterLayout(named="Motivo tramite") MotivoTramite
> > motivoTramite,
> > final @ParameterLayout(named="Nivel De Escalamiento")
> NivelEscalamiento
> > nivelEscalamiento,
> > final @ParameterLayout(named="Canal") Canal canal,
> > final @ParameterLayout(named="Estado") Estado estadoSolicitud,
> > final
> > @Parameter(optionality=Optionality.OPTIONAL)@
> ParameterLayout(named="Razon
> > De Prioridad") RazonPrioridad razonPrioridad,
> > final @ParameterLayout(named="Usuario Radicador") String usuario,
> > final
> > @Parameter(optionality=Optionality.OPTIONAL)@
> ParameterLayout(named="Estado
> > Ans") String estadoAns,
> > final
> > @Parameter(optionality=Optionality.OPTIONAL)@
> > ParameterLayout(named="Usuario
> > Asignado") String usuarioAsignado
> > ) throws UnsupportedEncodingException, IOException{
> >
> > Solicitud sol = container.newTransientInstance(Solicitud.class);
> > sol.setCorreoContacto(correoContacto);
> > sol.setDescripcion(descripcion.trim());
> > sol.setFechaCreacion(fechaCreacion);
> > sol.setFechaInicioCreacion(fechaInicioCreacion);
> > sol.setTelefonoContacto(telefonoContacto);
> > sol.setExtension(extension);
> > sol.setNombreSolicitante(nombreSolicitante);
> > sol.setNumDocSolicitante(numDocSolicitante);
> > sol.setNombreBeneficiario(nombreBeneficiario);
> > sol.setNumDocBeneficiario(numDocBeneficiario);
> > sol.setTipoPersona(tipoPerBeneficiario);
> > sol.setSucursal(sucursal);
> > sol.setCompania(compania);
> > sol.setTipoDocumentoBeneficiario(tiposDocBenef);
> > sol.setTipoDocumentoSolicitante(tiposDocSolicitante);
> > sol.setMotivoTramite(motivoTramite);
> > sol.setNivelEscalamiento(nivelEscalamiento);
> > sol.setCanal(canal);
> > sol.setEstado(estadoSolicitud);
> > sol.setRazonPrioridad(razonPrioridad);
> > sol.setUsuario(usuario);
> > sol.setUsuarioAsignado(usuarioAsignado);
> > sol.setEstadoAns(estadoAns);
> >
> > container.persist(sol);
> >
> > FechaCierreSolicitud fecCierre= new FechaCierreSolicitud() ;
> >
> >
> > int idSol=sol.getIdSolicitud();
> >
> > //
> >
> > // String fechaCierre2 =
> > fecCierre.calcularFechaCierreSolicitud(motivoTramite.getAns());
> > // LocalDateTime format = LocalDateTime.parse(fechaCierre2);
> > //
> > LocalDateTime fechaCierre=
> > fecCierre.calcularFechaCierreSolicitud(8,18,motivoTramite.getAns());
> > LocalDateTime fechaRadica= sol.getFechaCreacion();
> > sol.setFechaCierre(fechaCierre);
> > // sol.setAdjuntos(adjuntos);
> > /*load email body template*/
> >
> > ReceiveRestEasyMail rrem = new ReceiveRestEasyMail();
> >
> > rrem.enviaMail(PropertiesUtil.getProperty("
> servicioNotificacionElectronic
> > aAsunto"),
> > correoContacto, descripcion, "");
> > return sol;
> >
> > }
> >
> > Best regards...
> > --
> > [image: Logo]
> >
> > José Alejandro Manaure
> > *Project Manager*
> > Tel: (57) 1 703 17 77
> > Cel: (57) 312 5476188
> > E-mail: [email protected]
> > Calle 93 # 19b - 66 Ofc 202
> > Bogotá D.C., Colombia
> > www.ticxar.com
> > [image: facebook]
> > <https://www.facebook.com/pages/Ticxar/446503822192581> [image:
> > twitter] <http://twitter.com/ticxar> [image: linkedIn]
> > <https://www.linkedin.com/company/ticxar>
> >
>