I have the following object model which represents a directory structure:
Code:
@Entity
@Table(name = "workspace_folders")
@AttributeOverride(name="id", column=@Column(name="path_id"))
public class DirectoryModel extends BaseModel implements Serializable {
/* user to which the directory ownership belongs */
private long user_id;
/* is this directory a users root directory? */
private Boolean root;
private String name;
private String description;
@OneToOne(targetEntity = DirectoryModel.class, fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
@JoinTable(name = "workspace_folders_rel",
joinColumns = {@JoinColumn(name = "path_id", referencedColumnName="path_id")},
inverseJoinColumns = @JoinColumn(name = "parent_id")
)
private DirectoryModel parent;
@OneToMany(targetEntity = DirectoryModel.class, fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
@JoinTable(name = "workspace_folders_rel", joinColumns = {@JoinColumn(name = "parent_id", referencedColumnName = "path_id")}, inverseJoinColumns = {@JoinColumn(name = "path_id")})
private List<DirectoryModel> folders = new ArrayList<DirectoryModel>();
@OneToMany(targetEntity = FileModel.class, fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
@JoinTable(name = "workspace_file_folder_rel", joinColumns = {@JoinColumn(name = "path_id", referencedColumnName = "path_id")}, inverseJoinColumns = {@JoinColumn(name = "file_id")})
private List<FileModel> files = new ArrayList<FileModel>();
// GETTERS AND SETTERS BELOW
the folders and files collections maintain all the sub-folders and files within the current active Model. These collections work perfectly!
My issue is with the declaration here:
Code:
@OneToOne(targetEntity = DirectoryModel.class, fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
@JoinTable(name = "workspace_folders_rel",
joinColumns = {@JoinColumn(name = "path_id", referencedColumnName="path_id")},
inverseJoinColumns = @JoinColumn(name = "parent_id")
)
private DirectoryModel parent;
I am trying to keep a direct reference to the PARENT DirectoryModel of this folder (ie. current DirectoryModel is a sub-directory of which PARENT DirectoryModel.) the parent var can obviously be null (this would imply it is a root folder.)
The two Database tables that are of issue are defined as follows:
workspace_folders:
Code:
path_id bigint(20) unsigned NO PRI NULL auto_increment
user_id bigint(20) unsigned NO
name varchar(200) NO root
description varchar(255) YES NULL
root tinyint(4) NO 0
workspace_folders_rel:
Code:
path_id bigint(11) unsigned YES NULL
parent_id bigint(11) unsigned YES NULL
I believe it is a OneToOne relationship to obtain my 'parent' var since every DirectoryModel can have at most ONE parent DirectoryModel.
On the flipside tough, a parent DirectoryModel can have multiple sub-DirectoryModels (technically represented by the folders Collection.)
I know I'm overly confusing myself here but could anyone please provide some insight as to where I might be going wrong in my mapping on the
DirectoryModel parent variable?
At present MyEclipse doesn't like the OneToOne mapping saying: "the join column parent_null cannot be found on the table workspace_folders"
I suppose this has someting to do with the parent_id column not being part of workspace_folders but don't see where my mapping is incorrect?
Thanks so much to anyone who might offer some insight!
Regards,
Skoal
[/code]