我使用休眠调用数据库
List<Object> result = session.openSession().createSQLQuery(myNativeSQLQuery).list();
不,我可以遍历该列表
Iterator iter = result.iterator();
while(iter.hasNext()){
Object[] item = (Object[]) iter.next();
}
但是那个对象对我自己没有用,所以我想让那个对象映射到我实现的类上。
public class MyClass {
private Long id;
private String name
private Set<MySecondClass> myList
public void myFunc(){
// do sth with that id or any other attribute mapped
}
}
我不想使用休眠注释和东西,因为定义的类将由定义本机 sql 查询的 xml 文件填充数据。这样,我想将生成的文档具体化到 mongodb 中以进行快速数据访问。
您在下面看到的此类 xml 文件的示例
<?xml version="1.0" encoding="UTF-8"?>
<mapping>
<resource>myResource</resource> <!-- the resource table in mysql where the base query is executed -->
<destination>myDestination</destination> <!-- the destination collection in mongodb -->
<document mapped="MyClass"> <!-- that docuemnt and the base query will be mapped on the class "MyClass"
<base sql="select id, name from table1" />
<reference id="_ID_" column="id" /> <!-- use the result from the base query and replace the placeholder _ID_ of all following sql querys with the value of id in that current iteration -->
<lists>
<myList mapped="MySecondClass" sql="select col1, col2 from table2 where referenceId=_ID_" /> <!-- each element of that listed will be mapped on MySecondClass and fills the property "myList"
</lists>
</document>