使用XML与hibernate在基于一表联合主键时出现expecting IdClass mapping情况

以表user2为例:

首先在出现此错误(java.lang.IllegalArgumentException: expecting IdClass mapping)时

将user2.hbm.xml中的mapped="true" class="com.llr.po.User2PK"去掉

<composite-id mapped="true" class="com.llr.po.User2PK">
    <key-property name="key1">
        <column name="key1" sql-type="int(11)"/>
    </key-property>
    <key-property name="key2">
        <column name="key2" sql-type="int(11)"/>
    </key-property>
</composite-id>
变成

<composite-id >
    <key-property name="key1">
        <column name="key1" sql-type="int(11)"/>
    </key-property>
    <key-property name="key2">
        <column name="key2" sql-type="int(11)"/>
    </key-property>
</composite-id>

将user2 序列化(implement3 serializable)

此时运行即可成功

完整代码:

user2:

public class User2 implements Serializable{

    private int key1;
    private int key2;
    private String name;



    public int getKey1() {
        return key1;
    }

    public void setKey1(int key1) {
        this.key1 = key1;
    }

    public int getKey2() {
        return key2;
    }

    public void setKey2(int key2) {
        this.key2 = key2;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        User2 user2 = (User2) o;

        if (key1 != user2.key1) return false;
        if (key2 != user2.key2) return false;
        if (name != null ? !name.equals(user2.name) : user2.name != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = key1;
        result = 31 * result + key2;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        return result;
    }
}
 
 
 
 
User2DAOImpl:

package com.llr.dao;

import com.llr.po.User2;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import java.io.Serializable;


/**
 * Created by Administrator on 2018/3/27.
 */
public class User2DAOImpl {   //联合主键时 将user2 序列化(implement3 serializable)将user2.hbm.xml中composite-id 去掉class等其他属性
    private Configuration configuration=null;
    private SessionFactory sessionFactory=null;
    public User2DAOImpl(){
        configuration=new Configuration().configure("hibernate.cfg.xml");
        sessionFactory=configuration.buildSessionFactory();
    }
    @Test
    public void add(){
        Session session=sessionFactory.openSession();
        Transaction transaction=session.beginTransaction();
        User2 user2=new User2();
        user2.setKey1(1);
        user2.setKey2(2);
        user2.setName("tom");
        try{
            session.save(user2);
            transaction.commit();
        }catch (Exception e){
            transaction.rollback();
            e.printStackTrace();
        }
        session.close();
    }
}



猜你喜欢

转载自blog.csdn.net/serialization_rong/article/details/79714558