本文整理了Java中javax.persistence.EntityManager
类的一些代码示例,展示了EntityManager
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。EntityManager
类的具体详情如下:
包路径:javax.persistence.EntityManager
类名称:EntityManager
[英]Interface used to interact with the persistence context.
An EntityManager
instance is associated with a persistence context. A persistence context is a set of entity instances in which for any persistent entity identity there is a unique entity instance. Within the persistence context, the entity instances and their lifecycle are managed. The EntityManager
API is used to create and remove persistent entity instances, to find entities by their primary key, and to query over entities.
The set of entities that can be managed by a given EntityManager
instance is defined by a persistence unit. A persistence unit defines the set of all classes that are related or grouped by the application, and which must be colocated in their mapping to a single database.
[中]用于与持久性上下文交互的接口。EntityManager
实例与持久性上下文关联。持久性上下文是一组实体实例,其中对于任何持久性实体标识,都有一个唯一的实体实例。在持久性上下文中,管理实体实例及其生命周期。EntityManager
API用于创建和删除持久实体实例,通过主键查找实体,以及查询实体。
可由给定EntityManager
实例管理的实体集由持久性单元定义。持久性单元定义了应用程序相关或分组的所有类的集合,这些类在映射到单个数据库时必须进行共定位。
代码示例来源:origin: hibernate/hibernate-orm
@Test
@Priority(10)
public void initData() {
// Revision 1
EntityManager em = getEntityManager();
em.getTransaction().begin();
StrTestEntity fe = new StrTestEntity( "x" );
em.persist( fe );
em.flush();
em.remove( em.find( StrTestEntity.class, fe.getId() ) );
em.flush();
em.getTransaction().commit();
//
id = fe.getId();
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void test_criteria_typedquery_expression_example() {
doInJPA( this::entityManagerFactory, entityManager -> {
//tag::criteria-typedquery-expression-example[]
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<String> criteria = builder.createQuery( String.class );
Root<Person> root = criteria.from( Person.class );
criteria.select( root.get( Person_.nickName ) );
criteria.where( builder.equal( root.get( Person_.name ), "John Doe" ) );
List<String> nickNames = entityManager.createQuery( criteria ).getResultList();
//end::criteria-typedquery-expression-example[]
assertEquals(1, nickNames.size());
});
}
代码示例来源:origin: spring-projects/spring-data-examples
@Override
@Transactional
public Customer save(Customer customer) {
// Is new?
if (customer.getId() == null) {
em.persist(customer);
return customer;
} else {
return em.merge(customer);
}
}
代码示例来源:origin: stackoverflow.com
@Stateless
public class CommentService {
@PersistenceContext(unitName = "pu")
private EntityManager em;
public void create(Comment t) {
em.merge(t);
}
public Collection<Comment> getAll() {
Query q = em.createNamedQuery("Comment.findAll");
Collection<Comment> entities = q.getResultList();
return entities;
}
}
代码示例来源:origin: spring-projects/spring-data-examples
public List<User> myCustomBatchOperation() {
CriteriaQuery<User> criteriaQuery = em.getCriteriaBuilder().createQuery(User.class);
criteriaQuery.select(criteriaQuery.from(User.class));
return em.createQuery(criteriaQuery).getResultList();
}
}
代码示例来源:origin: spring-projects/spring-integration-samples
@Transactional
public void remove() {
if (this.entityManager == null) {
this.entityManager = entityManager();
}
if (this.entityManager.contains(this)) {
this.entityManager.remove(this);
} else {
LoanShark attached = this.entityManager.find(this.getClass(), this.id);
this.entityManager.remove(attached);
}
}
代码示例来源:origin: Impetus/Kundera
private void findByNameAndAgeGTAndLT()
{
EntityManager em;
String query;
Query q;
List<StudentBooleanPrimitive> students;
em = emf.createEntityManager();
query = "Select s From StudentBooleanPrimitive s where s.name = Amresh and s.age > " + getMinValue(short.class)
+ " and s.age < " + getMaxValue(short.class);
q = em.createQuery(query);
students = q.getResultList();
Assert.assertNotNull(students);
Assert.assertTrue(students.isEmpty());
em.close();
}
代码示例来源:origin: Impetus/Kundera
/**
* Test avg aggregation.
*/
private void testAvgAggregation()
{
String queryString = "Select avg(i.quantity) from Item i";
Query query = em.createQuery(queryString);
List resultList = query.getResultList();
Assert.assertEquals(1, resultList.size());
Assert.assertEquals(25.0, resultList.get(0));
}
代码示例来源:origin: hibernate/hibernate-orm
private List<RootEntity> getAllRows(EntityManager em) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<RootEntity> cq = cb.createQuery( RootEntity.class );
Root<RootEntity> c = cq.from( RootEntity.class );
return em.createQuery( cq.select( c ).orderBy( cb.desc( c.get( "status" ) ) ) ).getResultList();
}
代码示例来源:origin: Impetus/Kundera
public void testFindById(boolean useSameEm)
{
EntityManager em = emf.createEntityManager();
StudentMongoCalendar studentMax = em.find(StudentMongoCalendar.class, getMaxValue(Calendar.class));
Assert.assertNotNull(studentMax);
Assert.assertEquals(getMaxValue(short.class), studentMax.getAge());
Assert.assertEquals(getMaxValue(String.class), studentMax.getName());
em.close();
}
代码示例来源:origin: spring-projects/spring-framework
@Test
@SuppressWarnings("unchecked")
public void testEntityManagerProxyIsProxy() {
assertTrue(Proxy.isProxyClass(sharedEntityManager.getClass()));
Query q = sharedEntityManager.createQuery("select p from Person as p");
q.getResultList();
assertTrue("Should be open to start with", sharedEntityManager.isOpen());
sharedEntityManager.close();
assertTrue("Close should have been silently ignored", sharedEntityManager.isOpen());
}
代码示例来源:origin: spring-projects/spring-data-examples
@Override
public void removedExpiredAccounts(LocalDate reference) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Account> query = cb.createQuery(Account.class);
Root<Account> account = query.from(Account.class);
query.where(cb.lessThan(account.get("expiryDate").as(Date.class), reference.toDateTimeAtStartOfDay().toDate()));
for (Account each : em.createQuery(query).getResultList()) {
em.remove(each);
}
}
}
代码示例来源:origin: kiegroup/jbpm
@Test
public void testCreateCorrelation() throws Exception {
EntityManagerFactory emf = (EntityManagerFactory) context.get(EnvironmentName.ENTITY_MANAGER_FACTORY);
EntityManager em = emf.createEntityManager();
Query query = em.createNamedQuery("GetProcessInstanceIdByCorrelation");
query.setParameter("ckey", "test123");
List<Long> processInstances = query.getResultList();
em.close();
assertNotNull(processInstances);
assertEquals(1, processInstances.size());
}
代码示例来源:origin: Impetus/Kundera
public void testEntityListener()
{
EntityManagerFactory emf = getEntityManagerFactory();
EntityManager em = emf.createEntityManager();
PersonnelDTO dto = new PersonnelDTO();
dto.setFirstName("Vivek");
dto.setLastName("vivek");
dto.setPersonId("1_p");
em.persist(dto);
PersonnelDTO result = em.find(PersonnelDTO.class, "1_p");
Assert.assertNotNull(result);
Assert.assertEquals("Mishra", result.getLastName());
emf.close();
}
代码示例来源:origin: hibernate/hibernate-orm
@TestForIssue(jiraKey = "HHH-9296")
@Test
public void selectByParent() {
doInJPA( this::entityManagerFactory, entityManager -> {
Post post = entityManager.find( Post.class, 1 );
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<PostDetails> query = cb.createQuery( PostDetails.class );
Root<PostDetails> root = query.from( PostDetails.class );
query.where( cb.equal( root.get( "post" ), post ) );
final PostDetails result = entityManager.createQuery( query ).getSingleResult();
assertNotNull( result );
});
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
public void test_criteria_param_example() {
doInJPA( this::entityManagerFactory, entityManager -> {
//tag::criteria-param-example[]
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Person> criteria = builder.createQuery( Person.class );
Root<Person> root = criteria.from( Person.class );
ParameterExpression<String> nickNameParameter = builder.parameter( String.class );
criteria.where( builder.equal( root.get( Person_.nickName ), nickNameParameter ) );
TypedQuery<Person> query = entityManager.createQuery( criteria );
query.setParameter( nickNameParameter, "JD" );
List<Person> persons = query.getResultList();
//end::criteria-param-example[]
assertEquals(1, persons.size());
});
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
@TestForIssue(jiraKey = "HHH-12786")
public void testBasicProxyingWithProtectedMethodCalledInConstructor() {
doInJPA( this::entityManagerFactory, entityManager -> {
Adult adult = new Adult();
adult.setName( "Arjun Kumar" );
entityManager.persist( adult );
} );
doInJPA( this::entityManagerFactory, entityManager -> {
List<Adult> adultsCalledArjun = entityManager
.createQuery( "SELECT a from Adult a WHERE a.name = :name", Adult.class )
.setParameter( "name", "Arjun Kumar" ).getResultList();
Adult adult = adultsCalledArjun.iterator().next();
entityManager.remove( adult );
} );
}
代码示例来源:origin: spring-projects/spring-framework
@Test
@SuppressWarnings("unchecked")
public void testQueryNoPersons() {
EntityManager em = entityManagerFactory.createEntityManager();
Query q = em.createQuery("select p from Person as p");
List<Person> people = q.getResultList();
assertEquals(0, people.size());
try {
assertNull(q.getSingleResult());
fail("Should have thrown NoResultException");
}
catch (NoResultException ex) {
// expected
}
}
代码示例来源:origin: Impetus/Kundera
private void deleteNamedQueryOnCounter()
{
EntityManager em = emf.createEntityManager();
String deleteQuery = "Delete From Counters c where c.id <= " + id2;
Query q = em.createQuery(deleteQuery);
q.executeUpdate();
Counters counter2 = new Counters();
counter2 = em.find(Counters.class, id1);
Assert.assertNull(counter2);
Counters counter3 = new Counters();
counter3 = em.find(Counters.class, id2);
Assert.assertNull(counter3);
em.close();
}
}
代码示例来源:origin: Impetus/Kundera
public void deleteCounter()
{
EntityManager em = emf.createEntityManager();
Counters counters = new Counters();
counters = em.find(Counters.class, id3);
Assert.assertNotNull(counters);
Assert.assertNotNull(counters.getCounter());
em.remove(counters);
EntityManager em1 = emf.createEntityManager();
counters = em1.find(Counters.class, id3);
Assert.assertNull(counters);
em.close();
}
内容来源于网络,如有侵权,请联系作者删除!