本文整理了Java中javax.persistence.EntityManager.persist()
方法的一些代码示例,展示了EntityManager.persist()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。EntityManager.persist()
方法的具体详情如下:
包路径:javax.persistence.EntityManager
类名称:EntityManager
方法名:persist
[英]Make an instance managed and persistent.
[中]使实例得到管理和持久化。
代码示例来源:origin: stackoverflow.com
EntityManager em = ...;
EntityTransaction tx = em.getTransaction();
tx.begin();
for ( int i=0; i<100000; i++ ) {
Customer customer = new Customer(.....);
em.persist(customer);
if ( i % 20 == 0 ) { //20, same as the JDBC batch size
//flush a batch of inserts and release memory:
em.flush();
em.clear();
}
}
tx.commit();
session.close();
代码示例来源:origin: spring-projects/spring-framework
protected final TestEntity getTestEntity(String name) {
TestEntity te = entityManager.find(TestEntity.class, name);
if (te == null) {
te = new TestEntity(name, 0);
entityManager.persist(te);
}
return te;
}
代码示例来源:origin: spring-projects/spring-framework
@Test(expected = TransactionRequiredException.class)
public void transactionRequiredExceptionOnPersist() {
EntityManagerFactory emf = mock(EntityManagerFactory.class);
EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
em.persist(new Object());
}
代码示例来源:origin: stackoverflow.com
EntityManager em = emf.createEntityManager();
em.persist(emp);
em.close(); // Note: em can be closed before JTA tx committed.
em = emf.createEntityManager();
em.persist(emp);
Employee emp = em.find(...); // load the employee
em.close();
emf.close();
代码示例来源:origin: hibernate/hibernate-orm
@Test
@Priority(10)
public void initData() {
EntityManager em = getEntityManager();
em.getTransaction().begin();
DateTestEntity dte = new DateTestEntity( new Date( 12345000 ) );
em.persist( dte );
id1 = dte.getId();
em.getTransaction().commit();
em.getTransaction().begin();
dte = em.find( DateTestEntity.class, id1 );
dte.setDateValue( new Date( 45678000 ) );
em.getTransaction().commit();
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
@Priority(10)
public void initData() {
// Revision 1
EntityManager em = getEntityManager();
em.getTransaction().begin();
em.persist( testEntity );
em.getTransaction().commit();
em.close();
}
代码示例来源:origin: Impetus/Kundera
@Test
public void validConstructorTest() throws Exception
{
emf = Persistence.createEntityManagerFactory("hbaseTest");
em = emf.createEntityManager();
BookEntity book = new BookEntity();
book.setBookId(1);
book.setTitle("The Complete Reference");
book.setAuthor("Herbert Schildt");
book.setPages(500);
em.persist(book);
em.clear();
BookEntity book1 = em.find(BookEntity.class, 1);
Assert.assertNotNull(book1);
Assert.assertEquals(1, book1.getBookId());
Assert.assertEquals("The Complete Reference", book1.getTitle());
Assert.assertEquals("Herbert Schildt", book1.getAuthor());
Assert.assertEquals(500, book1.getPages());
em.remove(book1);
BookEntity book2 = em.find(BookEntity.class, 1);
Assert.assertNull(book2);
em.close();
emf.close();
}
代码示例来源:origin: Impetus/Kundera
@Test
public void testTypedQuery()
{
Object p1 = prepareData("1", 10);
Object p2 = prepareData("2", 20);
Object p3 = prepareData("3", 15);
em.persist(p1);
em.persist(p2);
em.persist(p3);
TypedQuery<Person> query = em.createQuery("Select p from Person p", Person.class);
List<Person> results = query.getResultList();
Assert.assertNotNull(query);
Assert.assertNotNull(results);
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
@Priority(10)
public void initData() {
final EntityManager em = getEntityManager();
final SetOwningEntity setOwningEntity = new SetOwningEntity( 1, "parent" );
final SetOwnedEntity setOwnedEntity = new SetOwnedEntity( 2, "child" );
// Revision 1: Initial persist
em.getTransaction().begin();
em.persist( setOwningEntity );
em.persist( setOwnedEntity );
em.getTransaction().commit();
em.clear();
ing_id = setOwningEntity.getId();
ed_id = setOwnedEntity.getId();
}
代码示例来源:origin: Impetus/Kundera
@Test
public void testUUID()
{
EntityManagerFactory emf = Persistence.createEntityManagerFactory("cass_pu");
UUID key = UUID.randomUUID();
CassandraUUIDEntity entity = new CassandraUUIDEntity();
entity.setAge(10);
entity.setName("vivek");
entity.setUuidKey(key);
EntityManager em = emf.createEntityManager();
em.persist(entity);
CassandraUUIDEntity result = em.find(CassandraUUIDEntity.class, key);
Assert.assertNotNull(result);
Assert.assertEquals(key, result.getUuidKey());
Assert.assertEquals("vivek", result.getName());
Assert.assertEquals(new Integer(10), result.getAge());
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void testLazyLoading() {
try {
Person tony = new Person();
tony.setFirstName("Tony");
tony.setLastName("Blair");
tony.setDriversLicense(new DriversLicense("8439DK"));
sharedEntityManager.persist(tony);
setComplete();
endTransaction();
startNewTransaction();
sharedEntityManager.clear();
Person newTony = entityManagerFactory.createEntityManager().getReference(Person.class, tony.getId());
assertNotSame(newTony, tony);
endTransaction();
assertNotNull(newTony.getDriversLicense());
newTony.getDriversLicense().getSerialNumber();
}
finally {
deleteFromTables("person", "drivers_license");
}
}
代码示例来源:origin: stackoverflow.com
EntityManager em = emf.createEntityManager();
try {
jtaTx.begin();
em.joinTransaction();
em.persist(emp);
em.persist(emp);
em.close(); // Note: em can be closed before JTA tx committed.
代码示例来源: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: stackoverflow.com
public void uniqueKey() {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("default");
ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
ValidatorContext validatorContext = validatorFactory.usingContext();
validatorContext.constraintValidatorFactory(new ConstraintValidatorFactoryImpl(entityManagerFactory));
Validator validator = validatorContext.getValidator();
EntityManager em = entityManagerFactory.createEntityManager();
User se = new User("abc", poizon);
Set<ConstraintViolation<User>> violations = validator.validate(se);
System.out.println("Size:- " + violations.size());
em.getTransaction().begin();
em.persist(se);
em.getTransaction().commit();
User se1 = new User("abc");
violations = validator.validate(se1);
System.out.println("Size:- " + violations.size());
}
代码示例来源:origin: hibernate/hibernate-orm
private void withBatch() {
int entityCount = 100;
//tag::batch-session-batch-insert-example[]
EntityManager entityManager = null;
EntityTransaction txn = null;
try {
entityManager = entityManagerFactory().createEntityManager();
txn = entityManager.getTransaction();
txn.begin();
int batchSize = 25;
for ( int i = 0; i < entityCount; i++ ) {
if ( i > 0 && i % batchSize == 0 ) {
//flush a batch of inserts and release memory
entityManager.flush();
entityManager.clear();
}
Person Person = new Person( String.format( "Person %d", i ) );
entityManager.persist( Person );
}
txn.commit();
} catch (RuntimeException e) {
if ( txn != null && txn.isActive()) txn.rollback();
throw e;
} finally {
if (entityManager != null) {
entityManager.close();
}
}
//end::batch-session-batch-insert-example[]
}
代码示例来源:origin: kiegroup/jbpm
@Override
public void saveMapping(Context context, Long ksessionId, String ownerId) {
EntityManagerInfo info = getEntityManager(context);
EntityManager em = info.getEntityManager();
em.persist(new ContextMappingInfo(resolveContext(context, em).getContextId().toString(),
ksessionId, ownerId));
if (!info.isShared()) {
em.close();
}
}
代码示例来源: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
@Test
@Priority(10)
public void initData() {
EntityManager em = getEntityManager();
em.getTransaction().begin();
PropertyAccessTypeEntity pate = new PropertyAccessTypeEntity( "data" );
em.persist( pate );
id1 = pate.getId();
em.getTransaction().commit();
em.getTransaction().begin();
pate = em.find( PropertyAccessTypeEntity.class, id1 );
pate.writeData( "data2" );
em.getTransaction().commit();
}
代码示例来源:origin: hibernate/hibernate-orm
@Test
@Priority(10)
public void initData() {
EntityManager em = getEntityManager();
// Revision 1
em.getTransaction().begin();
EffectiveEntity1 entity = new EffectiveEntity1( 1L, "commonField", "specificField1" );
em.persist( entity );
em.getTransaction().commit();
em.close();
}
代码示例来源:origin: hibernate/hibernate-orm
@Test(expected = RuntimeException.class)
public void testTransactionRollback() throws InterruptedException {
// Trying to persist an entity - however the listener should throw an exception, so the entity
// shouldn't be persisted
EntityManager em = getEntityManager();
em.getTransaction().begin();
StrTestEntity te = new StrTestEntity( "x" );
em.persist( te );
em.getTransaction().commit();
}
内容来源于网络,如有侵权,请联系作者删除!