我遇到了一个问题,即使用Hibernate重构数据库中的对象。 @Embedded
组件在为类'hashCode()
实现访问时会抛出NullPointerException。
重建正试图这样做:
@Override
public Collection<Web> getWebs() {
Collection<?> dbWebs =
this.sessionFactory.getCurrentSession()
.createQuery("from Web")
.list();
Set<Web> webs = new HashSet<Web>();
for (Object web : dbWebs) {
webs.add((Web) web);
}
return webs;
}
在.list()
失败。
更新:我将失败点隔离到将Shipment属性的获取策略设置为EAGER
。如果它是LAZY
所有项目都来自数据库,但我需要它EAGER
才能与应用程序的其他部分一起使用。进一步的想法?
父类:
@Entity
@Table(name = "web")
public class Web {
@Id
@GeneratedValue
private Long id;
@Embedded
private WebNumber number;
@Embedded
private IcnRange icnRange;
@ManyToOne(fetch = FetchType.EAGER) //Cannot make this EAGER
@JoinColumn(name = "shipment_id")
private Shipment shipment
...
@Override
public int hashCode() {
int hashcode = 0;
hashcode += this.getIcnRange().hashCode();
hashcode += this.getPublicId().hashCode();
return hashcode;
}
}
IcnRange:
@Embeddable
public class IcnRange implements Comparable<IcnRange>, Serializable {
@Embedded
@AttributeOverride(
name = "number",
column = @Column(
name = "first_icn",
length = ICN.FIELD_LENGTH,
nullable = false))
private ICN firstIcn;
@Embedded
@AttributeOverride(
name = "number",
column = @Column(
name = "last_icn",
length = ICN.FIELD_LENGTH,
nullable = false))
private ICN lastIcn;
...
public IcnRange(final IcnRange icnRange) {
this.firstIcn = new ICN(icnRange.getFirstIcn());
this.lastIcn = new ICN(icnRange.getLastIcn());
}
}
ICN:
@Embeddable
public class ICN implements Comparable<ICN>, Serializable {
@Column(nullable = false, length = FIELD_LENGTH)
private String number;
...
}
WebNumber:
@Embeddable
final class WebNumber implements Serializable {
@Enumerated(EnumType.STRING)
@Column (name = "stock_type", nullable = false, length = StockType.FIELD_LENGTH)
private StockType stockType;
@Embedded
@AttributeOverride (
name = "number",
column =
@Column (
name = "pallet",
nullable = false,
length = Pallet.FIELD_LENGTH))
private Pallet pallet;
@Enumerated(EnumType.STRING)
@Column (name = "pallet_id", nullable = false, length = PalletId.FIELD_LENGTH)
private PalletId palletId;
}
特定攻击行位于复制构造函数中:
public IcnRange(final IcnRange icnRange) {
this.firstIcn = new ICN(icnRange.getFirstIcn());
this.lastIcn = new ICN(icnRange.getLastIcn());
}
在调用icnRange.getFirstIcn()
时会窒息。进一步调查显示,icnRange
以null
的形式输入方法,因此由于某种原因,组件未被重构。对DB的调查显示,没有一个字段为空。我包括WebNumber就像我在hashCode()
中反转Web
方法一样,它仍然失败,因为getPublicId()
依赖于WebNumber
,返回null
完整堆栈跟踪:
java.lang.IllegalArgumentException: Cannot process null arguments.
at jaci.util.ExceptionUtils.illegalNullArgs(ExceptionUtils.java:20)
at jaci.model.check.IcnRange.<init>(IcnRange.java:54)
at jaci.model.web.Web.getIcnRange(Web.java:345)
at jaci.model.web.Web.hashCode(Web.java:255)
at java.util.HashMap.put(HashMap.java:372)
at java.util.HashSet.add(HashSet.java:200)
at java.util.AbstractCollection.addAll(AbstractCollection.java:305)
at org.hibernate.collection.PersistentSet.endRead(PersistentSet.java:352)
at org.hibernate.engine.loading.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:260)
at org.hibernate.engine.loading.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:245)
at org.hibernate.engine.loading.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:218)
at org.hibernate.loader.Loader.endCollectionLoad(Loader.java:900)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:888)
at org.hibernate.loader.Loader.doQuery(Loader.java:752)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1881)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:71)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:65)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3072)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:434)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:415)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:165)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:223)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:905)
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:873)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:590)
at org.hibernate.type.EntityType.resolve(EntityType.java:412)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:139)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:877)
at org.hibernate.loader.Loader.doQuery(Loader.java:752)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
at org.hibernate.loader.Loader.doList(Loader.java:2228)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125)
at org.hibernate.loader.Loader.list(Loader.java:2120)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:361)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1148)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at jaci.dao.WebDaoHibernate.getWebs(WebDaoHibernate.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:138)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy44.getWebs(Unknown Source)
at jaci.dao.WebDaoIntegrationTest.getWebs_NoCriteria_OneOrMoreWebs(WebDaoIntegrationTest.java:66)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.test.context.junit4.SpringTestMethod.invoke(SpringTestMethod.java:160)
at org.springframework.test.context.junit4.SpringMethodRoadie.runTestMethod(SpringMethodRoadie.java:233)
at org.springframework.test.context.junit4.SpringMethodRoadie$RunBeforesThenTestThenAfters.run(SpringMethodRoadie.java:333)
at org.springframework.test.context.junit4.SpringMethodRoadie.runWithRepetitions(SpringMethodRoadie.java:217)
at org.springframework.test.context.junit4.SpringMethodRoadie.runTest(SpringMethodRoadie.java:197)
at org.springframework.test.context.junit4.SpringMethodRoadie.run(SpringMethodRoadie.java:143)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:160)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:97)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
对疲惫的水手有什么想法吗?提前谢谢!