I am sure this has been asked before, but the keywords I used for the search did not help.
Why does the below test fail? Specifically, I expected the id to be non-null, but it was null.
Code:
public void testIdAfterMergeNotNull() {
EntityManager mgr = openSession();
mgr.getTransaction().begin();
ParentDBO parent = new ParentDBO();
parent.setName("notnullID");
mgr.merge(parent);
//NOTE: I think this should be non-null but the test fails
//because it is null
assertNotNull(parent.getId());
mgr.getTransaction().commit();
mgr.close();
}
Just in case it helps, here is the bean that goes with it....
Code:
@Entity
@Table( name = "Parents")
@NamedQueries(
{
@NamedQuery(name="getParents", query="SELECT c FROM ParentDBO c")
})
public class ParentDBO {
private Long id;
private int version;
private String name;
private Collection<ChildDBO> children;
@Column(name="Id")
@Id()
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
@SuppressWarnings("all")
private void setId(Long id) {
this.id = id;
}
@Version
@Column(name="Version", nullable=false)
public int getVersion() {
return version;
}
@SuppressWarnings("all")
private void setVersion(int version) {
this.version = version;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(mappedBy="parent", cascade = CascadeType.ALL, fetch=FetchType.EAGER)
public Collection<ChildDBO> getChildren() {
if(children == null) {
children = new ArrayList<ChildDBO>();
}
return children;
}
public void setChildren(Collection<ChildDBO> children) {
this.children = children;
}
public void addChild(ChildDBO child) {
if(child == null)
throw new IllegalArgumentException("child may not be null and was");
else if(child.getParent() != null)
child.getParent().getChildren().remove(child);
child.setParent(this);
getChildren().add(child);
//NOTE: testing
setChildren(getChildren());
}
public void removeChild(ChildDBO child) {
if(child == null)
throw new IllegalArgumentException("child may not be null and was");
else if(!getChildren().contains(child))
throw new IllegalArgumentException("Parent does not contain this child");
child.setParent(null);
getChildren().remove(child);
}
}
[/code]