I have created the basic struts2 application, added a Folder class having a
"String foldername" and a Task class having references to a source folder
and a destination folder. Since each folder can appear on different tasks
(or none at all), I am using a @ManyToOne annotation in Task.java, so that
each Task has a srcFolderid and dstFolderid.
After running appfuse:gen and fixing the taskForm.jsp I have a Task Form
with two dropdown menus. Everything works as expected: I can modify and save
both dropdown menus and the changes are properly stored in the database.
However once I have set both srcFolder and dstFolder to the same Folder, it
is no longer possible to set them to distinct values again. The srcFolder
value is always written to both fields. But when I remove the html code for
srcFolder from the jsp so that only the dstFolder dropdown is left, then the
dstFolder value is written to both.
Here is the code for Task.java:
package tutorial.model;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import javax.persistence.*;
import java.io.Serializable;
@Entity
@Table(name="task")
public class Task extends BaseObject implements Serializable {
private static final long serialVersionUID = 3832626162173359001L;
private Long id;
private Long ownerid;
private Folder srcFolder;
private Folder dstFolder;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(nullable=true)
public Long getOwnerid() {
return ownerid;
}
public void setOwnerid(Long ownerid) {
this.ownerid = ownerid;
}
@ManyToOne
@JoinColumn(name="srcFolderid")
public Folder getSrcFolder() {
return srcFolder;
}
public void setSrcFolder(Folder srcFolder) {
this.srcFolder = srcFolder;
}
@ManyToOne
@JoinColumn(name="dstFolderid")
public Folder getDstFolder() {
return dstFolder;
}
public void setDstFolder(Folder dstFolder) {
this.dstFolder = dstFolder;
}
/**
* {...@inheritdoc}
*/
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Task)) {
return false;
}
final Task task = (Task) o;
if (!ownerid.equals(task.ownerid))
return false;
if (!srcFolder.equals(task.srcFolder.getFoldername()))
return false;
if (!dstFolder.equals(task.dstFolder.getFoldername()))
return false;
return true;
}
/**
* {...@inheritdoc}
*/
public String toString() {
ToStringBuilder sb = new ToStringBuilder(this,
ToStringStyle.DEFAULT_STYLE)
.append("id", this.id)
.append("ownerid", ownerid)
.append("srcfolder", this.srcFolder.getFoldername())
.append("dstfolder", this.dstFolder.getFoldername());
return sb.toString();
}
/**
* {...@inheritdoc}
*/
public int hashCode() {
int hashCode = 0;
if (ownerid != null)
hashCode += 29 * ownerid.hashCode();
if (srcFolder != null)
hashCode += 29 * srcFolder.hashCode();
if (dstFolder != null)
hashCode += 29 * dstFolder.hashCode();
return hashCode;
}
}
I have also uploaded the complete source code here:
http://www.speedshare.org/download.php?id=4D5FF7FC11
--
View this message in context:
http://www.nabble.com/Strange-problem-with-%40ManyToOne-tp21539322s2369p21539322.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]