浏览 2959 次
|
锁定老贴子 主题:请教:这样查询如何处理?
该帖已经被评为精华帖
|
|
|---|---|
| 作者 | 正文 |
|
最后更新时间:2004-03-29
实体:File
id | parentid | filesize 1 | 0 | 100 2 | 1 | 75 3 | 1 | 25 4 | 2 | 50 5 | 2 | 25 6 | 5 | 20 7 | 6 | 5 1、求出所有id号为1的所有子对象数据(如上例显示包括:2,3,4,5,6,7); 2、更新所有id号为1的所有子对象的filesize为0 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
最后更新时间:2004-03-29
比如同时更新子对象\子对象的子对象\....
应如何高效的处理呢? |
|
| 返回顶楼 | |
|
最后更新时间:2004-04-03
这个问题看见好几次了,今天动手尝试了一下,这里是源程序:
Databas scheme: [code:1]use test; drop table if exists file; create table file( id int NOT NULL AUTO_INCREMENT, parentid int, filesize int NOT NULL DEFAULT 0, PRIMARY KEY (id), INDEX (parentid), FOREIGN KEY (parentid) REFERENCES file(id) ON DELETE CASCADE ); insert into file(id, filesize) values (1, 100); insert into file(id, parentid, filesize) values (2, 1, 75); insert into file(id, parentid, filesize) values (3, 1, 25); insert into file(id, parentid, filesize) values (4, 2, 50); insert into file(id, parentid, filesize) values (5, 2, 25); insert into file(id, parentid, filesize) values (6, 5, 20); insert into file(id, parentid, filesize) values (7, 6, 5); [/code:1] hibernate-mapping: [code:1] <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="file.File" table="file"> <id name="id" column="id" type="int" unsaved-value="null" > <generator class="identity"/> </id> <property name="parentID" column="parentid" type="int" not-null="false" insert="false" update="false"/> <property name="filesize" column="filesize" type="int" not-null="true" /> <many-to-one name="parent" column="parentid" update="false" not-null="false"/> <bag name="childs" inverse="true" lazy="false" cascade="all-delete-orphan" > <key column="parentid"/> <one-to-many class="file.File"/> </bag> </class> </hibernate-mapping> [/code:1] PO: [code:1] package file; import java.util.List; import java.util.ArrayList; public class File { private Integer id; private Integer parentID; private Integer filesize; private File parent; private List children = new ArrayList(); public File() { } public File(Integer fileSize) { this.filesize = fileSize; } public File(int size) { this.filesize = new Integer(size); } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getParentID() { return parentID; } public void setParentID(Integer parentID) { this.parentID = parentID; } public Integer getFilesize() { return filesize; } public void setFilesize(Integer filesize) { this.filesize = filesize; } public File getParent() { return parent; } public void setParent(File parent) { this.parent = parent; } public List getChildren() { return children; } public void setChildren(List children) { this.children= children; } public void addChild(File child) { child.setParent(this); this.children.add(child); } public void printTree() { System.out.println("ID: " + id + "\t" + "Parent: " + "\t" + "Filesize: " + filesize); for (int i = 0; i < children.size(); i++) { ((File)children.get(i)).printTree(); } } public void resetFilesize() { this.filesize = new Integer(0); for (int i = 0; i < children.size(); i++) { ((File)children.get(i)).resetFilesize(); } } } [/code:1] DAO这里就省略了。如需要,我可以附上全部代码,包括ant build和junit test case. database: MySQL, table type: InnoDB |
|
| 返回顶楼 | |
|
最后更新时间:2004-03-31
为了便于大家读程序,再加上junit test case:
[code:1] package file; import junit.framework.TestCase; import net.sf.hibernate.HibernateException; public class TestFile extends TestCase { FileDAO fileDAO = new FileDAO(); File file1; File file2; File file3; public void setUp() { file1 = new File(100); file2 = new File(200); file3 = new File(300); file2.addChild(file3); file1.addChild(file2); try { fileDAO.saveFile(file1); } catch (HibernateException e) { e.printStackTrace(); fail(); } } public void testSave() { assertTrue(file1.getId().intValue() > 0); assertTrue(file2.getId().intValue() > 0); assertTrue(file3.getId().intValue() > 0); } public void testDelete() { Integer id1 = file1.getId(); Integer id2 = file2.getId(); Integer id3 = file1.getId(); try { fileDAO.deleteFile(file1); } catch (HibernateException e) { e.printStackTrace(); fail(); } try { fileDAO.loadFile(id1); fail("File1 should be deleted"); } catch (HibernateException e) { } try { fileDAO.loadFile(id2); fail("File2 should be deleted"); } catch (HibernateException e) { } try { fileDAO.loadFile(id3); fail("File3 should be deleted"); } catch (HibernateException e) { } } public void testLoad() { try { File theFile1 = fileDAO.loadFile(file1.getId()); File theFile2 = (File) theFile1.getChildren().get(0); File theFile3 = (File) theFile2.getChildren().get(0); assertTrue(theFile1.getId().equals(file1.getId())); assertTrue(theFile2.getId().equals(file2.getId())); assertTrue(theFile3.getId().equals(file3.getId())); theFile1.printTree(); } catch (HibernateException e) { e.printStackTrace(); fail(); } } public void testResetFilesize() { file1.resetFilesize(); try { fileDAO.updateFile(file1); File theFile1 = fileDAO.loadFile(file1.getId()); File theFile2 = (File) theFile1.getChildren().get(0); File theFile3 = (File) theFile2.getChildren().get(0); assertTrue(theFile1.getFilesize().intValue() == 0); assertTrue(theFile2.getFilesize().intValue() == 0); assertTrue(theFile3.getFilesize().intValue() == 0); theFile1.printTree(); } catch (HibernateException e) { e.printStackTrace(); fail(); } } public void tearDown() { try { fileDAO.deleteFile(file1); } catch (HibernateException e) { e.printStackTrace(); } } }[/code:1] |
|
| 返回顶楼 | |




