If you want to get the primary key of any JPA entity object you can use PersistenceUnitUtil.getIdentifier()
method. This method take a single parameter which is the entity object whose identifier to be read. The PersistenceUnitUtil
instance can be accessed from the EntityManagerFactory
object.
If the entity object contains an identifier the getIdentifier()
method will return the identifier as a java.lang.Object
. If the entity object does not have an identifier ye it will return null
.
package org.kodejava.jpa;
import org.kodejava.jpa.entity.Artist;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class GetEntityIdDemo {
public static final String PERSISTENCE_UNIT_NAME = "music";
public static void main(String[] args) {
EntityManagerFactory factory =
Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager manager = factory.createEntityManager();
// Get object identifier of an exists entity.
Artist artist = manager.find(Artist.class, 1L);
if (artist != null) {
Object identifier =
factory.getPersistenceUnitUtil().getIdentifier(artist);
System.out.println("Identifier = " + identifier);
}
// Get object identifier of a newly inserted entity.
Artist newArtist = new Artist();
newArtist.setName("Bon Jovi");
manager.getTransaction().begin();
manager.persist(newArtist);
manager.getTransaction().commit();
Object identifier =
factory.getPersistenceUnitUtil().getIdentifier(newArtist);
System.out.println("Identifier = " + identifier);
}
}
Maven Dependencies
<dependencies>
<!-- https://search.maven.org/remotecontent?filepath=javax/persistence/javax.persistence-api/2.2/javax.persistence-api-2.2.jar -->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<!-- https://search.maven.org/remotecontent?filepath=org/hibernate/hibernate-core/5.6.9.Final/hibernate-core-5.6.9.Final.jar -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.9.Final</version>
</dependency>
<!-- https://search.maven.org/remotecontent?filepath=mysql/mysql-connector-java/8.0.31/mysql-connector-java-8.0.31.jar -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
</dependencies>
Latest posts by Wayan (see all)
- How do I get the number of processors available to the JVM? - March 29, 2023
- How do I show Spring transaction in log / console? - March 29, 2023
- How do I build simple search page using ZK and Spring Boot? - March 8, 2023