1

我有 2 个类,即与 Address 类有 ManyToOne 关系的 Person 类。当我试图获取人员时,地址对象内部有空元素,为什么会发生这种情况?懒惰的配置不起作用?我可以级联保存带有地址的人员对象。

这是我的代码:

人物类:

public class Person  implements java.io.Serializable {

     private int idPerson;
     private String name;
     private String surname;
     private String dni;
     private Date dateBirth;
     private String cellPhone;
     private String landPhone;
     private String email;
     private String discr;
     private Address address ;

     public Person() {
     }
    // ¡gett

ers 和 setters } 我的地址类:

public class Address  implements java.io.Serializable {

 private int idAddress;
 private City city;
 private String street;
 private String number;
 private Integer apartament;
 private int level;

    public Address() {
    }
 //getters an setters
}

我的两个映射文件:

<hibernate-mapping>
    <class name="com.efix.model.enterprise.Address" table="address" schema="public">
        <id name="idAddress" type="int">
            <column name="id_address" />
            <generator class="assigned" />
        </id>
        <many-to-one name="city" class="com.efix.model.enterprise.City" cascade="all" lazy="false">
            <column name="id_city" not-null="true" />
        </many-to-one>
        <property name="street" type="string">
            <column name="street" length="60" not-null="true" />
        </property>
        <property name="number" type="string">
            <column name="number" length="6" />
        </property>
        <property name="apartament" type="java.lang.Integer">
            <column name="apartament" />
        </property>
        <property name="level" type="int">
            <column name="level" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

上面是地址映射,这是我的人映射:

        <!-- Sublcase cliente -->
        <subclass name="com.efix.model.enterprise.Client" discriminator-value="Client">
            <property name="notes" type="string">
                <column name="notes" />
            </property>
        </subclass>

    </class>
</hibernate-mapping>

我的 NullPointerException 在这段代码中抛出:

ClientServiceImpl serviceImpl = new ClientServiceImpl();
for (Client cli : serviceImpl.findAll()) {
    System.out.println(cli.getAddress().getNumber());

先谢谢了!!

4

1 回答 1

0

谢谢你的回复!!我的错误出现在 . 我是对不存在的财产的引用!

于 2013-10-18T13:49:02.963 回答