-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: CRUD with a OneToMany association under Struts 2 / Hibernate
PostPosted: Sat Mar 27, 2010 1:10 pm 
Newbie

Joined: Thu Mar 25, 2010 5:55 am
Posts: 3
Hi

I am trying to implement a simple CRUD with a OneToMany association under Struts 2 / Hibernate 3.
I have two entities Parent and Child:

@Entity
@Table(name="PARENT")
public class Parent {
private Long id;
private Set<Child> values = new HashSet<Child>();
..
@Entity
@Table(name="CHILD")
public class Child {
private Long id;
private String name;
..

I can easily create, delete Parent or read the Child Set (values) but it is impossible to update Child Set.
The jsp page (see below) reinit the values Set, no record after updating!
Could u explain to me what's wrong?

here are my code:

@Entity
@Table(name="PARENT")
public class Parent {
private Long id;
private Set<Child> values = new HashSet<Child>();
@Id
@GeneratedValue
@Column(name="PARENT_ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "PARENT_CHILD", joinColumns = { @JoinColumn(name = "PARENT_ID") }, inverseJoinColumns = { @JoinColumn(name = "CHILD_ID") })
public Set<Child> getValues() {
return values;
}
public void setValues(Set<Child> lst) {
values = lst;
}
}

@Entity
@Table(name="CHILD")
public class Child {
private Long id;
private String name;
@Id
@GeneratedValue
@Column(name="CHILD_ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name="NAME")
public String getName() {
return name;
}
public void setName(String val) {
name = val;
}
}

public interface ParentDAO {
public void saveOrUpdateParent(Parent cl);
public void saveParent(Parent cl);
public List<Parent> listParent();
public Parent listParentById(Long clId);
public void deleteParent(Long clId);
}

public class ParentDAOImpl implements ParentDAO {
@SessionTarget
Session session;
@TransactionTarget
Transaction transaction;

@Override
public void saveOrUpdateParent(Parent cl) {
try {
session.saveOrUpdate(cl);
} catch (Exception e) {
transaction.rollback();
e.printStackTrace();
}
}

@Override
public void saveParent(Parent cl) {
try {
session.save(cl);
} catch (Exception e) {
transaction.rollback();
e.printStackTrace();
}
}

@Override
public void deleteParent(Long clId) {
try {
Parent cl = (Parent) session.get(Parent.class, clId);
session.delete(cl);
} catch (Exception e) {
transaction.rollback();
e.printStackTrace();
}
}

@SuppressWarnings("unchecked")
@Override
public List<Parent> listParent() {
List<Parent> courses = null;
try {
courses = session.createQuery("from Parent").list();
} catch (Exception e) {
e.printStackTrace();
}
return courses;
}

@Override
public Parent listParentById(Long clId) {
Parent cl = null;
try {
cl = (Parent) session.get(Parent.class, clId);
} catch (Exception e) {
e.printStackTrace();
}
return cl;
}
}

public class ParentAction extends ActionSupport implements ModelDriven<Parent> {

private static final long serialVersionUID = -2662966220408285700L;
private Parent cl = new Parent();
private List<Parent> clList = new ArrayList<Parent>();
private ParentDAO clDAO = new ParentDAOImpl();

@Override
public Parent getModel() {
return cl;
}

public String saveOrUpdate()
{
clDAO.saveOrUpdateParent(cl);
return SUCCESS;
}

public String save()
{
clDAO.saveParent(cl);
return SUCCESS;
}

public String list()
{
clList = clDAO.listParent();
return SUCCESS;
}

public String delete()
{
HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
clDAO.deleteParent(Long.parseLong(request.getParameter("id")));
return SUCCESS;
}

public String edit()
{
HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
cl = clDAO.listParentById(Long.parseLong(request.getParameter("id")));
return SUCCESS;
}

public Parent getParent() {
return cl;
}

public void setParent(Parent cl) {
this.cl = cl;
}

public List<Parent> getParentList() {
return clList;
}

public void setParentList(List<Parent> clList) {
this.clList = clList;
}
}

and finally the jsp page:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Parent Registration Page</title>
<s:head />
<style type="text/css">
@import url(style.css);
</style>
</head>
<body>
<s:form action="saveOrUpdateParent">
<s:push value="parent">
<s:hidden name="id" />
<s:push value="values">
<s:iterator id="p" value="values">
<s:textfield label="Nom" name="values(%{id}).name" value="%{name}"/>
</s:iterator>
</s:push>
<s:submit />
</s:push>
</s:form>

<s:if test="parentList.size() > 0">
<div class="content">
<table class="userTable" cellpadding="5px">
<tr class="even">
<th>Child(s)</th>
</tr>
<s:iterator value="parentList" status="userStatus">
<tr
class="<s:if test="#userStatus.odd == true ">odd</s:if><s:else>even</s:else>">
<td><s:property value="values.size()" /></td>
<s:iterator value="values">
<td><s:property value="name" /></td>
</s:iterator>
<td><s:url id="editURL" action="editParent">
<s:param name="id" value="%{id}"></s:param>
</s:url> <s:a href="%{editURL}">Edit</s:a></td>
<td><s:url id="deleteURL" action="deleteParent">
<s:param name="id" value="%{id}"></s:param>
</s:url> <s:a href="%{deleteURL}">Delete</s:a></td>
</tr>
</s:iterator>
</table>
</div>
</s:if>
</body>
</html>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.