将java对象树转换为二维表
我面临以下问题。我有一个 java 对象树,我必须将其每个字段值导出到 CSV 文件中。导出的结果必须类似于 SQL 左外连接(称为笛卡尔积)中的结果。
班级作者
@DataField(pos = 1)
String firstName;
@DataField(pos = 2)
String lastName;
@OneToMany
List<Book> books;
@OneToMany
List<Editor> editors;
@DataField(pos = 7)
String Age;
班级书籍
@DataField(pos = 3)
String title;
@DataField(pos = 4)
String year;
@OneToMany
List<Reference> references;
班级参考资料
@DataField(pos = 5)
String type;
@DataField(pos = 6)
String code;
班级编辑
@DataField(pos = 8)
String name;
备注: - @DataField注解表示该值在CSV记录中的位置 - 在此示例中,我们有一个对象 Author(Charles、Moulliard),其中包含 2 本书的列表(“Camel in action”和“Camel in action 2”)。第一本书有三个参考文献(ISBN 1234、ISBN 5678、ISBN 999)和第二个参考文献(ISBB 1111)。作者还包含两个编辑者的列表(“manning”,“manning 2”)
这是一个示例,结果需要
“firstName”,“lastName”,“age”,“title”,“year”,“type”, “代码”、“名称” “查尔斯”、“莫利亚尔”、“骆驼在行动”、“2009”、“ISBN”、“1234”、“曼宁”、“43” “查尔斯”、“莫利亚尔”、“骆驼在行动”、“2009”、“ISBN”、“1234”、“曼宁 2”、“43” “查尔斯”、“莫利亚尔”、“骆驼在行动”、“2009”、“ISBN”、“5678”、“曼宁”、“43” “查尔斯”、“莫利亚尔”、“骆驼在行动”、“2009”、“ISBN”、“5678”、“曼宁 2”、“43” “查尔斯”、“莫利亚尔”、“骆驼在行动”、“2009”、“ISBN”、“9999”、“曼宁”、“43” “查尔斯”、“莫利亚尔”、“骆驼在行动”、“2009”、“ISBN”、“9999”、“曼宁 2”、“43” “查尔斯”、“莫利亚德”、“骆驼行动 2”、“2011”、“ISBB”、“1111”、“曼宁”、“43” “charles”,“moulliard”,“camel in action 2”,“2011”,“ISBB”,“1111”,“manning 2”,“43”
我尝试使用递归函数将字段值放入LinkedList的Map中:映射,其中 Integer = CSV 中字段的位置,Linkedlist = 对象列表,但我丢失了有关树中元素位置的信息。
问候,
查尔斯
I'm faced to the following problem. I have a tree of java objects for which I have to export each field value into a CSV file. The result of the exportation must be similar to what we have in SQL left outer join (called cartesian product).
Class author
@DataField(pos = 1)
String firstName;
@DataField(pos = 2)
String lastName;
@OneToMany
List<Book> books;
@OneToMany
List<Editor> editors;
@DataField(pos = 7)
String Age;
Class Book
@DataField(pos = 3)
String title;
@DataField(pos = 4)
String year;
@OneToMany
List<Reference> references;
Class Reference
@DataField(pos = 5)
String type;
@DataField(pos = 6)
String code;
Class Editor
@DataField(pos = 8)
String name;
Remarks :
- @DataField annotation indicates the position of the value in the CSV record
- For this example we have one object Author (Charles, Moulliard) containing a list of 2 books ("Camel in action" and "Camel in action 2"). The first book has three Reference (ISBN 1234, ISBN 5678, ISBN 999) and the second one reference (ISBB 1111). Author contain also a list of two editors ("manning", "manning 2")
Here is an example and the result wanted
"firstName","lastName","age","title","year","type","code","name"
"charles","moulliard","camel in action","2009","ISBN","1234","manning","43"
"charles","moulliard","camel in action","2009","ISBN","1234","manning 2","43"
"charles","moulliard","camel in action","2009","ISBN","5678","manning","43"
"charles","moulliard","camel in action","2009","ISBN","5678","manning 2","43"
"charles","moulliard","camel in action","2009","ISBN","9999","manning","43"
"charles","moulliard","camel in action","2009","ISBN","9999","manning 2","43"
"charles","moulliard","camel in action 2","2011","ISBB","1111","manning","43"
"charles","moulliard","camel in action 2","2011","ISBB","1111","manning 2","43"
I have tried using a recursive function to put field values in Map of LinkedList : Map where Integer = position of the field in the CSV and Linkedlist = list of objects but I lost information about position of the element in the tree.
Regards,
Charles
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可能不明白你的问题。这样的事情行不通吗?
I probably don't understand your problem. Wouldn't something like this work?