gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:02:02 25 4
gpt4 key购买 nike

我遇到了这个异常:

  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

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

以下是代码流程:

查看代码:

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

这里是 ManageBean StaffUpdateMB removeAddress();方法:

    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的addressInfoDao:

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

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

下面是异常(exception)情况:

    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

这里还有实体:

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

最佳答案

尝试在 ManageBean StaffUpdateMB removeAddress() 中进行更改;方法:

addressInfoDao.remove(address);

addressInfoDao.remove(address.getAddressID());

在你的 DAO 中你有:

public void remove(Object id);

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

关于java - 如何修复在 jpa 中需要正确实体 id 的 java.lang.IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35723369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com