从 JSF 访问复合键时目标无法访问
在我的 JSF 中,我做了
<h:outputLabel value="Group:" for="group" />
<h:inputText id="group" value="#{newUserController.group.groupKey.groupId}" title="Group Id" />
Group.java
@Entity
public class Group {
@EmbeddedId
private GroupKey groupKey;
@ManyToOne
@JoinColumn(name="userId")
private User user;
//setter, getter, constructors, equals and hashes
}
GroupKey.java
@Embeddable
public class GroupKey {
@Column(name="userId", insertable=false, updatable=false)
private String userId;
private String groupId;
//setter, getter, constructors, equals and hashes
}
因此,当我尝试保留该对象时,它给了我这个错误
value="#{newUserController.group.groupKey.groupId}": Target Unreachable, 'null' returned null
编辑
这是我的托管 Bean 的内容。
@ManagedBean(name="newUserController")
@RequestScoped
public class NewUserController {
private User user = new User();
private Group group = new Group();
@EJB
DocumentSBean sBean;
public void createNewUser(){
user.addGroup(group);
sBean.persist(user);
}
}
Inside my JSF I did
<h:outputLabel value="Group:" for="group" />
<h:inputText id="group" value="#{newUserController.group.groupKey.groupId}" title="Group Id" />
Group.java
@Entity
public class Group {
@EmbeddedId
private GroupKey groupKey;
@ManyToOne
@JoinColumn(name="userId")
private User user;
//setter, getter, constructors, equals and hashes
}
GroupKey.java
@Embeddable
public class GroupKey {
@Column(name="userId", insertable=false, updatable=false)
private String userId;
private String groupId;
//setter, getter, constructors, equals and hashes
}
So when I try to persist the object, it give me this error
value="#{newUserController.group.groupKey.groupId}": Target Unreachable, 'null' returned null
EDIT
Here is the content of my Managed Bean.
@ManagedBean(name="newUserController")
@RequestScoped
public class NewUserController {
private User user = new User();
private Group group = new Group();
@EJB
DocumentSBean sBean;
public void createNewUser(){
user.addGroup(group);
sBean.persist(user);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
#{newUserController.group}
或#{newUserController.group.groupKey}
返回null
。 JSF 将仅设置 last 属性(在本例中为groupId
)。如果这是模板实体,您需要为group
和/或groupKey
提供/预设一个默认且非空的值。例如,您可以在 bean 的(后)构造函数中执行此操作。如果这是一个现有实体,您需要确保 JPA 正确预填充了这些属性。Either
#{newUserController.group}
or#{newUserController.group.groupKey}
returnednull
. JSF will only set the last property (which isgroupId
in this case). If this is a template entity, you need to provide/preset a default and non-null value forgroup
and/orgroupKey
. You can do this in for example the (post)constructor of the bean. If this is an existing entity, you need to ensure that those properties are properly prepopulated by JPA.我通过添加到 Group 类来解决它
,它将被初始化并且不会返回 null。
I solved it by adding
to the Group Class, it will be initialized and it will not return null.
PostConstruct 对我不起作用(但我是新手,所以请接受它的价值)。但是,将其添加到实体类构造函数中却可以。
The PostConstruct did not work for me (but I'm a newbie so take that for what it's worth). However, adding it to the entity class constructor did.