org.hibernate.ObjectNotFoundException: No row with the given identifier exists
load方法可能会抛出懒加载异常
1
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
注意:在session缓存中不能够有两个相同OID的对象,否则会报异常
1 2 3 4 5 6 7 8 9 10
publicstaticvoidtestOid(Session session){ User user = (User) session.get(User.class,1); System.out.println(user); User user1 = new User(); user1.setId(1); user1.setName("王五"); session.saveOrUpdate(user1); }
org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session
publicinterfaceSessionextendsSharedSessionContract{ /** * Obtain a {@link Session} builder with the ability to grab certain information from this session. * * @return The session builder */ public SharedSessionBuilder sessionWithOptions();
/** * Force this session to flush. Must be called at the end of a * unit of work, before committing the transaction and closing the * session (depending on {@link #setFlushMode(FlushMode)}, * {@link Transaction#commit()} calls this method). * <p/> * <i>Flushing</i> is the process of synchronizing the underlying persistent * store with persistable state held in memory. * * @throws HibernateException Indicates problems flushing the session or * talking to the database. */ publicvoidflush()throws HibernateException;
// 获取创建该会话的sessionFactory public SessionFactory getSessionFactory();
// 关闭数据库连接 public Connection close()throws HibernateException;
// 返回与当前实体关联的会话标识符 public Serializable getIdentifier(Object object);
/** * Check if this instance is associated with this <tt>Session</tt>. * * @param object an instance of a persistent class * @return true if the given instance is associated with this <tt>Session</tt> */ publicbooleancontains(Object object);
/** * Remove this instance from the session cache. Changes to the instance will * not be synchronized with the database. This operation cascades to associated * instances if the association is mapped with <tt>cascade="evict"</tt>. * * @param object The entity to evict * * @throws NullPointerException if the passed object is {@code null} * @throws IllegalArgumentException if the passed object is not defined as an entity */ publicvoidevict(Object object);
/** * Return the persistent instance of the given entity class with the given identifier, * assuming that the instance exists. This method might return a proxied instance that * is initialized on-demand, when a non-identifier method is accessed. * <br><br> * You should not use this method to determine if an instance exists (use <tt>get()</tt> * instead). Use this only to retrieve an instance that you assume exists, where non-existence * would be an actual error. * * @param theClass a persistent class * @param id a valid identifier of an existing persistent instance of the class * * @return the persistent instance or proxy */ public Object load(Class theClass, Serializable id);
/** * Persist the state of the given detached instance, reusing the current * identifier value. This operation cascades to associated instances if * the association is mapped with {@code cascade="replicate"} * * @param object a detached instance of a persistent class * @param replicationMode The replication mode to use */ publicvoidreplicate(Object object, ReplicationMode replicationMode);
/** * Persist the state of the given detached instance, reusing the current * identifier value. This operation cascades to associated instances if * the association is mapped with {@code cascade="replicate"} * * @param entityName The entity name * @param object a detached instance of a persistent class * @param replicationMode The replication mode to use */ publicvoidreplicate(String entityName, Object object, ReplicationMode replicationMode);
// 保存对象,生成标识,变为持久化状态 public Serializable save(Object object);
// 保存对象,生成标识,变为持久化状态 public Serializable save(String entityName, Object object);
/** * Copy the state of the given object onto the persistent object with the same * identifier. If there is no persistent instance currently associated with * the session, it will be loaded. Return the persistent instance. If the * given instance is unsaved, save a copy of and return it as a newly persistent * instance. The given instance does not become associated with the session. * This operation cascades to associated instances if the association is mapped * with {@code cascade="merge"} * <p/> * The semantics of this method are defined by JSR-220. * * @param object a detached instance with state to be copied * * @return an updated persistent instance */ public Object merge(Object object);
/** * Make a transient instance persistent. This operation cascades to associated * instances if the association is mapped with {@code cascade="persist"} * <p/> * The semantics of this method are defined by JSR-220. * * @param object a transient instance to be made persistent */ publicvoidpersist(Object object);
/** * Re-read the state of the given instance from the underlying database. It is * inadvisable to use this to implement long-running sessions that span many * business tasks. This method is, however, useful in certain special circumstances. * For example * <ul> * <li>where a database trigger alters the object state upon insert or update * <li>after executing direct SQL (eg. a mass update) in the same session * <li>after inserting a <tt>Blob</tt> or <tt>Clob</tt> * </ul> * * @param entityName a persistent class * @param object a persistent or detached instance */ publicvoidrefresh(String entityName, Object object);
// 为给定集合和查询条件创建查询实例 public Query createFilter(Object collection, String queryString);
// 清除该会话 publicvoidclear();
// 返回给定命名和标识符的持久化对象实例 public Object get(Class clazz, Serializable id);
// 返回给定命名和标识符的持久化对象实例 public Object get(String entityName, Serializable id);
/** * Return the entity name for a persistent entity. * * @param object a persistent entity * * @return the entity name */ public String getEntityName(Object object);
/** * Create an {@link IdentifierLoadAccess} instance to retrieve the specified entity by * primary key. * * @param entityClass The entity type to be retrieved * * @return load delegate for loading the specified entity type by primary key * * @throws HibernateException If the specified Class cannot be resolved as a mapped entity */ public IdentifierLoadAccess byId(Class entityClass); /** * Get the statistics for this session. * * @return The session statistics being collected for this session */ public SessionStatistics getStatistics();
/** * Controller for allowing users to perform JDBC related work using the Connection managed by this Session. * * @param work The work to be performed. * @throws HibernateException Generally indicates wrapped {@link java.sql.SQLException} */ publicvoiddoWork(Work work)throws HibernateException;
/** * Controller for allowing users to perform JDBC related work using the Connection managed by this Session. After * execution returns the result of the {@link ReturningWork#execute} call. * * @param work The work to be performed. * @param <T> The type of the result returned from the work * * @return the result from calling {@link ReturningWork#execute}. * * @throws HibernateException Generally indicates wrapped {@link java.sql.SQLException} */ public <T> T doReturningWork(ReturningWork<T> work)throws HibernateException;
/** * Add one or more listeners to the Session * * @param listeners The listener(s) to add */ publicvoidaddEventListeners(SessionEventListener... listeners); }