本文整理了Java中javax.persistence.EntityManager.merge()
方法的一些代码示例,展示了EntityManager.merge()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。EntityManager.merge()
方法的具体详情如下:
包路径:javax.persistence.EntityManager
类名称:EntityManager
方法名:merge
[英]Merge the state of the given entity into the current persistence context.
[中]将给定实体的状态合并到当前持久性上下文中。
代码示例来源:origin: stackoverflow.com
@Stateless
public class BookService {
@PersistenceContext
private EntityManager em;
public List<Book> list() {
return em.createQuery("FROM Book", Book.class).getResultList();
}
public Book find(Integer id) {
return em.find(Book.class, id);
}
public Integer save(Book book) {
em.persist(book);
return book.getId();
}
public void update(Book book) {
em.merge(book);
}
public void delete(Book book) {
em.remove(em.contains(book) ? book : em.merge(book));
}
}
代码示例来源:origin: kiegroup/optaplanner
protected <S extends Score, E extends AbstractTestJpaEntity<S>> void findAssertAndChangeScore(
Class<E> jpaEntityClass, Long id, S oldScore, S newScore) {
try {
transactionManager.begin();
EntityManager em = entityManagerFactory.createEntityManager();
E jpaEntity = em.find(jpaEntityClass, id);
em.persist(jpaEntity);
assertEquals(oldScore, jpaEntity.getScore());
jpaEntity.setScore(newScore);
jpaEntity = em.merge(jpaEntity);
transactionManager.commit();
} catch (NotSupportedException | SystemException | RollbackException | HeuristicRollbackException | HeuristicMixedException e) {
throw new RuntimeException("Transaction failed.", e);
}
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
@Priority(10)
public void initData() {
EntityManager em = getEntityManager();
// Revision 1
em.getTransaction().begin();
BasicTestEntity1 entity = new BasicTestEntity1( "str1", 1 );
em.persist( entity );
em.getTransaction().commit();
id = entity.getId();
// Revision 2 - both properties (str1 and long1) should be marked as modified.
em.getTransaction().begin();
entity = em.find( BasicTestEntity1.class, entity.getId() );
entity.setStr1( "str2" );
entity = em.merge( entity );
em.flush();
entity.setLong1( 2 );
entity = em.merge( entity );
em.flush();
em.getTransaction().commit();
em.close();
}
代码示例来源:origin: Impetus/Kundera
public void testMerge(boolean useSameEm)
{
EntityManager em = emf.createEntityManager();
StudentCouchDBChar student = em.find(StudentCouchDBChar.class, getMaxValue(char.class));
Assert.assertNotNull(student);
Assert.assertEquals(getMaxValue(short.class), student.getAge());
Assert.assertEquals(getMaxValue(String.class), student.getName());
student.setName("Kuldeep");
em.merge(student);
if (!useSameEm)
{
em.close();
em = emf.createEntityManager();
}
StudentCouchDBChar newStudent = em.find(StudentCouchDBChar.class, getMaxValue(char.class));
Assert.assertNotNull(newStudent);
Assert.assertEquals(getMaxValue(short.class), newStudent.getAge());
Assert.assertEquals("Kuldeep", newStudent.getName());
}
代码示例来源:origin: Impetus/Kundera
/**
* Test merge.
*/
private void testMerge()
{
Person p = em.find(Person.class, "101");
p.setPersonName("karthik");
em.merge(p);
em.clear();
Person p1 = em.find(Person.class, "101");
Assert.assertNotNull(p1);
Assert.assertEquals("karthik", p1.getPersonName());
}
代码示例来源:origin: Impetus/Kundera
/**
* Test update.
*/
private void testUpdate()
{
PersonMTM p1 = em.find(PersonMTM.class, "p1");
p1.setPersonName("Devender");
em.merge(p1);
em.clear();
PersonMTM p = em.find(PersonMTM.class, "p1");
Assert.assertEquals("Devender", p.getPersonName());
}
代码示例来源:origin: Impetus/Kundera
public void testMerge(boolean useSameEm)
{
EntityManager em = emf.createEntityManager();
StudentCouchDBDate student = em.find(StudentCouchDBDate.class, getMaxValue(Date.class));
Assert.assertNotNull(student);
Assert.assertEquals(getMaxValue(short.class), student.getAge());
Assert.assertEquals(getMaxValue(String.class), student.getName());
student.setName("Kuldeep");
em.merge(student);
if (!useSameEm)
{
em.close();
em = emf.createEntityManager();
}
StudentCouchDBDate newStudent = em.find(StudentCouchDBDate.class, getMaxValue(Date.class));
Assert.assertNotNull(newStudent);
Assert.assertEquals(getMaxValue(short.class), newStudent.getAge());
Assert.assertEquals("Kuldeep", newStudent.getName());
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
@Priority(10)
public void initData() {
EntityManager em = getEntityManager();
// Revision 1
em.getTransaction().begin();
StrTestEntity entity = new StrTestEntity( "x" );
em.persist( entity );
em.getTransaction().commit();
id = entity.getId();
// Revision 2
em.getTransaction().begin();
entity = em.find( StrTestEntity.class, entity.getId() );
entity.setStr( "y" );
entity = em.merge( entity );
em.getTransaction().commit();
em.close();
}
代码示例来源:origin: hibernate/hibernate-orm
private void saveUpdateAndRemoveEntity(EntityManager entityManager, Integer id) {
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
IntNoAutoIdTestEntity entity = new IntNoAutoIdTestEntity( 0, id );
entityManager.persist( entity );
assertEquals( id, entity.getId() );
transaction.commit();
transaction.begin();
entity = entityManager.find( IntNoAutoIdTestEntity.class, id );
entity.setNumVal( 1 );
entity = entityManager.merge( entity );
assertEquals( id, entity.getId() );
transaction.commit();
transaction.begin();
entity = entityManager.find( IntNoAutoIdTestEntity.class, id );
assertNotNull( entity );
entityManager.remove( entity );
transaction.commit();
}
}
代码示例来源:origin: Impetus/Kundera
/**
* Test merge.
*/
private void testMerge()
{
Person p = em.find(Person.class, "101");
p.setPersonName("karthik");
em.merge(p);
em.clear();
Person p1 = em.find(Person.class, "101");
Assert.assertNotNull(p1);
Assert.assertEquals("karthik", p1.getPersonName());
}
代码示例来源:origin: Impetus/Kundera
public void testMerge(boolean useSameEm)
{
EntityManager em = emf.createEntityManager();
StudentMongoTimestamp student = em.find(StudentMongoTimestamp.class, getMaxValue(Timestamp.class));
Assert.assertNotNull(student);
Assert.assertEquals(getMaxValue(short.class), student.getAge());
Assert.assertEquals(getMaxValue(String.class), student.getName());
student.setName("Kuldeep");
em.merge(student);
if (!useSameEm)
{
em.close();
em = emf.createEntityManager();
}
StudentMongoTimestamp newStudent = em.find(StudentMongoTimestamp.class, getMaxValue(Timestamp.class));
Assert.assertNotNull(newStudent);
Assert.assertEquals(getMaxValue(short.class), newStudent.getAge());
Assert.assertEquals("Kuldeep", newStudent.getName());
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
@Priority(10)
public void initData() {
EntityManager em = getEntityManager();
// Revision 1 - add
em.getTransaction().begin();
ObjectUserTypeEntity entity = new ObjectUserTypeEntity( "builtInType1", "stringUserType1" );
em.persist( entity );
em.getTransaction().commit();
id = entity.getId();
// Revision 2 - modify
em.getTransaction().begin();
entity = em.find( ObjectUserTypeEntity.class, entity.getId() );
entity.setUserType( 2 );
entity = em.merge( entity );
em.getTransaction().commit();
// Revision 3 - remove
em.getTransaction().begin();
entity = em.find( ObjectUserTypeEntity.class, entity.getId() );
em.remove( entity );
em.getTransaction().commit();
em.close();
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
@Priority(10)
public void initData() {
EntityManager em = getEntityManager();
// Revision 1 - Adding two entities
em.getTransaction().begin();
StrTestEntity ste1 = new StrTestEntity( "x" );
StrTestEntity ste2 = new StrTestEntity( "y" );
em.persist( ste1 );
em.persist( ste2 );
steId1 = ste1.getId();
steId2 = ste2.getId();
em.getTransaction().commit();
// Revision 2 - Adding first and removing second entity
em.getTransaction().begin();
ste1 = em.find( StrTestEntity.class, steId1 );
ste2 = em.find( StrTestEntity.class, steId2 );
ste1.setStr( "z" );
em.remove( ste2 );
em.getTransaction().commit();
// Revision 3 - Modifying and removing the same entity.
em.getTransaction().begin();
ste1 = em.find( StrTestEntity.class, steId1 );
ste1.setStr( "a" );
em.merge( ste1 );
em.remove( ste1 );
em.getTransaction().commit();
}
代码示例来源:origin: Impetus/Kundera
/**
* Test merge.
*/
private void testMerge()
{
Person p = em.find(Person.class, "101");
p.setPersonName("karthik");
em.merge(p);
em.clear();
Person p1 = em.find(Person.class, "101");
Assert.assertNotNull(p1);
Assert.assertEquals("karthik", p1.getPersonName());
}
代码示例来源:origin: Impetus/Kundera
public void testMerge(boolean useSameEm)
{
EntityManager em = emf.createEntityManager();
StudentBytePrimitive student = em.find(StudentBytePrimitive.class, getMaxValue(byte.class));
Assert.assertNotNull(student);
Assert.assertEquals(getMaxValue(short.class), student.getAge());
Assert.assertEquals(getMaxValue(String.class), student.getName());
student.setName("Kuldeep");
em.merge(student);
if (!useSameEm)
{
em.close();
em = emf.createEntityManager();
}
StudentBytePrimitive newStudent = em.find(StudentBytePrimitive.class, getMaxValue(byte.class));
Assert.assertNotNull(newStudent);
Assert.assertEquals(getMaxValue(short.class), newStudent.getAge());
Assert.assertEquals("Kuldeep", newStudent.getName());
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
@Priority(10)
public void initData() {
EntityManager em = getEntityManager();
// Revision 1
em.getTransaction().begin();
StrTestEntity emptyEntity = new StrTestEntity( "" );
em.persist( emptyEntity );
StrTestEntity nullEntity = new StrTestEntity( null );
em.persist( nullEntity );
em.getTransaction().commit();
emptyId = emptyEntity.getId();
nullId = nullEntity.getId();
em.close();
em = getEntityManager();
// Should not generate revision after NULL to "" modification and vice versa on Oracle.
em.getTransaction().begin();
emptyEntity = em.find( StrTestEntity.class, emptyId );
emptyEntity.setStr( null );
em.merge( emptyEntity );
nullEntity = em.find( StrTestEntity.class, nullId );
nullEntity.setStr( "" );
em.merge( nullEntity );
em.getTransaction().commit();
em.close();
}
代码示例来源:origin: hibernate/hibernate-orm
owning1 = em.find( ManyToOneOwning.class, owning1.getId() );
em.remove( owning1 );
em.getTransaction().commit();
owning2 = em.find( ManyToOneOwning.class, owning2.getId() );
owning2.setData( "data2modified" );
em.merge( owning2 );
em.getTransaction().commit();
代码示例来源:origin: Impetus/Kundera
public void testMerge(boolean useSameEm)
{
EntityManager em = emf.createEntityManager();
StudentCouchDBBigDecimal student = em.find(StudentCouchDBBigDecimal.class, getMaxValue(BigDecimal.class));
Assert.assertNotNull(student);
Assert.assertEquals(getMaxValue(short.class), student.getAge());
Assert.assertEquals(getMaxValue(String.class), student.getName());
student.setName("Kuldeep");
em.merge(student);
if (!useSameEm)
{
em.close();
em = emf.createEntityManager();
}
StudentCouchDBBigDecimal newStudent = em.find(StudentCouchDBBigDecimal.class, getMaxValue(BigDecimal.class));
Assert.assertNotNull(newStudent);
Assert.assertEquals(getMaxValue(short.class), newStudent.getAge());
Assert.assertEquals("Kuldeep", newStudent.getName());
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
@Priority(10)
public void initData() {
EntityManager entityManager = getOrCreateEntityManager();
try {
// Revision 1
SimpleEntity se = new SimpleEntity();
se.setData( "data" );
entityManager.getTransaction().begin();
entityManager.persist( se );
entityManager.getTransaction().commit();
entityManager.clear();
entityId = se.getId();
// Revision 2
entityManager.getTransaction().begin();
se = entityManager.find( SimpleEntity.class, se.getId() );
se.setData( "data2" );
entityManager.merge( se );
entityManager.getTransaction().commit();
// Revision 3
entityManager.getTransaction().begin();
se = entityManager.find( SimpleEntity.class, se.getId() );
entityManager.remove( se );
entityManager.getTransaction().commit();
}
finally {
entityManager.close();
}
}
代码示例来源:origin: hibernate/hibernate-orm
final Simple simple = entityManager.find( Simple.class, 1 );
simple.getEmbeddedMap().put( "3", "Three" );
entityManager.merge( simple );
} );
final Simple simple = entityManager.find( Simple.class, 1 );
simple.getEmbeddedMap().remove( "1" );
simple.getEmbeddedMap().remove( "2" );
entityManager.merge( simple );
} );
final Simple simple = entityManager.find( Simple.class, 1 );
simple.getEmbeddedMap().remove( "3" );
simple.getEmbeddedMap().put( "3", "Three-New" );
entityManager.merge( simple );
} );
final Simple simple = entityManager.find( Simple.class, 1 );
simple.getEmbeddedMap().clear();
entityManager.merge( simple );
} );
内容来源于网络,如有侵权,请联系作者删除!