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);
}
}
Either
#{newUserController.group}or#{newUserController.group.groupKey}returnednull. JSF will only set the last property (which isgroupIdin this case). If this is a template entity, you need to provide/preset a default and non-null value forgroupand/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.