文件夾表: -
folder_id,folder_name,parent_id
---------------------------------
1,7-Zip,NULL
2,7z,1
3,Lang,1
文件表: -
file_id,file_name,folder_id
---------------------------------
1,7-zip.chm,1
2,7-zip.dll,1
3,7z.dll,3
4,af.txt,3
當我插入的數據,該數據結構是象下面(DS的尺寸爲1)
[1,7-Zip,
childFolders[[2,7z],[3,Lang,childFolders[], childFiles[[3,7z.dll],[4,af.txt]]]],
childfiles[[1,7-zip.chm],[2,7-zip.dll]]]
當檢索從數據庫中的數據,然後我得到這樣的事情。(DS的大小爲3)
[1,7-Zip,
childFolders[[2,7z],[3,Lang,childFolders[], childFiles[[3,7z.dll],[4,af.txt]]]],
childfiles[[1,7-zip.chm],[2,7-zip.dll]]]
[2,7z,
childFolders[],
childfiles[]]
[3,Lang,
childFolders[],
childfiles[[3,7z.dll],[4,af.txt]]]
下面
是我的神器
SearchedFolder
@Entity
@Table(name="Searchedfolder")
public class SearchedFolder implements Serializable, Comparable<SearchedFolder>{
/**
*
*/
private static final long serialVersionUID = -7373115811110399682L;
private long folderId;
private String folderName;
private String path;
private Date searchedTime;
private String description;
private Set<SearchedFile> childFiles = new HashSet<SearchedFile>();
private Set<SearchedFolder> childFolders = new TreeSet<SearchedFolder>();
private SearchedFolder parentFolder;
@Id
@Column(name="folder_id",insertable=true, updatable=true)
@GeneratedValue(strategy = GenerationType.AUTO)
public long getFolderId() {
return folderId;
}
public void setFolderId(long id) {
this.folderId = id;
}
@Column(name="path")
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
@Column(name="searched_time")
public Date getSearchedTime() {
return searchedTime;
}
public void setSearchedTime(Date searchedTime) {
this.searchedTime = searchedTime;
}
@Column(name="description")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Column(name="folder_name")
public String getFolderName() {
return folderName;
}
public void setFolderName(String folderName) {
this.folderName = folderName;
}
@OneToMany(targetEntity= SearchedFile.class, mappedBy="parentFolder", cascade={CascadeType.ALL}, fetch = FetchType.EAGER)
public Set<SearchedFile> getChildFiles() {
return childFiles;
}
public void setChildFiles(Set<SearchedFile> childFiles) {
this.childFiles = childFiles;
}
@OneToMany(mappedBy="parentFolder", cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
public Set<SearchedFolder> getChildFolders() {
return childFolders;
}
public void setChildFolders(Set<SearchedFolder> folders) {
this.childFolders = folders;
}
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name="parent_id")
public SearchedFolder getParentFolder() {
return parentFolder;
}
public void setParentFolder(SearchedFolder parentFolder) {
this.parentFolder = parentFolder;
}
public int compareTo(SearchedFolder searchedFolder) {
return this.getFolderName().compareTo(searchedFolder.getFolderName());
}
}
已檢索文件
@Entity
@Table(name="searchedfile")
public class SearchedFile implements Serializable, Comparable<SearchedFile>{
/**
*
*/
private static final long serialVersionUID = -7474936475144364605L;
private long fileId;
private String fileName;
private Date searchedTime;
private String extension;
private String description;
private SearchedFolder parentFolder;
@Id
@Column(name="file_id",insertable=true, updatable=true)
@GeneratedValue(strategy = GenerationType.AUTO)
public long getFileId() {
return fileId;
}
public void setFileId(long fileId) {
this.fileId = fileId;
}
@Column(name="file_name")
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
@Column(name="searched_time")
public Date getSearchedTime() {
return searchedTime;
}
public void setSearchedTime(Date searchedTime) {
this.searchedTime = searchedTime;
}
@Column(name="extension")
public String getExtension() {
return extension;
}
public void setExtension(String extension) {
this.extension = extension;
}
@Column(name="description")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@ManyToOne(targetEntity= SearchedFolder.class, cascade={CascadeType.ALL}, fetch = FetchType.EAGER)
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(name="folder_id")
public SearchedFolder getParentFolder() {
return this.parentFolder;
}
public void setParentFolder(SearchedFolder parentFolder) {
this.parentFolder = parentFolder;
}
public int compareTo(SearchedFile searchedFile) {
return this.getFileName().compareTo(searchedFile.getFileName());
}
}
DAO文件: -
@Repository
public class SearchedFolderDAOImpl implements SearchedFolderDAO {
@Autowired
private SessionFactory schedulerSessionFactory;
@Override
public void addSearchedData(SearchedFolder searchedFolder) {
Session session = schedulerSessionFactory.openSession();
if(session == null){
schedulerSessionFactory.openSession();
}
try{
session.save(searchedFolder);
}catch (HibernateException e) {
e.printStackTrace();
}finally {
session.close();
}
}
@Override
public List<SearchedFolder> getSearchedData() {
Criteria ctria = schedulerSessionFactory.getCurrentSession().createCriteria(SearchedFolder.class)
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
.setFetchMode("childFiles", FetchMode.SELECT);
List<SearchedFolder> folders = (List<SearchedFolder>) ctria.list();
return folders;
}
@Override
public void deleteSearchedData(Integer SearchedDataId) {
// TODO Auto-generated method stub
}
@Override
public List<SearchedFolder> getSearchedData(String folderName) {
// TODO Auto-generated method stub
return null;
}
}
服務文件: -
@Service("defaultSearchDataService")
public class SearchDataServiceImpl implements SearchDataService{
{System.out.println("defaultSearchDataService...");}
@Autowired
private SearchedFileDAO searchedFileDAO;
@Autowired
private SearchedFolderDAO searchedFolderDAO;
@Override
@Transactional(readOnly=false, timeout=300, propagation=Propagation.REQUIRES_NEW)
public void saveFiles(List<SearchedFile> files) {
// TODO Auto-generated method stub
}
@Override
@Transactional(readOnly=true, timeout=300, propagation=Propagation.REQUIRED)
public List<SearchedFile> getFiles() {
// TODO Auto-generated method stub
return null;
}
@Override
@Transactional(readOnly=true, timeout=300, propagation=Propagation.REQUIRES_NEW)
public List<SearchedFolder> getFolders() {
List<SearchedFolder> folders = searchedFolderDAO.getSearchedData();
return folders;
}
@Override
@Transactional(readOnly=false,propagation=Propagation.REQUIRES_NEW, timeout=30)
public void addFolder(NewFolderForm newFolderForm) {
// TODO Auto-generated method stub
}
@Override
@Transactional(readOnly=false, timeout=300, propagation=Propagation.REQUIRED)
public void saveFolder(SearchedFolder searchedFolder) {
searchedFolderDAO.addSearchedData(searchedFolder);
}
@Override
public void saveFolder(Set<SearchedFolder> directory) {
Iterator<SearchedFolder> searchedFolderIterator = directory.iterator();
while(searchedFolderIterator.hasNext()){
SearchedFolder searchedFolder = searchedFolderIterator.next();
saveFolder(searchedFolder);
}
}
}
問題1:爲什麼插入和檢索數據結構有什麼不同?
問題2:如何解決此問題
好的!相當不錯並且有效的一點。然後在我的情況下如何獲得最多的根數據,並避免其他結果,如文件夾2,3作爲1是文件夾的父文件夾[2,3] – connect2vishal
我很高興幫助你。我忘了第二個問題,所以我只是補充了第二部分。 – JCalcines
但想象一下,我們有超過1個根級別的數據,那麼可能會有什麼解決方案。 例如, 1-> 2-> 3 - > 4 - > 5 6-> 7-> 8-> 9 10 - > 11 - > 12 現在這裏我們有3個根元素。 – connect2vishal