本文整理汇总了Java中javax.persistence.Persistence.createEntityManagerFactory方法的典型用法代码示例。如果您正苦于以下问题:Java Persistence.createEntityManagerFactory方法的具体用法?Java Persistence.createEntityManagerFactory怎么用?Java Persistence.createEntityManagerFactory使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.persistence.Persistence
的用法示例。
在下文中一共展示了Persistence.createEntityManagerFactory方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: jpa
import javax.persistence.Persistence; //导入方法依赖的package包/类
@Test
public void jpa() {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("jpa");
Employee employee = new Employee();
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(employee);
entityManager.getTransaction().commit();
entityManager.close();
entityManagerFactory.close();
assertNotNull(employee.id);
List<MockSpan> finishedSpans = mockTracer.finishedSpans();
assertEquals(8, finishedSpans.size());
checkSpans(finishedSpans);
assertNull(mockTracer.activeSpan());
}
示例2: testPersistTwiceInNewFactory
import javax.persistence.Persistence; //导入方法依赖的package包/类
@Test
public void testPersistTwiceInNewFactory() {
User02 user02 = new User02();
user02.setId(1L);
boolean persisted = persistInATransaction(user02);
assertTrue(persisted);
em.close();
factory.close();
factory = Persistence.createEntityManagerFactory("DB");
em = factory.createEntityManager();
persisted = persistInATransaction(user02);
assertTrue(persisted);
/*
Here we managed to commit, because createEntityManagerFactory does
create a new database, as we used "create-drop" in the persisten.xml file
*/
}
开发者ID:arcuri82,项目名称:testing_security_development_enterprise_systems,代码行数:21,代码来源:IdHandlingTest.java
示例3: getPermission
import javax.persistence.Persistence; //导入方法依赖的package包/类
/**
* retrieves the <code>RolesPermission</code> by the role.
*
* @param role
* the role
* @return userPermissions the list of permissions of the user
*
*/
public static Set<String> getPermission(String role) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("userData");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<RolePermission> q = cb.createQuery(RolePermission.class);
Root<RolePermission> c = q.from(RolePermission.class);
q.select(c).where(cb.equal(c.get("roleName"), role));
TypedQuery<RolePermission> query = em.createQuery(q);
List<RolePermission> permissions = query.getResultList();
Set<String> userPermissions = new HashSet<String>();
for (RolePermission permission : permissions)
userPermissions.add(permission.getPermission());
tx.commit();
em.close();
return userPermissions;
}
示例4: main
import javax.persistence.Persistence; //导入方法依赖的package包/类
public static void main(String[] args) {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("pl.edu.bogdan.training.db.entity");
EntityManager em = entityManagerFactory.createEntityManager();
em.getTransaction().begin();
Query query = em
.createQuery("Select user from User user join user.role role where role.name = :name");
query.setParameter("name", "ordinary");
List<User> users = query.getResultList();
for (User user : users) {
System.out.println(user.getFirstName() + " " + user.getRole().getName());
}
em.getTransaction().commit();
em.close();
entityManagerFactory.close();
}
示例5: btn_ekleActionPerformed
import javax.persistence.Persistence; //导入方法依赖的package包/类
private void btn_ekleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_ekleActionPerformed
// TODO add your handling code here:
if (tbl_kisiliste.getSelectedRow() < 0) {
JOptionPane.showMessageDialog(rootPane, "Bir kullanıcı Seçmelisiniz");
return;
}
int id = (int) dtm1.getValueAt(tbl_kisiliste.getSelectedRow(), 0);
EntityManagerFactory emf = Persistence.createEntityManagerFactory("SosyalMedyaAppWithDatabasePU");
EntityManager em = emf.createEntityManager();
Query q = em.createQuery("SELECT u FROM USER_1 u WHERE u.id=:id");
q.setParameter("id", id);
USER_1 gelen = (USER_1) q.getSingleResult();
if (gelen == null) {
return;
}
ARKADASLIK yeni = new ARKADASLIK();
yeni.setId(3);
yeni.setIdUserIstek(Frm_Login.loginuser.getId());
yeni.setIdUserKabul(gelen.getId());
yeni.setKabulDurumu(false);
em.getTransaction().begin();
em.persist(yeni);
em.getTransaction().commit();
}
示例6: getAllBooks
import javax.persistence.Persistence; //导入方法依赖的package包/类
@Override
public List<Book> getAllBooks() {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("pl.edu.bogdan.training.db.entity");
EntityManager em = entityManagerFactory.createEntityManager();
// begining of transaction
em.getTransaction().begin();
Query query = em.createQuery("Select b from Book b");
return query.getResultList();
}
示例7: main
import javax.persistence.Persistence; //导入方法依赖的package包/类
public static void main(String[] args)
{
// create an EntityManagerFactory for the persistence unit
EntityManagerFactory entityManagerFactory =
Persistence.createEntityManagerFactory(
"BooksDatabaseExamplesPU");
// create an EntityManager for interacting with the persistence unit
EntityManager entityManager =
entityManagerFactory.createEntityManager();
// create a dynamic TypedQuery<Authors> that selects all authors
TypedQuery<Authors> findAllAuthors = entityManager.createQuery(
"SELECT author FROM Authors AS author", Authors.class);
// display List of Authors
System.out.printf("Authors Table of Books Database:%n%n");
System.out.printf("%-12s%-13s%s%n",
"Author ID", "First Name", "Last Name");
// get all authors, create a stream and display each author
findAllAuthors.getResultList().stream()
.forEach((author) ->
{
System.out.printf("%-12d%-13s%s%n", author.getAuthorid(),
author.getFirstname(), author.getLastname());
}
);
}
示例8: setUp
import javax.persistence.Persistence; //导入方法依赖的package包/类
/**
* Sets up a {@link SimpleJpaRepository} instance.
*/
@Before
public void setUp() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa.sample.plain");
em = factory.createEntityManager();
userRepository = new SimpleJpaRepository<User, Long>(User.class, em);
em.getTransaction().begin();
}
示例9: getAllPublishers
import javax.persistence.Persistence; //导入方法依赖的package包/类
@Override
public List<Publisher> getAllPublishers() {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("pl.edu.bogdan.training.db.entity");
EntityManager em = entityManagerFactory.createEntityManager();
// begining of transaction
em.getTransaction().begin();
Query query = em.createQuery("Select p from Publisher p");
return query.getResultList();
}
示例10: GetAllHasta
import javax.persistence.Persistence; //导入方法依赖的package包/类
public static List<Hasta> GetAllHasta() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("BP2_LAB2PU");
EntityManager em = emf.createEntityManager();
Query q = em.createQuery("SELECT h FROM Hasta h");
List<Hasta> hastalar = q.getResultList();
return hastalar;
}
示例11: getBooksByCategory
import javax.persistence.Persistence; //导入方法依赖的package包/类
public List<Book> getBooksByCategory() {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("pl.edu.bogdan.training.db.entity");
EntityManager em = entityManagerFactory.createEntityManager();
// begining of transaction
em.getTransaction().begin();
Category category = em.find(Category.class, Integer.parseInt(categoryId));
return category.getBooks();
}
示例12: createDao
import javax.persistence.Persistence; //导入方法依赖的package包/类
public static OperationDao createDao() {
OperationDao dao = new OperationDao();
OperationDtoDao dtoDao = new OperationDtoDao();
dtoDao.emf = Persistence.createEntityManagerFactory("OperationManagement");
dao.dao = dtoDao;
dao.operationFactory = new OperationFactory();
return dao;
}
示例13: createTestEMF
import javax.persistence.Persistence; //导入方法依赖的package包/类
private EntityManagerFactory createTestEMF() {
Map<String, String> properties = new HashMap<String, String>();
properties.put("javax.persistence.jdbc.driver", "org.apache.derby.jdbc.EmbeddedDriver");
properties.put("javax.persistence.jdbc.url", "jdbc:derby:memory:TEST;create=true");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("tasklist", properties);
return emf;
}
示例14: createTestEMF
import javax.persistence.Persistence; //导入方法依赖的package包/类
private EntityManagerFactory createTestEMF() {
Map<String, String> properties = new HashMap<String, String>();
properties.put("javax.persistence.jdbc.driver", "org.apache.derby.jdbc.EmbeddedDriver");
properties.put("javax.persistence.jdbc.url", "jdbc:derby:target/test;create=true");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("tasklist", properties);
return emf;
}
示例15: testIdPersistence
import javax.persistence.Persistence; //导入方法依赖的package包/类
@Test
public void testIdPersistence(){
EntityManagerFactory factory = Persistence.createEntityManagerFactory("DB");//same name as in persistence.xml
EntityManager em = factory.createEntityManager();//it works as a cache/buffer until we commit a transaction
User01 user01 = new User01();
user01.setName("AName");
user01.setSurname("ASurname");
// by default, no id, until data committed to the database
assertNull(user01.getId());
//committing data to database needs to be inside a transaction
EntityTransaction tx = em.getTransaction();
tx.begin();
try{
/*
The following is actually executing this SQL statement:
insert into User01 (name, surname, id) values (?, ?, ?)
*/
em.persist(user01);
//there can be several operations on the "cache" EntityManager before we actually commit the transaction
tx.commit();
} catch (Exception e){
//abort the transaction if there was any exception
tx.rollback();
fail();//fail the test
} finally {
//in any case, make sure to close the opened resources
em.close();
factory.close();
}
//id should have now be set
assertNotNull(user01.getId());
System.out.println("GENERATED ID: "+user01.getId());
}
开发者ID:arcuri82,项目名称:testing_security_development_enterprise_systems,代码行数:43,代码来源:IdCreationTest.java