使用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();
    }
}



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值