在 JSF 形式中有:
<h:inputText value="#{personBean.person.personDetail.attribute}" />
实体是在带注释的方法Person
上从数据库加载的。但是, instancePostConstruct
的属性可以为 null。因此,当填写输入并提交表单时,会抛出异常(更具体地说)。personDetail
Person
javax.el.PropertyNotFoundException: (...) Target Unreachable, ''personDetail'' returned null
我搜索并找到了一些很好的例子来解决这个异常,例如:
但是,我认为这个问题是不同的。person
找到实例并且其实例属性可以为空。
我还尝试设置一个新实例,PersonDetail
当它在方法上为空时,但由于“对象( )引用了未保存的瞬态实例” PostConstruct
,因此问题变成了持久性问题。personDetail
那么,如何看待这个问题呢?
资源
短豆:
@Named(value = "personBean")
@ViewScoped
public class PersonBean {
(...)
@PostConstruct
public void init() {
person = personService.findById(1L);
// throw -> TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing
// if (person.getPersonDetail() == null) {
// person.setPersonDetail(new PersonDetail());
// }
(...)
}
}
短实体类:
@Entity
public class Person {
(...)
@OneToOne(mappedBy = "pessoa", fetch = FetchType.LAZY)
private PersonDetail personDetail;
// Getters and Setters
}
-
public class PersonDetail {
private String attribute;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "person_id", unique = true, nullable = false)
private Person person;
// Getters and Setters
}