我用我的域模型的枚舉枚舉場中的目標,但我得到以下錯誤,當我試圖堅持對象到數據庫:如何堅持有
Exception in thread "main" java.lang.ClassCastException: nl.ru.cmbi.pdbeter.core.model.enums.Enum_WhifFunction cannot be cast to java.lang.String at org.hibernate.validator.NotEmptyValidator.isValid(NotEmptyValidator.java:36) at org.hibernate.validator.ClassValidator.getInvalidValues(ClassValidator.java:386) at org.hibernate.validator.ClassValidator.getInvalidValues(ClassValidator.java:352) at org.hibernate.validator.event.ValidateEventListener.validate(ValidateEventListener.java:139) at org.hibernate.validator.event.ValidateEventListener.onPreInsert(ValidateEventListener.java:172) at org.hibernate.action.EntityIdentityInsertAction.preInsert(EntityIdentityInsertAction.java:142) at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:65) at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279) at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321) at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204) at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:117) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93) at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:535) at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:527) at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:523) at nl.ru.cmbi.pdbeter.core.controller.DAO.GenericDAO.makePersistent(GenericDAO.java:73) at nl.ru.cmbi.pdbeter.core.controller.DAO.WhifFunctionDAO.getWhifFunctionSet(WhifFunctionDAO.java:36) at nl.ru.cmbi.pdbeter.core.controller.DAO.LoggedErrorWhifDAO.updateWhifFunctionSet(LoggedErrorWhifDAO.java:42) at nl.ru.cmbi.pdbeter.whifclient.controller.WhifFunctionsUpdater.executeWhifFunctionsByAccessionCode(WhifFunctionsUpdater.java:93) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:616) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) at $Proxy31.executeWhifFunctionsByAccessionCode(Unknown Source) at nl.ru.cmbi.pdbeter.whifclient.controller.WhifFunctionsExecutor.executeWhifFunctions(WhifFunctionsExecutor.java:26) at nl.ru.cmbi.pdbeter.whifclient.controller.WhifClient.updateWhifFunctions(WhifClient.java:22) at nl.ru.cmbi.pdbeter.updater.controller.UpdaterMain.start(UpdaterMain.java:65) at nl.ru.cmbi.pdbeter.updater.controller.UpdaterMain.main(UpdaterMain.java:44)
這是我的域名模型:
package nl.ru.cmbi.pdbeter.core.model.domain;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.ManyToMany;
import lombok.AccessLevel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import nl.ru.cmbi.pdbeter.core.model.enums.Enum_WhifFunction;
import org.hibernate.annotations.NaturalId;
import org.hibernate.validator.NotEmpty;
@Entity
@Data
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@EqualsAndHashCode(callSuper = false, of = { "whifFunction" })
@SuppressWarnings("PMD.UnusedPrivateField")
public class WhifFunction extends DomainObject implements Serializable {
@NaturalId
@NotEmpty
@Enumerated(EnumType.STRING)
private Enum_WhifFunction whifFunction;
@ManyToMany(mappedBy = "whifFunctionSet", cascade = CascadeType.ALL)
private Set<LoggedErrorWhif> loggedErrorWhifSet = new HashSet<LoggedErrorWhif>();
public WhifFunction(Enum_WhifFunction whifFunction) {
if (whifFunction == null) {
throw new IllegalStateException("WhifFunction is null");
} else {
this.whifFunction = whifFunction;
}
}
@Override
public String toString() {
return whifFunction.toString();
}
}
爲什麼不是@Enumerated註釋工作,因爲我認爲它應該工作?如果它按照預期的方式工作,那我該如何讓它按我現在認爲正在工作的方式工作?或換句話說:如何持久化一個枚舉字段的對象。
編輯: 感謝您的所有答案。 NotEmpty在那裏是偶然的,我沒有看到它。我不得不做很多映射,所以我複製粘貼了很多東西,但是當我從字符串更改爲枚舉時,意外忘記刪除NotEmpty。下次我會試着更仔細地看一下堆棧跟蹤,我完全錯過了NotEmptyValidator。
顯示完整的堆棧跟蹤,請。 – axtavt 2010-11-22 16:20:09
請提供一個SSCCE。順便提一下,Enum_WhifFunction不遵循Java命名約定。我建議擺脫下劃線。 – Puce 2010-11-22 16:26:30
添加了堆棧跟蹤。 – FinalArt2005 2010-11-22 16:31:09