0

hibernateを使用してデータベースを呼び出しています

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ファイルによってデータで満たされるため、Hibernateアノテーションなどを使用したくありません。そうすれば、結果のドキュメントを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>

4

1 に答える 1

0

その結果、オブジェクトをコピー/マップする必要はありません。アノテーションorg.springframework.data.mongodb.core.mapping.Documentと春のデータからの抽象クラスcom.mongodb.BasicDBObjectを利用できます。

@org.springframework.data.mongodb.core.mapping.Document
public class BasicDocument extends BasicDBObject {

}

その後、私はただ行うことができました

BasicDocument doc = new BasicDocument();
doc.put("key", "value");

そのドキュメントをmongodbに保存します。そうすれば、リストを作成することもできます。

于 2012-10-16T09:38:45.363 に答える