简体   繁体   English

使用Hibernate模板插入数据

[英]Inserting data using Hibernate Template

Consider the following: 考虑以下:

User.java User.java

class User{
    ...
    private Set<Community> communities = new HashSet<Community>();
    ...
}

Community.java 社区.java

class Community{
    ...
    private Set<User> users = new HashSet<User>();
    ...
}

User.hbm.xml User.hbm.xml

<set name="communities" cascade="save-update" lazy="false" table="tbl_user_community">
        <key column="user_id" />
        <many-to-many class="Community" column="community_id"/>
     </set> 

Community.hbm.xml Community.hbm.xml

<set name="users" lazy="false" cascade="save-update" table="tbl_user_community" inverse="true">
    <key column="community_id" />
    <many-to-many class="User" column="user_id"/>
</set>

both has many-to-many relationship. 两者都有many-to-many关系。

Code for adding community to user: 用于向用户添加社区的代码:

HibernateTemplate template = null;
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
template = new HibernateTemplate(sessionFactory);

User user = template.get(User.class, "100");
Community community = template.get(Community.class,1);



user.getCommunities().add(community);


template.saveOrUpdate(user);

Scenario: 场景:

  1. community1 is assigned to user1 (inserted into database) 将community1分配给user1(插入数据库中)
  2. community2 is assigned to user1 (inserted into database) 将community2分配给user1(插入数据库中)
  3. community1 is assigned to user2 (inserted into database) 将community1分配给user2(插入数据库中)
  4. community2 is assigned to user2 (not inserted into database, throws NonUniqueObjectException ) 将community2分配给user2(未插入到数据库中,抛出NonUniqueObjectException
  5. if i use template.merge(user) instead of template.saveOrUpdate(user) .. it works .. why??? 如果我使用template.merge(user)而不是template.saveOrUpdate(user) ..它起作用..为什么?

saveOrUpdate() and update() take a detached object and attach this object instance to the session. saveOrUpdate()update()采用一个分离的对象,并将该对象实例附加到会话。 So if an entity with the same type and ID is already attached to the session, Hibernate can't attach another instance: it would break the guarantee that only one entity with a given ID exists in a session. 因此,如果会话中已经连接了具有相同类型和ID的实体,则Hibernate无法附加另一个实例:这将无法保证会话中仅存在一个具有给定ID的实体。 So it throws this exception. 因此,它将引发此异常。

merge() is different. merge()是不同的。 It takes an detached entity, loads the entity with the same ID in the session if not already loaded, and copies the state (ie the fields) of the detached entity to the attached one. 它需要一个分离的实体,如果尚未加载,则在会话中加载具有相同ID的实体,然后将分离的实体的状态(即字段)复制到附加的实体中。 The detached entity stays deteached and unmodified. 独立实体仍保持教学状态不变。

So, in general, merge() should be preferred. 因此,通常应首选merge() BTW, the other methods don't exist in the JPA API. 顺便说一句,JPA API中不存在其他方法。 If you use saveOrUpdate(), you should generally call it as the first instruction of the transaction, to avoid being in the situation causing the NonUniqueException. 如果使用saveOrUpdate(),通常应将其作为事务的第一条指令进行调用,以避免出现导致NonUniqueException的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM