本文整理汇总了Java中org.hibernate.SessionFactory类的典型用法代码示例。如果您正苦于以下问题:Java SessionFactory类的具体用法?Java SessionFactory怎么用?Java SessionFactory使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SessionFactory类属于org.hibernate包,在下文中一共展示了SessionFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getSessionFactory
import org.hibernate.SessionFactory; //导入依赖的package包/类
public static SessionFactory getSessionFactory() {
if (null != sessionFactory)
return sessionFactory;
Configuration configuration = new Configuration();
String jdbcUrl = "jdbc:mysql://"
+ System.getenv("RDS_HOSTNAME")
+ "/"
+ System.getenv("RDS_DB_NAME");
configuration.setProperty("hibernate.connection.url", jdbcUrl);
configuration.setProperty("hibernate.connection.username", System.getenv("RDS_USERNAME"));
configuration.setProperty("hibernate.connection.password", System.getenv("RDS_PASSWORD"));
configuration.configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
try {
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (HibernateException e) {
System.err.println("Initial SessionFactory creation failed." + e);
throw new ExceptionInInitializerError(e);
}
return sessionFactory;
}
示例2: SpringSessionSynchronization
import org.hibernate.SessionFactory; //导入依赖的package包/类
public SpringSessionSynchronization(
SessionHolder sessionHolder, SessionFactory sessionFactory,
SQLExceptionTranslator jdbcExceptionTranslator, boolean newSession) {
this.sessionHolder = sessionHolder;
this.sessionFactory = sessionFactory;
this.jdbcExceptionTranslator = jdbcExceptionTranslator;
this.newSession = newSession;
// Check whether the SessionFactory has a JTA TransactionManager.
TransactionManager jtaTm =
SessionFactoryUtils.getJtaTransactionManager(sessionFactory, sessionHolder.getAnySession());
if (jtaTm != null) {
this.hibernateTransactionCompletion = true;
// Fetch current JTA Transaction object
// (just necessary for JTA transaction suspension, with an individual
// Hibernate Session per currently active/suspended transaction).
try {
this.jtaTransaction = jtaTm.getTransaction();
}
catch (SystemException ex) {
throw new DataAccessResourceFailureException("Could not access JTA transaction", ex);
}
}
}
示例3: withActiveSpanOnlyNoParent
import org.hibernate.SessionFactory; //导入依赖的package包/类
@Test
public void withActiveSpanOnlyNoParent() throws InterruptedException {
SessionFactory sessionFactory = createSessionFactory(";traceWithActiveSpanOnly=true");
Session session = sessionFactory.openSession();
Employee employee = new Employee();
session.beginTransaction();
session.save(employee);
session.getTransaction().commit();
session.close();
sessionFactory.close();
List<MockSpan> finishedSpans = mockTracer.finishedSpans();
assertEquals(0, finishedSpans.size());
assertNull(mockTracer.scopeManager().active());
}
示例4: getNumberOfEntities
import org.hibernate.SessionFactory; //导入依赖的package包/类
@Override
public long getNumberOfEntities(SessionFactory sessionFactory) {
RedisClusterCommands<String, String> connection = getConnection( sessionFactory );
List<String> keys = connection.keys( "*" );
long result = 0;
for ( String key : keys ) {
if ( key.startsWith( AbstractRedisDialect.ASSOCIATIONS ) || key.startsWith( AbstractRedisDialect.IDENTIFIERS ) ) {
continue;
}
String type = connection.type( key );
if ( type.equals( "hash" ) || type.equals( "string" ) ) {
result++;
}
}
return result;
}
示例5: getNumberOfAssociations
import org.hibernate.SessionFactory; //导入依赖的package包/类
@Override
public long getNumberOfAssociations(SessionFactory sessionFactory) {
int associationCount = 0;
IgniteDatastoreProvider datastoreProvider = getProvider( sessionFactory );
for ( CollectionPersister collectionPersister : ( (SessionFactoryImplementor) sessionFactory ).getCollectionPersisters().values() ) {
AssociationKeyMetadata associationKeyMetadata = ( (OgmCollectionPersister) collectionPersister ).getAssociationKeyMetadata();
if ( associationKeyMetadata.getAssociationKind() == AssociationKind.ASSOCIATION ) {
IgniteCache<Object, BinaryObject> associationCache = getAssociationCache( sessionFactory, associationKeyMetadata );
StringBuilder query = new StringBuilder( "SELECT " )
.append( StringHelper.realColumnName( associationKeyMetadata.getColumnNames()[0] ) )
.append( " FROM " ).append( associationKeyMetadata.getTable() );
SqlFieldsQuery sqlQuery = datastoreProvider.createSqlFieldsQueryWithLog( query.toString(), null );
Iterable<List<?>> queryResult = associationCache.query( sqlQuery );
Set<Object> uniqs = new HashSet<>();
for ( List<?> row : queryResult ) {
Object value = row.get( 0 );
if ( value != null ) {
uniqs.add( value );
}
}
associationCount += uniqs.size();
}
}
return associationCount;
}
示例6: queryForInt
import org.hibernate.SessionFactory; //导入依赖的package包/类
private int queryForInt(String hql,Object[] parameters,Map<String,Object> parameterMap,String dataSourceName){
SessionFactory factory=this.getSessionFactory(dataSourceName);
Session session=factory.openSession();
int count=0;
try{
Query countQuery=session.createQuery(hql);
if(parameters!=null){
setQueryParameters(countQuery,parameters);
}else if(parameterMap!=null){
setQueryParameters(countQuery,parameterMap);
}
Object countObj=countQuery.uniqueResult();
if(countObj instanceof Long){
count=((Long)countObj).intValue();
}else if(countObj instanceof Integer){
count=((Integer)countObj).intValue();
}
}finally{
session.close();
}
return count;
}
示例7: processDeferredClose
import org.hibernate.SessionFactory; //导入依赖的package包/类
/**
* Process all Hibernate Sessions that have been registered for deferred close
* for the given SessionFactory.
* @param sessionFactory the Hibernate SessionFactory to process deferred close for
* @see #initDeferredClose
* @see #releaseSession
*/
public static void processDeferredClose(SessionFactory sessionFactory) {
Assert.notNull(sessionFactory, "No SessionFactory specified");
Map<SessionFactory, Set<Session>> holderMap = deferredCloseHolder.get();
if (holderMap == null || !holderMap.containsKey(sessionFactory)) {
throw new IllegalStateException("Deferred close not active for SessionFactory [" + sessionFactory + "]");
}
logger.debug("Processing deferred close of Hibernate Sessions");
Set<Session> sessions = holderMap.remove(sessionFactory);
for (Session session : sessions) {
closeSession(session);
}
if (holderMap.isEmpty()) {
deferredCloseHolder.remove();
}
}
示例8: testGetAdmin
import org.hibernate.SessionFactory; //导入依赖的package包/类
@Test
public void testGetAdmin(){
SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory");
Session session = sessionFactory.openSession();
Admin admin = (Admin) session.get(Admin.class, 1);
System.out.println(admin);
System.out.println(admin.getAuthorization().getSuperSet());
session.close();
}
示例9: getSessionFactory
import org.hibernate.SessionFactory; //导入依赖的package包/类
synchronized SessionFactory getSessionFactory()
{
if( sessionFactory == null )
{
sessionFactory = getHibernateFactory().getSessionFactory();
}
return sessionFactory;
}
示例10: getCountOfAllNewsInfo
import org.hibernate.SessionFactory; //导入依赖的package包/类
/**
* 获得记录总数
*/
@Override
public Integer getCountOfAllNewsInfo() {
Session session = SessionFactory.getCurrentSession();
Criteria criteria = session.createCriteria(NewsInfo.class);
return criteria.list().size();
}
示例11: getSessionFactory
import org.hibernate.SessionFactory; //导入依赖的package包/类
private static SessionFactory getSessionFactory() {
if (HibernateSessionManager.sessionFactory == null) {
WebApplicationContext wac = WebApplicationContextUtils
.getRequiredWebApplicationContext(SessionManager.getServletContext());
HibernateSessionManager.sessionFactory = (SessionFactory) wac.getBean("coreSessionFactory");
}
return HibernateSessionManager.sessionFactory;
}
示例12: dropSchemaAndDatabase
import org.hibernate.SessionFactory; //导入依赖的package包/类
@Override
public void dropSchemaAndDatabase(SessionFactory sessionFactory) {
if ( Ignition.allGrids().size() > 1 ) { // some tests doesn't stop DatastareProvider
String currentGridName = getProvider( sessionFactory ).getGridName();
for ( Ignite grid : Ignition.allGrids() ) {
if ( !Objects.equals( currentGridName, grid.name() ) ) {
grid.close();
}
}
}
}
示例13: testSaveAdmin
import org.hibernate.SessionFactory; //导入依赖的package包/类
@Test
public void testSaveAdmin(){
SessionFactory sessionFactory = (SessionFactory)context.getBean("sessionFactory");
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Admin admin = new Admin();
admin.setName("cairou");
admin.setUsername("admin");
admin.setPwd(Md5Utils.md5("admin"));
session.save(admin);
transaction.commit();
session.close();
}
示例14: getTransactionManager
import org.hibernate.SessionFactory; //导入依赖的package包/类
@Bean(name = "transactionManager")
public HibernateTransactionManager getTransactionManager(
SessionFactory sessionFactory) {
HibernateTransactionManager transactionManager = new HibernateTransactionManager(
sessionFactory);
return transactionManager;
}
示例15: setApplicationContext
import org.hibernate.SessionFactory; //导入依赖的package包/类
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
sessionFactoryBeanList = applicationContext.getBeansOfType(HibernateSessionFactory.class)
.values();
for (HibernateSessionFactory bean : sessionFactoryBeanList) {
SessionFactory sessionFactory = bean.getObject();
sessionFactoryMap.put(bean.getDataSourceName(), sessionFactory);
if (bean.isAsDefault()) {
defaultSessionFactory = sessionFactory;
defaultSessionFactoryName = bean.getDataSourceName();
}
}
}