在尝试加载带有Map的类时,我在使用Hibernate时遇到异常。
它没有,但是当我将这个字段添加到类中时,它会在标题中给出错误。
以下是类,Person,PhoneNumber,PersonServiceImpl和IPersonService。
所有这些似乎都没有Person.PhoneNumber字段。
有什么想法吗?
我的测试用例运行良好:
for (int i = 0; i < getIterations(); i++) {
entityManager.getTransaction().begin();
Person person = getPersonForIteration(i);
entityManager.persist(person);
entityManager.getTransaction().commit();
Person foundPerson = entityManager.find(Person.class,
person.getId());
doAssertsForFoundPerson(foundPerson, person);
}
实体和服务类:
@Entity
@Table(name = "ent_person")
public class Person implements Serializable, Comparable<Person> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
/**
* Comment for <code>serialVersionUID</code>
*/
private static final long serialVersionUID = -4680156785318108346L;
protected String firstName;
protected String nickname;
protected String lastName;
@ElementCollection(fetch = FetchType.EAGER)
protected List<String> middleNames;
protected String idNum;
protected char isMale;
@Temporal(value = TemporalType.DATE)
protected Date birthday;
@ElementCollection(fetch=FetchType.EAGER)
@MapKeyColumn(name = "name")
@Column(name = "value")
protected Map<String, PhoneNumber> phoneNumbers;
public Person() {
super();
}
/**
* @param firstName
* @param nickname
* @param lastName
* @param middleNames
* @param idNum
* @param isMale
* @param birthday
*/
public Person(String firstName, String nickname, String lastName, List<String> middleNames,
String idNum, char isMale, Date birthday) {
super();
this.firstName = firstName;
this.nickname = nickname;
this.lastName = lastName;
this.middleNames = middleNames;
this.idNum = idNum;
this.isMale = isMale;
this.birthday = birthday;
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public void setPhoneNumber(String numberType, PhoneNumber number) {
if (phoneNumbers == null) {
phoneNumbers = new HashMap<String, PhoneNumber>();
}
phoneNumbers.put(numberType, number);
}
public Map<String, PhoneNumber> getPhoneNumbers() {
return phoneNumbers;
}
/**
* @return Returns the firstName.
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName The firstName to set.
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return Returns the nickname.
*/
public String getNickname() {
return nickname;
}
/**
* @param nickname The nickname to set.
*/
public void setNickname(String nickname) {
this.nickname = nickname;
}
/**
* @return Returns the lastName.
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName The lastName to set.
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void addMiddleName(String middleName) {
if (middleNames == null) {
middleNames = new ArrayList<String>();
}
if (!middleNames.contains(middleName)) {
middleNames.add(middleName);
}
}
/**
* @return Returns the middleNames.
*/
public List<String> getMiddleNames() {
return middleNames;
}
/**
* @param middleNames The middleNames to set.
*/
public void setMiddleNames(List<String> middleNames) {
this.middleNames = middleNames;
}
/**
* @return Returns the idNum.
*/
public String getIdNum() {
return idNum;
}
/**
* @param idNum The idNum to set.
*/
public void setIdNum(String idNum) {
this.idNum = idNum;
}
/**
* @return Returns the isMale.
*/
public char getIsMale() {
return isMale;
}
/**
* @param isMale The isMale to set.
*/
public void setIsMale(char isMale) {
this.isMale = isMale;
}
@JsonIgnore
public boolean isMale() {
return isMale == 'y';
}
/**
* @return Returns the birthday.
*/
public Date getBirthday() {
return birthday;
}
/**
* @param birthday The birthday to set.
*/
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
/**
* @return
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((birthday == null) ? 0 : birthday.hashCode());
result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
result = prime * result + ((idNum == null) ? 0 : idNum.hashCode());
result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
result = prime * result + ((middleNames == null) ? 0 : middleNames.hashCode());
result = prime * result + ((nickname == null) ? 0 : nickname.hashCode());
return result;
}
/**
* @param o
* @return
*
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
public int compareTo(Person o) {
int result = lastName.compareTo(o.getLastName());
return result == 0 ? firstName.compareTo(o.getLastName()) : result;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Person other = (Person) obj;
if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
return false;
}
if ((this.firstName == null) ? (other.firstName != null) : !this.firstName.equals(other.firstName)) {
return false;
}
if ((this.nickname == null) ? (other.nickname != null) : !this.nickname.equals(other.nickname)) {
return false;
}
if ((this.lastName == null) ? (other.lastName != null) : !this.lastName.equals(other.lastName)) {
return false;
}
if (this.middleNames != other.middleNames && (this.middleNames == null || !this.middleNames.equals(other.middleNames))) {
return false;
}
if ((this.idNum == null) ? (other.idNum != null) : !this.idNum.equals(other.idNum)) {
return false;
}
if (this.birthday != other.birthday && (this.birthday == null || !this.birthday.equals(other.birthday))) {
return false;
}
return true;
}
@Override
public String toString() {
return "Person{" + "id=" + id + ", firstName=" + firstName + ", nickname=" + nickname + ", lastName=" + lastName + ", middleNames=" + middleNames + ", idNum=" + idNum + ", isMale=" + isMale + ", birthday=" + birthday + ", phoneNumbers=" + phoneNumbers + '}';
}
}
public class PhoneNumber implements Serializable {
private static final long serialVersionUID = -423634682785318106L;
public static transient final String HOME = "Home";
public static final String PERSONAL_MOBILE = "Personal Mobile";
public static final String OFFICE = "Office";
public static final String WORK_MOBILE = "Work Mobile";
public static final String FAX = "Fax";
public static final String PAGER = "Pager";
public static final String TOLL_FREE = "Toll Free";
public static final String OTHER = "Other";
String countryCode;
String areaCode;
String subscriberNubmer;
String extension;
public PhoneNumber() {
super();
}
/**
* @param countryCode
* @param areaCode
* @param subscriberNubmer
* @param extension
*/
public PhoneNumber(String countryCode, String areaCode, String subscriberNubmer,
String extension) {
super();
this.countryCode = countryCode;
this.areaCode = areaCode;
this.subscriberNubmer = subscriberNubmer;
this.extension = extension;
}
/**
* @return Returns the countryCode.
*/
public String getCountryCode() {
return countryCode;
}
/**
* @param countryCode The countryCode to set.
*/
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
/**
* @return Returns the areaCode.
*/
public String getAreaCode() {
return areaCode;
}
/**
* @param areaCode The areaCode to set.
*/
public void setAreaCode(String areaCode) {
this.areaCode = areaCode;
}
/**
* @return Returns the subscriberNubmer.
*/
public String getSubscriberNubmer() {
return subscriberNubmer;
}
/**
* @param subscriberNubmer The subscriberNubmer to set.
*/
public void setSubscriberNubmer(String subscriberNubmer) {
this.subscriberNubmer = subscriberNubmer;
}
/**
* @return Returns the extension.
*/
public String getExtension() {
return extension;
}
/**
* @param extension The extension to set.
*/
public void setExtension(String extension) {
this.extension = extension;
}
/**
* @return
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "PhoneNumber [countryCode=" + countryCode + ", areaCode=" + areaCode
+ ", subscriberNubmer=" + subscriberNubmer + ", extension=" + extension + "]";
}
/**
* @return
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((areaCode == null) ? 0 : areaCode.hashCode());
result = prime * result + ((countryCode == null) ? 0 : countryCode.hashCode());
result = prime * result + ((extension == null) ? 0 : extension.hashCode());
result = prime * result + ((subscriberNubmer == null) ? 0 : subscriberNubmer.hashCode());
return result;
}
/**
* @param obj
* @return
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
PhoneNumber other = (PhoneNumber) obj;
if (areaCode == null) {
if (other.areaCode != null) {
return false;
}
} else if (!areaCode.equals(other.areaCode)) {
return false;
}
if (countryCode == null) {
if (other.countryCode != null) {
return false;
}
} else if (!countryCode.equals(other.countryCode)) {
return false;
}
if (extension == null) {
if (other.extension != null) {
return false;
}
} else if (!extension.equals(other.extension)) {
return false;
}
if (subscriberNubmer == null) {
if (other.subscriberNubmer != null) {
return false;
}
} else if (!subscriberNubmer.equals(other.subscriberNubmer)) {
return false;
}
return true;
}
}
@Remote
@Local
public interface IPersonService {
public Collection<Person> getAllPeople();
Person getPerson(String idNum);
public void removePerson(Person p);
}
@Stateless(name = "personService")
public class PersonServiceImpl implements IPersonService {
@PersistenceContext
EntityManager em;
public Collection<Person> getAllPeople(){
Query query = em.createQuery("SELECT p FROM Person p");
return (Collection<Person>)query.getResultList();
}
public Person getPerson(String idNum) {
Person p = new Person();
p.setFirstName("John");
p.setLastName("Doe");
p.setIdNum(idNum);
p.setPhoneNumber(PhoneNumber.HOME, new PhoneNumber());
em.persist(p);
return p;
}
public void removePerson(Person p) {
em.remove(p);
}
}
这也是堆栈跟踪:
Warning: StandardWrapperValve[net.somepackage.demo.web.servlet.BasicServlet]: Servlet.service() for servlet net.somepackage.demo.web.servlet.BasicServlet threw exception
javax.ejb.EJBException
........
Caused by: java.lang.IllegalStateException: This web container has not yet been started
at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1674)
at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1633)
at sun.misc.Unsafe.defineClass(Native Method)
at sun.reflect.ClassDefiner.defineClass(ClassDefiner.java:63)
at sun.reflect.MethodAccessorGenerator$1.run(MethodAccessorGenerator.java:399)
at sun.reflect.MethodAccessorGenerator$1.run(MethodAccessorGenerator.java:394)
at java.security.AccessController.doPrivileged(Native Method)
at sun.reflect.MethodAccessorGenerator.generate(MethodAccessorGenerator.java:393)
at sun.reflect.MethodAccessorGenerator.generateSerializationConstructor(MethodAccessorGenerator.java:112)
at sun.reflect.ReflectionFactory.newConstructorForSerialization(ReflectionFactory.java:340)
at java.io.ObjectStreamClass.getSerializableConstructor(ObjectStreamClass.java:1376)
at java.io.ObjectStreamClass.access$1500(ObjectStreamClass.java:72)
at java.io.ObjectStreamClass$2.run(ObjectStreamClass.java:493)
at java.io.ObjectStreamClass$2.run(ObjectStreamClass.java:468)
at java.security.AccessController.doPrivileged(Native Method)
at java.io.ObjectStreamClass.<init>(ObjectStreamClass.java:468)
at java.io.ObjectStreamClass.lookup(ObjectStreamClass.java:365)
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:602)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1623)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1518)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1774)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
at org.hibernate.internal.util.SerializationHelper.doDeserialize(SerializationHelper.java:225)
at org.hibernate.internal.util.SerializationHelper.deserialize(SerializationHelper.java:287)
at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.fromBytes(SerializableTypeDescriptor.java:138)
at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:113)
at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:27)
at org.hibernate.type.descriptor.sql.VarbinaryTypeDescriptor$2.doExtract(VarbinaryTypeDescriptor.java:53)
at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:241)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:237)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:227)
at org.hibernate.persister.collection.AbstractCollectionPersister.readElement(AbstractCollectionPersister.java:833)
at org.hibernate.collection.internal.PersistentMap.readFrom(PersistentMap.java:263)
at org.hibernate.loader.plan.exec.process.internal.CollectionReferenceInitializerImpl.finishUpRow(CollectionReferenceInitializerImpl.java:77)
at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.readRow(AbstractRowReader.java:108)
at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:112)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:121)
at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:85)
at org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:82)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:682)
at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:75)
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1922)
at org.hibernate.collection.internal.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:709)
at org.hibernate.engine.internal.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:885)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)
at org.hibernate.loader.Loader.doList(Loader.java:2611)
at org.hibernate.loader.Loader.doList(Loader.java:2594)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2423)
at org.hibernate.loader.Loader.list(Loader.java:2418)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:501)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:371)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:220)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1268)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:87)
at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:567)
at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:436)
at net.somepackage.demo.service.PersonServiceImpl.getAllPeople(PersonServiceImpl.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4786)
at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:656)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608)
at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:46)
at org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:140)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369)
at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4758)
at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4746)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)
... 33 more
答案 0 :(得分:2)
据我所知,类加载存在问题。我不确定它是什么,但当我降级到Hibernate 4.3.11时。最终的东西开始正常工作。
我不确定为什么,并且厌倦了想弄明白。一旦我切换了Hibernate的版本,一切都运转了。