本文整理汇总了Java中javax.persistence.Persistence类的典型用法代码示例。如果您正苦于以下问题:Java Persistence类的具体用法?Java Persistence怎么用?Java Persistence使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Persistence类属于javax.persistence包,在下文中一共展示了Persistence类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<User> cq = cb.createQuery(User.class);
Root<User> from = cq.from(User.class);
Join<User, Role> join = from.join("role", JoinType.LEFT);
cq.where(cb.equal(join.get("name"), "ordinary"));
TypedQuery<User> tq = em.createQuery(cq);
List<User> users = tq.getResultList();
for (User u : users) {
System.out.println(u.getLastName());
}
em.getTransaction().commit();
em.close();
entityManagerFactory.close();
}
示例2: 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());
}
示例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: getUser
import javax.persistence.Persistence; //导入依赖的package包/类
public static User getUser(String username) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("userData");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<User> q = cb.createQuery(User.class);
Root<User> c = q.from(User.class);
q.select(c).where(cb.equal(c.get("username"), username));
TypedQuery<User> query = em.createQuery(q);
List<User> users = query.getResultList();
em.close();
LOGGER.info("found " + users.size() + " users with username " + username);
if (users.size() == 1)
return users.get(0);
else
return null;
}
示例5: jpaWithActiveSpanOnlyWithParent
import javax.persistence.Persistence; //导入依赖的package包/类
@Test
public void jpaWithActiveSpanOnlyWithParent() {
try (Scope activeSpan = mockTracer.buildSpan("parent").startActive(true)) {
EntityManagerFactory entityManagerFactory =
Persistence.createEntityManagerFactory("jpa_active_span_only");
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(9, finishedSpans.size());
checkSameTrace(finishedSpans);
assertNull(mockTracer.scopeManager().active());
}
示例6: main
import javax.persistence.Persistence; //导入依赖的package包/类
/**
* @param args the command line arguments
*/
@PersistenceUnit
public static void main(String[] args) {
System.out.println("Creating entity information...");
EntityManager entityManager = Persistence.createEntityManagerFactory("DataAppLibraryPULocal").createEntityManager();
EntityTransaction et = entityManager.getTransaction();
et.begin();
loadDiscountRate(entityManager);
loadRegion(entityManager);
loadRole(entityManager);
loadTransmission(entityManager);
loadProductType(entityManager);
loadEngine(entityManager);
loadProduct(entityManager);
et.commit();
EntityManager specialEntityManager = new InitialLoadEntityManagerProxy(entityManager);
SalesSimulator simulator = new SalesSimulator(specialEntityManager);
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
cal.clear();
cal.set(year-1, 0, 1, 0, 0, 0); // go back to begining of year, 3 years ago
System.out.println("Creating historical data...");
System.out.println(" This may take 5 to 15min depending on machine speed.");
simulator.run(cal.getTime(), new Date());
entityManager.close();
}
示例7: PreferencesDialog
import javax.persistence.Persistence; //导入依赖的package包/类
/** Creates new form PreferencesDialog */
public PreferencesDialog() {
initComponents();
emf = Persistence.createEntityManagerFactory("GeoImageViewerPU");
EntityManager em = emf.createEntityManager();
Query q = em.createNamedQuery("Preferences.findAll");
prefs = q.getResultList();
em.close();
setTitle("SUMO Preferences");
initTable();
}
示例8: PluginsManager
import javax.persistence.Persistence; //导入依赖的package包/类
public PluginsManager() {
plugins = new HashMap<String, Plugins>();
actions = new HashMap<String, ISumoAction>();
emf = Persistence.createEntityManagerFactory("GeoImageViewerPU");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Query q = em.createQuery("select p.className from Plugins p");
List<String> dbPuginNames = q.getResultList();
em.getTransaction().commit();
populateDatabase(dbPuginNames);
em.getTransaction().begin();
Query q2 = em.createQuery("select p from Plugins p");
List<Plugins> dbPlugins = q2.getResultList();
dbPlugins = q2.getResultList();
em.getTransaction().commit();
em.close();
List<ISumoAction> landActions=getDynamicActionForLandmask();
parseActions(dbPlugins);
parseActionsLandMask(landActions);
}
示例9: initEntityManager
import javax.persistence.Persistence; //导入依赖的package包/类
/**
* Set up memory database and insert data from test-dataset.xml
*
* @throws DatabaseUnitException
* @throws HibernateException
* @throws SQLException
*/
@BeforeClass
public static void initEntityManager() throws HibernateException, DatabaseUnitException, SQLException {
entityManagerFactory = Persistence.createEntityManagerFactory("listing-test-db");
entityManager = entityManagerFactory.createEntityManager();
connection = new DatabaseConnection(((SessionImpl) (entityManager.getDelegate())).connection());
connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new HsqldbDataTypeFactory());
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(datasetXml);
if (inputStream != null) {
FlatXmlDataSetBuilder flatXmlDataSetBuilder = new FlatXmlDataSetBuilder();
flatXmlDataSetBuilder.setColumnSensing(true);
dataset = flatXmlDataSetBuilder.build(inputStream);
DatabaseOperation.CLEAN_INSERT.execute(connection, dataset);
}
}
示例10: retrieveAuthor
import javax.persistence.Persistence; //导入依赖的package包/类
/**
* gets an author from the database by determining the type of the provided id. if no author is present it builds one from the id.
* @param id the author identifier
* @return the author retrieved from the database or build with the identifier
* @throws JDOMException thrown upon parsing the source response
* @throws IOException thrown upon reading profiles from disc
* @throws SAXException thrown when parsing the files from disc
*/
public PublicationAuthor retrieveAuthor(String id) throws JDOMException, IOException, SAXException {
typeOfID = determineID(id);
LOGGER.info("given ID: " + id + " is of type " + typeOfID);
EntityManagerFactory emf = Persistence.createEntityManagerFactory("publicationAuthors");
EntityManager em = emf.createEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<PublicationAuthor> q = cb.createQuery(PublicationAuthor.class);
Root<PublicationAuthor> c = q.from(PublicationAuthor.class);
List<Predicate> predicates = new ArrayList<>();
if (typeOfID.equals("surname")) {
if (id.contains(",")) {
predicates.add(cb.equal(c.get("surname"),id.substring(0,id.indexOf(","))));
predicates.add(cb.equal(c.get("firstname"),id.substring(id.indexOf(",")+1)));
LOGGER.info("retriving surname, firstname from database for " + id);
} else if (id.contains(" ")) {
predicates.add(cb.equal(c.get("firstname"),id.substring(0,id.indexOf(" "))));
predicates.add(cb.equal(c.get("surname"),id.substring(id.indexOf(" ")+1)));
LOGGER.info("retrieving firstname surname from database for " + id);
} else {
predicates.add(cb.equal(c.get("surname"), id));
LOGGER.info("retrieving surname from database for " + id);
}
}
predicates.add(cb.equal(c.get(typeOfID), id));
q.select(c).where(cb.equal(c.get(typeOfID), id));
TypedQuery<PublicationAuthor> query = em.createQuery(q);
List<PublicationAuthor> authors = query.getResultList();
em.close();
if (authors.size() == 1) {
LOGGER.info("found author in database");
this.author = authors.get(0);
return author;
}
LOGGER.info("no match in database");
return buildAuthor(id);
}
示例11: connect
import javax.persistence.Persistence; //导入依赖的package包/类
@BeforeClass
public static void connect() throws DatabaseException {
factory = Persistence.createEntityManagerFactory(persistencUnitName);
assertNotNull(factory);
em = factory.createEntityManager();
assertNotNull(em);
tx = em.getTransaction();
assertNotNull(tx);
edao.setEntityManager(em);
tx.begin();
for (int i = 1; i <= 7; i++) {
if (edao.findById(i) == null)
edao.createEnumeration(i);
}
tx.commit();
}
示例12: getRoles
import javax.persistence.Persistence; //导入依赖的package包/类
/**
* retrieves the list of roles for a given username.
*
* @param username
* the username
* @return userRoles the roles
*
*/
public static Set<String> getRoles(String username) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("userData");
EntityManager em = emf.createEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<UserRole> q = cb.createQuery(UserRole.class);
Root<UserRole> c = q.from(UserRole.class);
q.select(c).where(cb.equal(c.get("username"), username));
TypedQuery<UserRole> query = em.createQuery(q);
List<UserRole> roles = query.getResultList();
Set<String> userRoles = new HashSet<String>();
em.close();
for (UserRole role : roles)
userRoles.add(role.getRoleName());
return userRoles;
}
示例13: getVersion
import javax.persistence.Persistence; //导入依赖的package包/类
/**
* @see fr.univlorraine.ecandidat.services.siscol.SiScolGenericService#getVersion()
*/
@Override
public Version getVersion() throws SiScolException {
try {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("pun-jpa-siscol");
EntityManager em = emf.createEntityManager();
Query query = em.createQuery("Select a from VersionApo a where a.datCre is not null order by a.datCre desc",
VersionApo.class).setMaxResults(1);
List<VersionApo> listeVersionApo = query.getResultList();
em.close();
if (listeVersionApo != null && listeVersionApo.size() > 0) {
VersionApo versionApo = listeVersionApo.get(0);
return new Version(versionApo.getId().getCodVer());
} else {
return null;
}
} catch (Exception e) {
throw new SiScolException("SiScol database error on getVersion", e.getCause());
}
}
示例14: jpa_with_active_span_only
import javax.persistence.Persistence; //导入依赖的package包/类
@Test
public void jpa_with_active_span_only() {
EntityManagerFactory entityManagerFactory = Persistence
.createEntityManagerFactory("jpa_active_span_only");
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(0, finishedSpans.size());
assertNull(mockTracer.activeSpan());
}
示例15: getVoeuxApogee
import javax.persistence.Persistence; //导入依赖的package包/类
/**
* Renvoie les voeux OPI d'un individu
*
* @param indOpi
* @return
* @throws SiScolException
*/
private List<VoeuxIns> getVoeuxApogee(IndOpi indOpi) throws SiScolException {
try {
String queryString = "Select a from VoeuxIns a where a.id.codIndOpi = " + indOpi.getCodIndOpi();
logger.debug("Vérification des voeux " + queryString);
EntityManagerFactory emf = Persistence.createEntityManagerFactory("pun-jpa-siscol");
EntityManager em = emf.createEntityManager();
Query query = em.createQuery(queryString, VoeuxIns.class);
List<VoeuxIns> listeSiScol = query.getResultList();
em.close();
return listeSiScol;
} catch (Exception e) {
throw new SiScolException("SiScol database error on getVoeuxApogee", e);
}
}