org.hibernate.SessionFactory.getCurrentSession()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(8.8k)|赞(0)|评价(0)|浏览(165)

本文整理了Java中org.hibernate.SessionFactory.getCurrentSession()方法的一些代码示例,展示了SessionFactory.getCurrentSession()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。SessionFactory.getCurrentSession()方法的具体详情如下:
包路径:org.hibernate.SessionFactory
类名称:SessionFactory
方法名:getCurrentSession

SessionFactory.getCurrentSession介绍

[英]Obtains the current session. The definition of what exactly "current" means controlled by the org.hibernate.context.spi.CurrentSessionContext impl configured for use.

Note that for backwards compatibility, if a org.hibernate.context.spi.CurrentSessionContextis not configured but JTA is configured this will default to the org.hibernate.context.internal.JTASessionContextimpl.
[中]获取当前会话。“当前”的确切含义由组织控制。冬眠上下文spi。CurrentSessionContext impl已配置为可供使用。
请注意,为了向后兼容,如果一个组织。冬眠上下文spi。CurrentSessionContext未配置,但配置了JTA。这将默认为组织。冬眠上下文内部的JTASessionContextimpl。

代码示例

代码示例来源:origin: stackoverflow.com

return (T) sessionFactory.getCurrentSession().save(o);
  sessionFactory.getCurrentSession().delete(object);
  return (T) sessionFactory.getCurrentSession().get(type, id);
  return (T) sessionFactory.getCurrentSession().merge(o);
  sessionFactory.getCurrentSession().saveOrUpdate(o);
  final Session session = sessionFactory.getCurrentSession();
  final Criteria crit = session.createCriteria(type);
return crit.list();

代码示例来源:origin: stackoverflow.com

Session session = sessionFactory.getCurrentSession();
Integer personID = (Integer) session.save(p);
return personID;
Session session = sessionFactory.getCurrentSession();
Person retrievedPerson = (Person) session.get(Person.class, id);
return retrievedPerson;
Session session = sessionFactory.getCurrentSession();
Criteria criteria = session.createCriteria(Person.class);
return criteria.list();
Session session = sessionFactory.getCurrentSession();
Person personToDelete = getById(id);
session.delete(personToDelete);
Session session = sessionFactory.getCurrentSession();
session.update(personToUpdate);

代码示例来源:origin: gocd/gocd

@Override
  public Object doInTransaction(TransactionStatus transactionStatus) {
    return sessionFactory.getCurrentSession()
        .createCriteria(PipelineState.class)
        .add(Restrictions.eq("pipelineName", pipelineName))
        .setCacheable(false).uniqueResult();
  }
});

代码示例来源:origin: gocd/gocd

@Override
  public Object doInTransaction(TransactionStatus status) {
    PropertyProjection pipelineName = Projections.property("pipelineName");
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(PipelineState.class).setProjection(pipelineName).add(
        Restrictions.eq("locked", true));
    criteria.setCacheable(false);
    List<String> list = criteria.list();
    return list;
  }
});

代码示例来源:origin: spring-projects/spring-framework

@Override
public Person findByName(String name) {
  return (Person) this.sessionFactory.getCurrentSession().createQuery(
    "from Person person where person.name = :name").setParameter("name", name).getSingleResult();
}

代码示例来源:origin: gocd/gocd

public EnvironmentVariables load(final Long entityId, final EnvironmentVariableType type) {
  List<EnvironmentVariable> result = (List<EnvironmentVariable>) transactionTemplate.execute((TransactionCallback) transactionStatus -> {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(EnvironmentVariable.class).add(Restrictions.eq("entityId", entityId)).add(
        Restrictions.eq("entityType", type.toString())).addOrder(Order.asc("id"));
    criteria.setCacheable(true);
    return criteria.list();
  });
  return new EnvironmentVariables(result);
}

代码示例来源:origin: hibernate/hibernate-orm

public HolidayCalendar getHolidayCalendar() {
    Session session = sessionFactory.getCurrentSession();

    session.beginTransaction();

    List calendars = session.createQuery("from HolidayCalendar").setCacheable(true).list();

    session.getTransaction().commit();

    return calendars.isEmpty() ? null : (HolidayCalendar)calendars.get(0);
  }
}

代码示例来源:origin: gocd/gocd

@Override
public JobAgentMetadata load(final Long jobId) {
  return (JobAgentMetadata) transactionTemplate.execute((TransactionCallback) transactionStatus -> sessionFactory.getCurrentSession()
      .createCriteria(JobAgentMetadata.class)
      .add(Restrictions.eq("jobId", jobId))
      .setCacheable(true).uniqueResult());
}

代码示例来源:origin: hibernate/hibernate-orm

/**
 * Use a Criteria query - see FORGE-247
 */
public List listEventsWithCriteria() {
  Session session = sessionFactory.getCurrentSession();
  session.beginTransaction();
  List result = session.createCriteria(Event.class)
    .setCacheable(true)
    .list();
  session.getTransaction().commit();
  return result;
}

代码示例来源:origin: gocd/gocd

public Users findNotificationSubscribingUsers() {
  return (Users) transactionTemplate.execute((TransactionCallback) transactionStatus -> {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(User.class);
    criteria.setCacheable(true);
    criteria.add(Restrictions.isNotEmpty("notificationFilters"));
    criteria.add(Restrictions.eq("enabled", true));
    return new Users(criteria.list());
  });
}

代码示例来源:origin: hibernate/hibernate-orm

public HolidayCalendar getHolidayCalendar() {
    Session session = sessionFactory.getCurrentSession();

    session.beginTransaction();

    List calendars = session.createQuery("from HolidayCalendar").setCacheable(true).list();

    session.getTransaction().commit();

    return calendars.isEmpty() ? null : (HolidayCalendar)calendars.get(0);
  }
}

代码示例来源:origin: gocd/gocd

@Override
public VersionInfo findByComponentName(final String name) {
  return (VersionInfo) transactionTemplate.execute((TransactionCallback) transactionStatus -> sessionFactory.getCurrentSession()
      .createCriteria(VersionInfo.class)
      .add(Restrictions.eq("componentName", name))
      .setCacheable(true).uniqueResult());
}

代码示例来源:origin: hibernate/hibernate-orm

/**
 * Use a Criteria query - see FORGE-247
 */
public List listEventsWithCriteria() {
  Session session = sessionFactory.getCurrentSession();
  session.beginTransaction();
  List result = session.createCriteria(Event.class)
    .setCacheable(true)
    .list();
  session.getTransaction().commit();
  return result;
}

代码示例来源:origin: openmrs/openmrs-core

/**
 * @see org.openmrs.api.db.ProgramWorkflowDAO#getAllPrograms(boolean)
 */
@Override
@SuppressWarnings("unchecked")
public List<Program> getAllPrograms(boolean includeRetired) throws DAOException {
  Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Program.class);
  if (!includeRetired) {
    criteria.add(Restrictions.eq("retired", false));
  }
  return criteria.list();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testCurrentSession() {
  String firstName = "Tony";
  insertPerson(firstName);
  Query q = sessionFactory.getCurrentSession().createQuery("select p from Person as p");
  List<Person> people = q.getResultList();
  assertEquals(1, people.size());
  assertEquals(firstName, people.get(0).getFirstName());
  assertSame(applicationContext, people.get(0).postLoaded);
}

代码示例来源:origin: gocd/gocd

public User findUser(final String userName) {
  return (User) transactionTemplate.execute((TransactionCallback) transactionStatus -> {
    User user = (User) sessionFactory.getCurrentSession()
        .createCriteria(User.class)
        .add(Restrictions.eq("name", userName))
        .setCacheable(true).uniqueResult();
    return user == null ? new NullUser() : user;
  });
}

代码示例来源:origin: openmrs/openmrs-core

/**
 * @see org.openmrs.api.db.PatientDAO#getAllPatients(boolean)
 */
@SuppressWarnings("unchecked")
  @Override
public List<Patient> getAllPatients(boolean includeVoided) throws DAOException {
  Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Patient.class);
  
  if (!includeVoided) {
    criteria.add(Restrictions.eq("voided", false));
  }
  
  return criteria.list();
}

代码示例来源:origin: hibernate/hibernate-orm

/**
 * Call setEntity() on a cacheable query - see FORGE-265
 */
public List listEventsOfOrganizer(Person organizer) {
  Session session = sessionFactory.getCurrentSession();
  session.beginTransaction();
  Query query = session.createQuery("from Event ev where ev.organizer = :organizer");
  query.setCacheable(true);
  query.setEntity("organizer", organizer);
  List result = query.list();
  session.getTransaction().commit();
  return result;
}

代码示例来源:origin: gocd/gocd

@Override
public Plugin findPlugin(final String pluginId) {
  String cacheKey = cacheKeyForPluginSettings(pluginId);
  Plugin plugin = (Plugin) goCache.get(cacheKey);
  if (plugin != null) {
    return plugin;
  }
  synchronized (cacheKey) {
    plugin = (Plugin) goCache.get(cacheKey);
    if (plugin != null) {
      return plugin;
    }
    plugin = (Plugin) transactionTemplate.execute((TransactionCallback) transactionStatus -> sessionFactory.getCurrentSession()
        .createCriteria(Plugin.class)
        .add(Restrictions.eq("pluginId", pluginId))
        .setCacheable(true).uniqueResult());
    if (plugin != null) {
      goCache.put(cacheKey, plugin);
      return plugin;
    }
    goCache.remove(cacheKey);
    return new NullPlugin();
  }
}

代码示例来源:origin: openmrs/openmrs-core

/**
 * @see org.openmrs.api.db.FormDAO#getFormResourcesForForm(org.openmrs.Form)
 */
@Override
public Collection<FormResource> getFormResourcesForForm(Form form) {
  Criteria crit = sessionFactory.getCurrentSession().createCriteria(FormResource.class).add(
    Restrictions.eq("form", form));
  return crit.list();
}

相关文章