Hibernate - 访问一对多关联的子元素时出现NullPointerException

时间:2011-11-03 20:39:45

标签: java hibernate

有一个带有Hibernate ORM的Java SE项目。我觉得问题很简单,但需要一些帮助。

有一段代码:

    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

1 个答案:

答案 0 :(得分:1)

可能是你的映射错了。你的list-index列绝对不应该是ID。 如果您真的需要订购,最好为此创建一个单独的列,否则您将遇到问题。

我注意到的另一件事。您不必在一对多关系中指定逆。

有一段时间我见过hbm.xml文件,你能用注释吗?它们更容易理解。