1

I have got this exception :

  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

when I want to delete StaffRegiAddressInfo object, I have used manyToOne mapping in this Entity.

Here are code flow:

view code:

    <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>

Here is ManageBean staffUpdateMB removeAddress(); method:

    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);
    }
}

Here is addressInfoDao which extend 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;
    }

Here is the implementation EntityDao:

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

And Here is the Exception below:

    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

And Here also Entities:

    @Entity
    @Table(name="staffregi_addressinfo")
    public class StaffRegiAddressInfo implements Serializable{


private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="addressID")
private int addressID;

@Column(name="addressType")
private String addressType;

@Column(name="village")
private String village;

@Column(name="postOffice")
private String postOffice;

@Column(name="postalCode")
private String postalCode;

@Column(name="thanaName")
private String thanaName;

@Column(name="districtName")
private String districtName;

@Column(name="divisionName")
private String divisionName;

@Column(name="countryName")
private String countryName;

@Column(name="instituteID")
private String instituteID;

@Column(name="recordNote")
private String recordNote;

@Column(name="userExecuted")
private String userExecuted;

@Column(name="dateExecuted")
@Temporal(TemporalType.TIMESTAMP)
private Date dateExecuted;

@Column(name="ipExecuted")
private String ipExecuted;

@Column(name="recordStatus")
private int recordStatus;


@ManyToOne
@JoinColumn(name="staffID")
private StaffRegiBasicInfo basicInfoAddress;
    //setter, getter also...
   }

   @Entity
   @Table(name="staffregi_basicinfo")
   public class StaffRegiBasicInfo implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Id
@Column(name="staffID")
private String staffID;

@Column(name="staffName")
private String staffName;

@OneToMany(cascade=CascadeType.ALL, mappedBy="basicInfoAddress")
@LazyCollection(LazyCollectionOption.FALSE)
private Set<StaffRegiAddressInfo> addressInfoList;
//setter, getter also
}

1 Answer 1

2

try to change in ManageBean staffUpdateMB removeAddress(); method:

addressInfoDao.remove(address);

by

addressInfoDao.remove(address.getAddressID());

in your DAO you have:

public void remove(Object id);

but you are passing an object, not ID

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, But when I use this addressInfoDao.remove(address.getAddressID()); then same exception occured
@AbuBakarSiddik so you mean that you set address.getAddressID() in your remove method of your addressInfoDao and you get the exception that this method expects an int class argument but a StaffRegiAddressInfo is provided instead? This is very strange since from your code addressID is integer. Can you check again please?
@elefasGR thank you, When I have changed 'addressID' 'int' to 'Integer' then no exception show. But data are not delete from DB
So you solved one problem, and now you have another one. please either close this question and open a second one. or change the question to describe the new problem
@AbuBakarSiddik with no exceptions? can you check (via debug) if (E)getEntityManager().find(getEntityClass(), id) in EntityService.remove() returns you the entity you really want to delete? If there is an exception thrown please post it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.