Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将本机java对象映射到自己实现的类_Java_Hibernate_Object_Mapping - Fatal编程技术网

将本机java对象映射到自己实现的类

将本机java对象映射到自己实现的类,java,hibernate,object,mapping,Java,Hibernate,Object,Mapping,我使用hibernate调用数据库 List<Object> result = session.openSession().createSQLQuery(myNativeSQLQuery).list(); 但是这个对象对我自己没有用处,所以我想把这个对象映射到我实现的类上 public class MyClass { private Long id; private String name private Set<MySecondClass>

我使用hibernate调用数据库

List<Object> result = session.openSession().createSQLQuery(myNativeSQLQuery).list();
但是这个对象对我自己没有用处,所以我想把这个对象映射到我实现的类上

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
    }   
}
公共类MyClass{
私人长id;
私有字符串名
私有集myList
public void myFunc(){
//用该id或任何其他属性做某事
}   
}
我不想使用hibernate注释之类的东西,因为定义的类将由定义本机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>

我的资源
我的目的地

结果是我不需要复制/映射对象。我只需要使用spring数据中的注释org.springframework.data.mongodb.core.mapping.Document和abtract类com.mongodb.basicdbobobject

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

}
在那之后,我就可以创作了

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

并将该文档存储到mongodb中。这样,我甚至可以创建列表。

不清楚您想要实现什么。“将对象映射到类上”是什么意思?我从Hibernate获得的对象的类型为object[],其中所有列都可用。因此,从查询“select id,name from table1”中,我将在每次迭代中得到一个对象[]obj,其中列“id”在obj[0]处可用,但我想将其映射到id属性Y上,我知道select语句会生成对象数组。“将其映射到id属性”对您意味着什么?如果您希望Hibernate返回类的实例,您应该在“from MyClass where…”模式下使用HQL。否则,您必须自己将对象数组复制到bean中。
BasicDocument doc = new BasicDocument();
doc.put("key", "value");