当前位置: 首页>>代码示例>>Java>>正文


Java LazyInitializationException类代码示例

本文整理汇总了Java中org.hibernate.LazyInitializationException的典型用法代码示例。如果您正苦于以下问题:Java LazyInitializationException类的具体用法?Java LazyInitializationException怎么用?Java LazyInitializationException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


LazyInitializationException类属于org.hibernate包,在下文中一共展示了LazyInitializationException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: initialize

import org.hibernate.LazyInitializationException; //导入依赖的package包/类
@Override
public final void initialize() throws HibernateException {
	if ( !initialized ) {
		if ( allowLoadOutsideTransaction ) {
			permissiveInitialization();
		}
		else if ( session == null ) {
			throw new LazyInitializationException( "could not initialize proxy - no Session" );
		}
		else if ( !session.isOpen() ) {
			throw new LazyInitializationException( "could not initialize proxy - the owning Session was closed" );
		}
		else if ( !session.isConnected() ) {
			throw new LazyInitializationException( "could not initialize proxy - the owning Session is disconnected" );
		}
		else {
			target = session.immediateLoad( entityName, id );
			initialized = true;
			checkTargetState();
		}
	}
	else {
		checkTargetState();
	}
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:26,代码来源:AbstractLazyInitializer.java

示例2: getDepartment

import org.hibernate.LazyInitializationException; //导入依赖的package包/类
public Department getDepartment() {
    Department dept = null;
    try {
        dept = this.getSubjectArea().getDepartment();
        if(dept.toString()==null) {
        }
    }
    catch (LazyInitializationException lie) {
        new _RootDAO().getSession().refresh(this);
        dept = this.getSubjectArea().getDepartment();
    }
    
	return (dept);
}
 
开发者ID:Jenner4S,项目名称:unitimes,代码行数:15,代码来源:CourseOffering.java

示例3: selectFromWithoutJoinFetch

import org.hibernate.LazyInitializationException; //导入依赖的package包/类
@Test(expected = LazyInitializationException.class)
public void selectFromWithoutJoinFetch() {
	log.info("... selectFromWithoutJoinFetch ...");

	EntityManager em = emf.createEntityManager();
	em.getTransaction().begin();

	Author a = em.createQuery("SELECT a FROM Author a WHERE id = 1", Author.class).getSingleResult();
	
	log.info("Commit transaction and close Session");
	em.getTransaction().commit();
	em.close();
	
	try {
		log.info(a.getFirstName()+" "+a.getLastName()+" wrote "+a.getBooks().size()+" books.");
	} catch (Exception e) {
		log.error(e);
		throw e;
	}
}
 
开发者ID:thjanssen,项目名称:HibernateTips,代码行数:21,代码来源:TestJoinFetch.java

示例4: initialize

import org.hibernate.LazyInitializationException; //导入依赖的package包/类
public final void initialize() throws HibernateException {
	if (!initialized) {
		if ( session==null ) {
			throw new LazyInitializationException("could not initialize proxy - no Session");
		}
		else if ( !session.isOpen() ) {
			throw new LazyInitializationException("could not initialize proxy - the owning Session was closed");
		}
		else if ( !session.isConnected() ) {
			throw new LazyInitializationException("could not initialize proxy - the owning Session is disconnected");
		}
		else {
			target = session.immediateLoad(entityName, id);
			initialized = true;
			checkTargetState();
		}
	}
	else {
		checkTargetState();
	}
}
 
开发者ID:cacheonix,项目名称:cacheonix-core,代码行数:22,代码来源:AbstractLazyInitializer.java

示例5: getTableCellRendererComponent

import org.hibernate.LazyInitializationException; //导入依赖的package包/类
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
		boolean isSelected, boolean hasFocus, int row, int column) {
	JLabel lbText = (JLabel) super.getTableCellRendererComponent(table,
			null, isSelected, hasFocus, row, column);
	if (value == null || value instanceof String) {
		return lbText;
	}
	try {
		lbText.setText(value.toString());
	} catch (LazyInitializationException lie) {
		MainFrame mainFrame = (MainFrame) table
				.getClientProperty(ClientPropertyName.MAIN_FRAME.toString());
		BookModel model = mainFrame.getBookModel();
		Session session = model.beginTransaction();
		session.refresh((Chapter) value);
		lbText.setText(value.toString());
		model.commit();
	} catch (Exception e) {
		e.printStackTrace();
	}
	return lbText;
}
 
开发者ID:markkohdev,项目名称:oStorybook,代码行数:24,代码来源:ChapterTableCellRenderer.java

示例6: refire

import org.hibernate.LazyInitializationException; //导入依赖的package包/类
private void refire() {
    EntityManager em = getEntityManager();
    try {
        for (AlertDefinition def : getAlertDefs(em)) {
            try {
                checkRefire(def);
            } catch (LazyInitializationException e) {
                // failed to lazily initialize a collection of role: org.rhq.core.domain.alert.AlertDefinition.alerts, could not initialize proxy - no Session
                log.debug("spurious exception?", e);
                def = em.find(AlertDefinition.class, def.getId());
                checkRefire(def);
            }
        }
    } finally {
        em.close();
    }
}
 
开发者ID:genman,项目名称:rhq-server-plugins,代码行数:18,代码来源:AlertCleanup.java

示例7: intercept

import org.hibernate.LazyInitializationException; //导入依赖的package包/类
/**
 * Interception of access to the named field
 *
 * @param target The call target
 * @param fieldName The name of the field.
 * @param value The value.
 *
 * @return ?
 */
protected final Object intercept(Object target, String fieldName, Object value) {
	if ( initializing ) {
		return value;
	}

	if ( uninitializedFields != null && uninitializedFields.contains( fieldName ) ) {
		if ( session == null ) {
			throw new LazyInitializationException( "entity with lazy properties is not associated with a session" );
		}
		else if ( !session.isOpen() || !session.isConnected() ) {
			throw new LazyInitializationException( "session is not connected" );
		}

		final Object result;
		initializing = true;
		try {
			result = ( (LazyPropertyInitializer) session.getFactory().getEntityPersister( entityName ) )
					.initializeLazyProperty( fieldName, target, session );
		}
		finally {
			initializing = false;
		}
		// let's assume that there is only one lazy fetch group, for now!
		uninitializedFields = null;
		return result;
	}
	else {
		return value;
	}
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:40,代码来源:AbstractFieldInterceptor.java

示例8: throwLazyInitializationException

import org.hibernate.LazyInitializationException; //导入依赖的package包/类
private void throwLazyInitializationException(String message) {
	throw new LazyInitializationException(
			"failed to lazily initialize a collection" +
					(role == null ? "" : " of role: " + role) +
					", " + message
	);
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:8,代码来源:AbstractPersistentCollection.java

示例9: bfsTraverse

import org.hibernate.LazyInitializationException; //导入依赖的package包/类
/**
 * <strong>使用广度优先搜索收集</strong>当前实体及其所有子孙实体到集合,叶子实体排在最前面<br/>
 * 这样才能保证先删除子孙节点,后删除父节点,避免违反外键约束
 * @param currentEntity 存放当前实体及其所有子孙实体的集合
 * @param treeEntities 所有实体的集合
 * @return 当前实体及其所有子孙实体的集合,叶子实体排在集合最前面
 */
@SuppressWarnings("unchecked")
@Transactional
private LinkedList<SelfReference<T>> bfsTraverse(SelfReference<T> currentEntity, LinkedList<SelfReference<T>> treeEntities) {

    // 无法获取子孙类目时,在事务中重新获取当前节点
    try {
        currentEntity.getChildren().size();
    } catch (LazyInitializationException e) {
        ID id = (ID) ((BaseEntity)currentEntity).getId();
        currentEntity = (SelfReference<T>) findOne(id);
    }

    // 队列,用于广度优先遍历。每一时刻,队列所包含的节点是那些本身已经被访问,而它的邻居(这里即子节点)还有未被访问的节点
    Queue<SelfReference<T>> queue = new ArrayDeque<>();
    treeEntities.addFirst(currentEntity); // 对当前遍历到的元素执行的操作:加在头部,后续用于删除节点(避免违反外键约束)
    queue.add(currentEntity); // 加在尾部

    while (queue.size() != 0) { // 直到队列为空
        SelfReference<T> parent = queue.remove(); // 移除在队列头部的节点

        if(parent.getChildren().size() != 0) {
            parent.getChildren().forEach(child -> {
                treeEntities.addFirst((SelfReference<T>) child); // 对当前遍历到的元素执行的操作:加在头部
                queue.add((SelfReference<T>) child); // 加在尾部
            });
        }
    }

    return treeEntities;
}
 
开发者ID:chaokunyang,项目名称:amanda,代码行数:38,代码来源:AbstractSelfReferenceRepository.java

示例10: getRooms

import org.hibernate.LazyInitializationException; //导入依赖的package包/类
public Set<Location> getRooms() {
	try {
		return super.getRooms();
	} catch (LazyInitializationException e) {
		(new AssignmentDAO()).getSession().merge(this);
		return super.getRooms();
	}
}
 
开发者ID:Jenner4S,项目名称:unitimes,代码行数:9,代码来源:Assignment.java

示例11: getHtmlHint

import org.hibernate.LazyInitializationException; //导入依赖的package包/类
@Deprecated
  public String getHtmlHint(String preference) {
try {
	if (!Hibernate.isPropertyInitialized(this, "roomType") || !Hibernate.isInitialized(getRoomType())) {
		return LocationDAO.getInstance().get(getUniqueId()).getHtmlHintImpl(preference);
	} else {
		return getHtmlHintImpl(preference);
	}
} catch (LazyInitializationException e) {
	return LocationDAO.getInstance().get(getUniqueId()).getHtmlHintImpl(preference);
}
  }
 
开发者ID:Jenner4S,项目名称:unitimes,代码行数:13,代码来源:Location.java

示例12: testFailLazyAccess

import org.hibernate.LazyInitializationException; //导入依赖的package包/类
@Test(expected = LazyInitializationException.class)
public void testFailLazyAccess() {
	User user = entityManager.find(User.class, 1L);
	assertNotNull(user);
	// Lazy init exception cars is mapped as lazy
	user.getCars().stream().forEach(System.out::println);
}
 
开发者ID:Javatar81,项目名称:code-examples,代码行数:8,代码来源:StaticFetchTest.java

示例13: testFailLazyAccessByExplicitJoinInJPAQL

import org.hibernate.LazyInitializationException; //导入依赖的package包/类
@Test(expected = LazyInitializationException.class)
public void testFailLazyAccessByExplicitJoinInJPAQL() {
	TypedQuery<User> loadUserQuery = entityManager.createQuery(
			"select usr from User usr left outer join usr.cars cars where cars.licensePlate = :licensePlate",
			User.class);
	loadUserQuery.setParameter("licensePlate", "HIBERNATE");
	User user = loadUserQuery.getSingleResult();
	user.getCars().stream().forEach(System.out::println);
}
 
开发者ID:Javatar81,项目名称:code-examples,代码行数:10,代码来源:DynamicFetchTest.java

示例14: testGetUserLazyProperties

import org.hibernate.LazyInitializationException; //导入依赖的package包/类
@Test
public void testGetUserLazyProperties() {
    List<User> users = service.getAllUsers4LazyTest();
    assertNotNull(users);
    assertThat(users.size(), greaterThan(0));
    User user = users.get(0);
    assertThat(user, notNullValue());
    assertThat(user.getName(), notNullValue());

    try {
        user.getType().getValue();
    } catch (Exception e) {
        assertThat(e, instanceOf(LazyInitializationException.class));
    }
}
 
开发者ID:jimmyblylee,项目名称:example-spring-hibernate,代码行数:16,代码来源:ServiceTest.java

示例15: testGetUserLazyProperties

import org.hibernate.LazyInitializationException; //导入依赖的package包/类
@Test
public void testGetUserLazyProperties() {
    List<User> users = service.getAllUsers4LazyTest();
    assertNotNull(users);
    assertThat(users.size(), greaterThan(0));
    User user = users.get(0);
    assertThat(user, notNullValue());
    assertThat(user.getName(), notNullValue());
    
    try {
        user.getType().getValue();
    } catch (Exception e) {
        assertThat(e, instanceOf(LazyInitializationException.class));
    }
}
 
开发者ID:jimmyblylee,项目名称:example-spring-hibernate,代码行数:16,代码来源:ServiceTest.java


注:本文中的org.hibernate.LazyInitializationException类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。