`
weiweichen1985
  • 浏览: 142143 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

关于org.hibernate.NonUniqueObjectException

阅读更多
具体异常说明:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:

解决方案:
在多次检索过程中,共用一个Session对象


示例:
-----------------------------
/**
	 * <p>
	 * 更改指定用户的角色
	 * </p>
	 * 
	 * @author chenwei
	 * @param eid:用户ID
	 * @param roleids:角色ID数组
	 * 
	 */
	public static void opeRolesOfEmp(Integer eid, String[] roleids)
			throws HibernateMsgException {
		org.hibernate.Session session = null;
		org.hibernate.Transaction tran = null;
		try {
			session = com.supersit.hibernate.factory.HibernateSessionFactory
					.getSession();
			tran = session.beginTransaction();
			Employee emp = (Employee) session.load(Employee.class, eid);
			java.util.Set roles = new java.util.HashSet();
			for (int i = 0; i < roleids.length; i++) {
				//查询角色(此处应传入一个Session对象,否则会抛出org.hibernate.NonUniqueObjectException)
				Role role = new RoleDao().getRoleById(session,new Integer(roleids[i]));
				System.out.println("rolename=" + role.getRolename());
				roles.add(role);
			}
			emp.setRoles(roles);
			session.saveOrUpdate(emp);
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			com.supersit.hibernate.factory.HibernateSessionFactory
					.rollbackTran(tran);
		} finally {
			com.supersit.hibernate.factory.HibernateSessionFactory
					.closeSession(session);
		}
	}




/**
	 * <p>
	 * 根据角色ID查找角色(传入一个Session对象)
	 * </p>
	 * 
	 * @param roleid:角色ID
	 * @return com.supersit.hibernate.bean.Role
	 * @throws HibernateMsgException
	 */
	public com.supersit.hibernate.bean.Role getRoleById(Session session,
			Integer roleid) throws HibernateMsgException {
		Role role = new Role();
		try {
			java.util.List list = session.createQuery(
					"from Role r where r.roleid=:id").setInteger("id", roleid)
					.list();
			if (list != null && list.size() > 0)
				role = (Role) list.get(0);

		} catch (Exception e) {

		}
		return role;
	}




/**
	 * <p>
	 * 根据角色ID查找角色(创建一个新的Session)
	 * </p>
	 * 
	 * @param roleid:角色ID
	 * @return com.supersit.hibernate.bean.Role
	 * @throws HibernateMsgException
	 */
	public com.supersit.hibernate.bean.Role getRoleById(Integer roleid)
			throws HibernateMsgException {
		Role role = new Role();
		Session session = null;
		Transaction tran = null;
		try {
			session = com.supersit.hibernate.factory.HibernateSessionFactory
					.getSession();
			tran = session.beginTransaction();
			java.util.List list = session.createQuery(
					"from Role r where r.roleid=:id").setInteger("id", roleid)
					.list();
			if (list != null && list.size() > 0)
				role = (Role) list.get(0);
			tran.commit();
		} catch (Exception e) {
			com.supersit.hibernate.factory.HibernateSessionFactory
					.rollbackTran(tran);
		} finally {
			com.supersit.hibernate.factory.HibernateSessionFactory
					.closeSession(session);
		}
		return role;
	}
分享到:
评论

相关推荐

    weblogic12 下 org.hibernate.hql.ast.HqlToken

    ### WebLogic 12下org.hibernate.hql.ast.HqlToken冲突解决方案 在使用WebLogic 12部署应用程序时,可能会遇到与`org.hibernate.hql.ast.HqlToken`相关的异常问题。这种异常通常与Hibernate版本之间的不兼容性有关...

    org.springframework.orm.hibernate3.LocalSessionFactoryBean

    hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect"); hibernateProperties.setProperty("hibernate.show_sql", "true"); sessionFactory.setHibernateProperties...

    org.hibernate.ejb-library-3.4.0.GA-A

    标题 "org.hibernate.ejb-library-3.4.0.GA-A" 指示这是一个与Hibernate相关的库,特别针对EJB(Enterprise JavaBeans)版本3.4.0,并且是为OSGi环境优化的。描述中提到它适用于在Virgo Jetty服务器上进行Web开发,...

    org.hibernate.HibernateException: No Hibernate Session bound to thread

    Hibernate Session 绑定线程解决方案 在 Java web 开发中, Hibernate 是一个非常流行的 ORM(Object-Relational Mapping)框架,用于将 Java 对象映射到关系数据库中。然而,在使用 Hibernate 进行数据库操作时,...

    org.hibernate.ejb-library-3.4.0.GA

    标题中的"org.hibernate.ejb-library-3.4.0.GA"是Hibernate Entity Beans的一个特定版本,它是Hibernate框架的一部分,专门用于处理Java Enterprise Edition (EE)环境中的持久化。Hibernate是著名的对象关系映射...

    org.hibernate.jpa.QueryHints jar包

    org.hibernate.jpa.QueryHints jar hibernate-entitymanager-4.3.0.Final.jar

    解决SpringDataJPA报错:org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null w

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/...

    org.springframework.orm.hibernate4.HibernateOptimisticLockingFailureException

    标题中的"org.springframework.orm.hibernate4.HibernateOptimisticLockingFailureException"是一个具体的异常类型,源自Spring框架的Hibernate4模块。这个异常通常在乐观锁(Optimistic Locking)机制失败时抛出,...

    HIbernate4.3.6整合c3p0所需jar

    org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider] at org.hibernate.service.internal....

    启动tomcat报错org.hibernate.cache.CacheProvider

    标题中的“启动tomcat报错org.hibernate.cache.CacheProvider”指的是在尝试启动Apache Tomcat服务器时遇到了与Hibernate缓存提供者相关的错误。这通常涉及到Hibernate框架的配置或运行时环境问题。Hibernate是一个...

    org.hibernate.eclipse.feature_3.2.3.GA

    org.hibernate.eclipse.feature_3.2.3.GAorg.hibernate.eclipse.feature_3.2.3.GA

    weblogic10.3 org.hibernate.hql.ast.HqlToken报错问题

    在本问题中,我们遇到了一个特定的错误,即“weblogic10.3 org.hibernate.hql.ast.HqlToken报错”,这涉及到Hibernate ORM框架和其在WebLogic上的运行。让我们深入探讨这个问题,以及可能的解决方案。 首先,`org....

    com.springsource.org.hibernate.cache-3.3.2.GA.jar

    jar包,官方版本,自测可用

    hibernate4.0使用二级缓存jar包

    &lt;property name="hibernate.cache.region.factory_class"&gt;org.hibernate.cache.ehcache.EhCacheRegionFactory 3.3配置如下: &lt;property name="hibernate.cache.use_second_level_cache"&gt;true &lt;property name="cache...

    hibernate-tools(包含版本3、版本5)解决hbm.xml中文注释乱码和生成实体类注释

    在eclipse上通过hibernate...重启eclipse即可生效(插件包所在路径是:D:\eclipse\plugins\org.hibernate.eclipse.libs_3.6.0.Final-v20130327-1513-B111\lib\tools\hibernate-tools-3.4.0.CR2.jar(版本号可能有出入))

    HIbernate4.3.6-c3p0所需jar.rar

    Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.connection.C3P0ConnectionProvider] as strategy [org.hibernate.engine.jdbc.... ...

    maven+hibernate

    通过配置`hibernate.cfg.xml`文件,我们可以指定数据库连接参数,如URL、用户名、密码等。然后,通过定义Java实体类并使用注解或XML映射文件,我们可以创建对象与数据库表之间的映射关系。 3. **Spring**: Spring...

    hibernate annotations

    1. **配置**:首先,需要在项目中引入Hibernate库,并创建一个配置文件(通常是hibernate.cfg.xml),配置数据库连接信息。 2. **实体定义**:使用注解定义实体类及其属性,指定与数据库表的对应关系。 3. **...

    Hibernate不同数据库的连接及SQL方言

    Hibernate提供了多种SQL方言,例如org.hibernate.dialect.OracleDialect、org.hibernate.dialect.MySQLDialect、org.hibernate.dialect.SQLServerDialect等。我们可以在配置文件中使用元素来设置SQL方言,例如: ...

    hibernate.5.1.0.jar全部

    包含hibernate所有所需jar包还有一些其他包日志包、jpa支持包等: 列如:hibernate-core-5.1.0.Final.jar hibernate-ehcache-5.1.0.Final.jar hibernate-entitymanager-5.1.0.Final.jar hibernate-envers-5.1.0....

Global site tag (gtag.js) - Google Analytics