unfortunately hibernate doesn't quite work like that.
The Cascade.ALL reference on the OneToMany only means that if you call
session.delete(parent) it'll cascade down to this collection as well.
In order to delete a single child element you need to actually call
session.delete(child). Failing to do this means that all hibernate
thinks you are doing is breaking the association between parent and
child.
Personally I implment this sort of thing on the ParentDao in a method
something like:
public void removeChild(Parent parent, Child child ){
parent.removeChild(child)
getHibenerateTemplate().delete(child);
}
Hope this helps.
Gareth
On 9 Jan 2008, at 05:54, paulie wrote:
Hello,
I am using Struts 2 basic archtype version 2.0.1. I have been
unsuccessful
in using DAO for deleting a child in a simple parent/child
relationship.
Here are the steps that I took.
1. Create Master POJO:
@Entity
public class Parent extends BaseObject implements
java.io.Serializable {
private Long parentId;
private String parentName;
private List<Child> children = new
ArrayList<Child>();
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Long getParentId() {
return this.parentId;
}
public void setParentId(Long parentId) {
this.parentId = parentId;
}
@Column(name="parent_name", length=50,
nullable=false)
public String getParentName() {
return parentName;
}
public void setParentName(String parentName) {
this.parentName = parentName;
}
@OneToMany(mappedBy="parent",
cascade=CascadeType.ALL,
fetch=FetchType.EAGER)
@OrderBy("childName")
public List<Child> getChildren() {
return children;
}
public void setChildren(List<Child> childen) {
this.children = children;
}
public void addChild(Child child) {
if (child == null)
throw new
IllegalArgumentException("Null child");
this.getChildren().add(child);
}
public void removeChild(Child child) {
this.getChildren().remove(child);
}
public Child findChild(String childName) {
Child child = null;
Iterator<Child> it =
children.iterator();
while (it.hasNext()) {
child = (Child)it.next();
if
(child.getChildName().equalsIgnoreCase(childName)) {
return child;
}
}
return null;
}
}
2. Create Child POJO:
@Entity
public class Child extends BaseObject implements
java.io.Serializable {
private Long childId;
private String childName;
private Parent parent;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Long getChildId() {
return this.childId;
}
public void setChildId(Long childId) {
this.childId = childId;
}
@Column(name="child_name", length=50,
nullable=false)
public String getChildName() {
return childName;
}
public void setChildName(String childName) {
this.childName = childName;
}
@ManyToOne
@JoinColumn(name="parentId")
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
}
3. Add POJO names to hibernate.cfg.xml
<mapping class="com.appfuse.model.Parent" />
<mapping class="com.appfuse.model.Child" />
4. mvn appfuse:gen (for both Parent and Child)
5. mvn appfuse:install (for both Parent and Child)
6. mvn jetty:run-war
7. Here is the ParentDaoTest method that is failing
public void testAddAndRemoveChild() throws Exception {
Parent parent = new Parent();
parent = (Parent)parentDao.get(1L);
Child child = new Child();
child.setParent(parent);
child.setChildName("Child");
area.addChild(child);
parent = (Parent)parentDao.save(parent);
flush();
parent = (Parent)parentDao.get(parent.getParentId());
assertEquals(3, parent.getChildren().size());
child = parent.findChild("Child");
assertNotNull(child.getChildId());
parent.removeChild(child);
parent = (Parent)paretDao.save(parent);
flush();
parent = (Parent)parentDao.get(parent.getParentId());
assertEquals(2, parent.getChildren().size());
}
The insert of the child works correctly, but I am having problems
getting
the child record removed. I am getting an Assertion error that the
number
of children records is expected to be two (there are 2 other
children on
this parent by default), but three records are returned. Also, I am
not
seeing the delete SQL statement, but all others appear correctly.
Any help would be appreciated. Thanks.
--
View this message in context:
http://www.nabble.com/Problem-deleting-child-in-1..M-relationship-tp14705755s2369p14705755.html
Sent from the AppFuse - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]