Java如何在JPA中通过ID查找实体?
在此示例中,您将学习如何使用JPA通过实体ID查找实体对象。要通过ID查找实体,我们使用EntityManager.find()方法,并将实体ID作为参数传递给实体类。
在实例化DAO时,将从主程序传递EntityManager所需的以下代码段ArtistDaoImpl。查找Artist实体的过程findById()在DAO类的方法中定义。您必须将实体的ID传递给此方法。
该findById()方法调用该EntityManager.find()方法以查找实体。如果没有找到实体,其中artist==null一个javax.persistence.EntityNotFoundException将被抛出。
package org.nhooo.example.jpa.dao.impl; import org.nhooo.example.jpa.dao.ArtistDao; import org.nhooo.example.jpa.entity.Artist; import javax.persistence.EntityManager; import javax.persistence.EntityNotFoundException; import javax.persistence.Query; import java.util.List; public class ArtistDaoImpl implements ArtistDao { private EntityManager manager; public ArtistDaoImpl(EntityManager manager) { this.manager = manager; } /** * Find Artist based on the entity Id. * * @param artistId the artist Id. * @return Artist. * @throws EntityNotFoundException when no artist is found. */ public Artist findById(Long artistId) { Artist artist = manager.find(Artist.class, artistId); if (artist == null) { throw new EntityNotFoundException("Can't find Artist for ID " + artistId); } return artist; } @Override public void save(Artist artist) { manager.getTransaction().begin(); manager.persist(artist); manager.getTransaction().commit(); } @Override @SuppressWarnings(value = "unchecked") public List<Artist> getArtists() { Query query = manager.createQuery("select a from Artist a", Artist.class); return query.getResultList(); } }
要运行DAO类,我们在下面的代码片段中创建一个主程序。步骤EntityManagerFactory由您的persistence.xml文件创建配置。EntityManager使用工厂对象创建。创建DAO并将其传递EntityManager给它。最后调用findById()DAO类的方法。
package org.nhooo.example.jpa; import org.nhooo.example.jpa.dao.ArtistDao; import org.nhooo.example.jpa.dao.impl.ArtistDaoImpl; import org.nhooo.example.jpa.entity.Artist; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityNotFoundException; import javax.persistence.Persistence; public class FindEntityByIdDemo { public static final String PERSISTENCE_UNIT_NAME = "music"; public static void main(String[] args) { EntityManagerFactory factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); EntityManager em = factory.createEntityManager(); ArtistDao dao = new ArtistDaoImpl(em); //从数据库中找到ID=1的艺术家。该实体是 //存在于数据库中。 Artist artist = dao.findById(1L); System.out.println("Artist = " + artist); try { //查找数据库中不存在的实体将 //引发异常。 artist = dao.findById(100L); System.out.println("Artist = " + artist); } catch (EntityNotFoundException e) { System.out.println(e.getMessage()); } } }
Maven依赖
<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.4.1.Final/hibernate-core-5.4.1.Final.jar --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.4.1.Final</version> </dependency> <!-- https://search.maven.org/remotecontent?filepath=mysql/mysql-connector-java/5.1.47/mysql-connector-java-5.1.47.jar --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> </dependencies>