如何修复jpa中需要正确实体id的java.lang.IllegalArgumentException

时间:2016-03-01 12:47:03

标签: java hibernate jpa

我有这个例外:

<div id="pentagon"></div>

当我想删除StaffRegiAddressInfo对象时, 我在这个实体中使用了manyToOne映射。

以下是代码流程:

查看代码:

  java.lang.IllegalArgumentException: Provided id of the wrong type for   class com.netizenbd.domain.staff.StaffRegiAddressInfo. Expected: class java.lang.Integer, got class com.netizenbd.domain.staff.StaffRegiAddressInfo

这是ManageBean staffUpdateMB removeAddress();方法:

    <p:dataGrid value="#{staffUpdateMB.addressInfoList}"
        id="addesList" var="address" columns="1" layout="grid"
        styleClass="NoPadding NoIndent">
        <p:panel>
            <div
                class="Container100 Responsive100 TealGreenBack BordRad5 White NoIndent">

                <div class="Container50 Responsive100 NoIndent">
                    <h:outputText value="#{address.addressType}" styleClass="Fs22" />
                </div>
                <div class="Container50 Responsive100 TexAlRight NoIndent">
                    <p:commandButton class="TealGreenBack" icon="fa fa-edit"
                    onstart="PF('staffAddressEditDialog').show()">
                    <f:setPropertyActionListener value="#{address}"
                            target="#{staffUpdateMB.beanAddressInfo}"/>
                    </p:commandButton>
                    <p:commandButton styleClass="RedButton RaisedButton"
                        action="#{staffUpdateMB.removeAddress}" icon="fa fa-trash-o"
                        update="addesList" ajax="false">
                        <f:setPropertyActionListener value="#{address}"
                            target="#{staffUpdateMB.beanAddressInfo}" />
                    </p:commandButton>
                </div>
            </div>
    ... .... ....
    </p:panel>
    </p:dataGrid>

这是扩展EntityDao的addressInfoDao:

    public void removeAddress() {
    try {

        System.out.println("Address ID :"+this.beanAddressInfo.getAddressID());
        StaffRegiAddressInfo address=addressInfoDao.findById(this.beanAddressInfo.getAddressID());
        System.out.println("Address ID :"+address.getAddressID());
        addressInfoDao.remove(address);


        context.addMessage(null, new FacesMessage(msg.getPropValue("deleteSuccess")));
    } catch (Exception e) {
        context.addMessage(null, new FacesMessage(msg.getPropValue("deleteError")));
        logger.error("This is error : " + e);
        logger.fatal("This is fatal : " + e);
    }
}

这是实现EntityDao:

    public interface StaffRegiAddressInfoDao extends EntityDao<StaffRegiAddressInfo>{

    }

    public interface EntityDao<E> {

void persist(E e) throws Exception;

void merge(E e) throws Exception;

void remove(Object id) throws Exception;
    }

以下为例外情况:

    public class EntityService<E>  implements EntityDao<E> {

@PersistenceContext(unitName="persistenceUnit")
protected EntityManager entityManager;

protected E instance;
private Class<E> entityClass;


@Transactional
public void persist(E e) throws HibernateException{     
    getEntityManager().persist(e);
}
@Transactional
public void merge(E e) throws HibernateException{     
    getEntityManager().merge(e);
}
@Transactional
public void remove(Object id) throws Exception{     
    getEntityManager().remove((E)getEntityManager().find(getEntityClass(), id));
    getEntityManager().flush();
}

此处还有实体:

    Address ID :33
    Address ID :33
    01/Mar/2016 18:14:05,440- StaffUpdateMB: This is error :      java.lang.IllegalArgumentException: Provided id of the wrong type for class com.netizenbd.domain.staff.StaffRegiAddressInfo. Expected: class java.lang.Integer, got class com.netizenbd.domain.staff.StaffRegiAddressInfo
    01/Mar/2016 18:44:57,091- StaffUpdateMB: This is fatal : java.lang.IllegalArgumentException: Provided id of the wrong type for class com.netizenbd.domain.staff.StaffRegiAddressInfo. Expected: class java.lang.Integer, got class com.netizenbd.domain.staff.StaffRegiAddressInfo

1 个答案:

答案 0 :(得分:2)

尝试更改ManageBean staffUpdateMB removeAddress();方法:

  

addressInfoDao.remove(地址);

通过

  

addressInfoDao.remove(address.getAddressID());

在你的DAO中

你有:

  

public void remove(Object id);

但是你传递的是一个对象,而不是ID