gpt4 book ai didi

java - Hibernate在对象关系中全部抛出空指针异常

转载 作者:行者123 更新时间:2023-12-01 04:16:03 24 4
gpt4 key购买 nike

我有 2 个类,即与 Address 类具有多对一关系的 Person 类。当我试图获取人员时,地址对象内部有空元素,为什么会发生这种情况?惰性配置不起作用?我可以级联保存一个包含地址的 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() {
}
// getters and 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>

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

<hibernate-mapping>
<class name="com.efix.model.enterprise.Person" table="person" schema="public">
<id name="idPerson" type="int">
<column name="id_person" />
<generator class="identity" />
</id>
<discriminator column="discr" type="string"/>
<property name="name" type="string">
<column name="name" length="40" not-null="true" />
</property>
<property name="surname" type="string">
<column name="surname" length="40" not-null="true" />
</property>
<property name="dni" type="string">
<column name="dni" length="40" />
</property>
<property name="dateBirth" type="date">
<column name="date_birth" length="13" />
</property>
<property name="cellPhone" type="string">
<column name="cell_phone" length="20"/>
</property>
<property name="landPhone" type="string">
<column name="land_phone" length="20"/>
</property>
<property name="email" type="string">
<column name="email" length="25" />
</property>
<many-to-one class="com.efix.model.enterprise.Address" name="address">
<column name="id_address" not-null="true"/>
</many-to-one>
<!-- Subclase Empleado -->
<subclass name="com.efix.model.enterprise.Employee" discriminator-value="Employee">
<property name="admissionDate" type="date">
<column name="admission_date" length="13" />
</property>
<property name="outDate" type="date">
<column name="out_date" length="13" />
</property>
<property name="position" type="string">
<column name="position" length="40" />
</property>
</subclass>

<!-- 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());

最佳答案

谢谢您的回复!!我的错误出在 .我引用了一个不存在的属性(property)!!

关于java - Hibernate在对象关系中全部抛出空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19435959/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com