Существует проект Java SE с Hibernate ORM.Я чувствую, что проблема тривиальна, но мне нужна помощь.
Существует фрагмент кода:
SessionFactory factory = new Configuration().configure().buildSessionFactory();
Session s = factory.openSession();
int id = 1;
ExperimentSetResult experimentSetResult = (ExperimentSetResult)s.get(ExperimentSetResult.class, id);
System.out.println("size: " + experimentSetResult.getExperimentResults().size());
System.out.println("id[0]: " + experimentSetResult.getExperimentResults().get(0).getId());
Я получаю исключение NullPointerException для последней строки кода (при доступе к 0-ый элемент коллекции, связанный с недавно загруженным объектом).
Имеются фрагменты файлов hbm:
ExperimentResult.hbm.xml:
<hibernate-mapping>
<class name="rmocommon.driverreaction.ExperimentResult" table="experiment_results">
<id name="id" type="int">
<generator class="increment"/>
</id>
<many-to-one class="rmocommon.driverreaction.ExperimentSetResult" name="ExperimentSetResult" column="ExperimentSetResultId" not-null="true" />
</class>
</hibernate-mapping>
ExperimentSetResult.hbm.xml:
<hibernate-mapping>
<class name="rmocommon.driverreaction.ExperimentSetResult" table="experiment_set_results">
<id name="id" type="int">
<generator class="increment"/>
</id>
<list name="ExperimentResults" cascade="all-delete-orphan" inverse="true">
<key column="ExperimentSetResultId" not-null="true"/>
<list-index column="Id"/>
<one-to-many class="rmocommon.driverreaction.ExperimentResult"/>
</list>
</class>
</hibernate-mapping>
Что не так с отображением или моим исходным кодом?
ОБНОВЛЕНИЕ: Вот вывод и трассировка стека:
Hibernate: select experiment0_.id as id4_2_, experiment0_.StartedDate as StartedD2_4_2_, experiment0_.FinishedDate as Finished3_4_2_, experiment0_.DeviceOutput as DeviceOu4_4_2_, person1_.id as id0_0_, person1_.Login as Login0_0_, person1_.LastName as LastName0_0_, person1_.Patronymic as Patronymic0_0_, person1_.FirstName as FirstName0_0_, person1_.Age as Age0_0_, experiment2_.id as id1_1_, experiment2_.TestMode as TestMode1_1_, experiment2_.TransportType as Transpor3_1_1_, experiment2_.TransportStartSpeed as Transpor4_1_1_, experiment2_.RoadType as RoadType1_1_, experiment2_.RoadLength as RoadLength1_1_, experiment2_.DirectionLeft as Directio7_1_1_, experiment2_.RespondToFirstEffort as RespondT8_1_1_, experiment2_.SoundOnFirstEffort as SoundOnF9_1_1_, experiment2_.ScaleObjects as ScaleOb10_1_1_, experiment2_.ShowTransportSpeed as ShowTra11_1_1_, experiment2_.BarrierXMin as Barrier12_1_1_, experiment2_.BarrierXMax as Barrier13_1_1_, experiment2_.ReactionTime as Reactio14_1_1_, experiment2_.SoundOnBarrierAppearance as SoundOn15_1_1_, experiment2_.AllowedCheatCount as Allowed16_1_1_ from experiment_set_results experiment0_ left outer join persons person1_ on experiment0_.id=person1_.id left outer join experiment_set_settings experiment2_ on experiment0_.id=experiment2_.id where experiment0_.id=?
Hibernate: select experiment0_.ExperimentSetResultId as Experim11_4_1_, experiment0_.id as id1_, experiment0_.Id as Id1_, experiment0_.id as id2_0_, experiment0_.Distance as Distance2_0_, experiment0_.Crash as Crash2_0_, experiment0_.BrakingStarted as BrakingS4_2_0_, experiment0_.BrakingStartedTime as BrakingS5_2_0_, experiment0_.BrakingStartedDistance as BrakingS6_2_0_, experiment0_.BarrierX as BarrierX2_0_, experiment0_.Number as Number2_0_, experiment0_.Time as Time2_0_, experiment0_.Valid as Valid2_0_, experiment0_.ExperimentSetResultId as Experim11_2_0_ from experiment_results experiment0_ where experiment0_.ExperimentSetResultId=?
size: 6
Exception in thread "main" java.lang.NullPointerException
at hibernateTest.HibernateTest.main(HibernateTest.java:45)
Java Result: 1