1. 程式人生 > >Duplicate entry '30-25' for key 'PRIMARY'

Duplicate entry '30-25' for key 'PRIMARY'

在執行hibernate多對多關係時一直報下圖的錯誤
在這裡插入圖片描述

程式碼

實體類

public class SysUser {
    private long userId;
    private String userCode;
    private String userName;
    private String userPassword;
    private String userState;

    private Set<SysRole> roles=new HashSet<>();

    public Set<SysRole> getRoles() {
        return roles;
    }
    ……
public class SysRole {
    private long roleId;
    private String roleName;
    private String roleMemo;

    private Set<SysUser> users=new HashSet<>();

    public Set<SysUser> getUsers() {
        return users;
    }
    ……

實體類對映檔案

<hibernate-mapping>

    <class name="com.hibernate.domain.SysUser" table="sys_user" schema="yy-visualization">
        <id name="userId" column="user_id">
            <generator class="native"></generator>
        </id>
        <property name="userCode" column="user_code"/>
        <property name="userName" column="user_name"/>
        <property name="userPassword" column="user_password"/>
        <property name="userState" column="user_state"/>

        <!--
        name:關聯的另一方的集合屬性名稱
        table:中間表的名稱
        -->
        <set name="roles" table="sys_user_role">
            <!--column:當前物件在中件表中的外來鍵名稱-->
            <key column="user_id"></key>
            <!--
            class:關聯另一方的類全路徑
            column:關聯另一方在中件表的外來鍵名稱
            -->
            <many-to-many class="com.hibernate.domain.SysRole"
                          column="role_id"></many-to-many>
        </set>
    </class>
</hibernate-mapping>
<hibernate-mapping>

    <class name="com.hibernate.domain.SysRole" table="sys_role" schema="yy-visualization">
        <id name="roleId" column="role_id">
            <generator class="identity"></generator>
        </id>
        <property name="roleName" column="role_name"/>
        <property name="roleMemo" column="role_memo"/>

        <set name="users" table="sys_user_role">
            <key column="role_id"></key>
            <many-to-many column="user_id"
                          class="com.hibernate.domain.SysUser"></many-to-many>
        </set>
    </class>
</hibernate-mapping>

測試類

@Test
    public void demo03(){
        Configuration configuration = new Configuration().configure();
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        Session session = sessionFactory.openSession();

        Transaction tx=session.beginTransaction();
        SysUser u1=new SysUser();
        u1.setUserCode("1231323213");
        u1.setUserPassword("123");
        u1.setUserState("1");
        u1.setUserName("敖哈哈");
        SysUser u2=new SysUser();
        u2.setUserCode("435454");
        u2.setUserName("袁嘻嘻");
        u2.setUserPassword("123");
        u2.setUserState("1");

        SysRole r1=new SysRole();
        r1.setRoleName("男朋友");
        SysRole r2=new SysRole();
        r2.setRoleName("女朋友");
        SysRole r3=new SysRole();
        r3.setRoleName("室友");

        u1.getRoles().add(r1);
        u1.getRoles().add(r3);
        u2.getRoles().add(r2);
        u2.getRoles().add(r3);

        r1.getUsers().add(u1);
        r2.getUsers().add(u2);
        r3.getUsers().add(u1);
        r3.getUsers().add(u2);
        session.save(u1);
        session.save(u2);
        session.save(r1);
        session.save(r2);
        session.save(r3);
        tx.commit();
        session.close();
    }

錯誤原因猜測:

hibernate在save的時候會產生多餘的sql語句,可能多餘的sql語句導致資料表中主鍵重複

解決方法

在多對多關係中,找到一方放棄關係維護,我這裡是讓role放棄
在user的對映檔案中的set標籤,新增inverse屬性

 <set name="roles" table="sys_user_role" inverse="true">
            <!--column:當前物件在中件表中的外來鍵名稱-->
            <key column="user_id"></key>
            <!--
            class:關聯另一方的類全路徑
            column:關聯另一方在中件表的外來鍵名稱
            -->
            <many-to-many class="com.hibernate.domain.SysRole"
                          column="role_id"></many-to-many>
        </set>