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
}