1
我有2個類,Person類與Address類具有ManyToOne關係。當我試圖找人時,地址對象裏面有空元素,爲什麼會發生這種情況?懶惰的配置不工作?我可以使用地址級聯保存人員對象。休眠獲取所有拋出Nullpointerexcepetion ina對象關係
這裏是我的代碼:
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和setter } 我的地址類:
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());
感謝先進!
請發佈錯誤的整個堆棧跟蹤並突出顯示解釋錯誤的代碼行。而且什麼是'ClientServiceImpl'? – Paolo
這是一個例外:2013年10月18日上午9:45:53 com.sun.faces.context.PartialViewContextImpl $ PhaseAwareVisitCallback訪問 SEVERE:javax.el.ELException:/enterprises/persons/client.xhtml @ 236,141 value =「 #{clientController.selectedClient.address.number}「:讀取類型爲com.efix.model.enterprise.Address的$號錯誤_ $$ _ javassist_0 ClientServiceImpl是調用DAO以將數據保存在D B。我調試該項目,我有地址對象null的引用。 – user1977204
愚蠢的問題,但我需要確定...這個人有一個有效的(非空)地址(我的意思是數據庫)? – Paolo