2010-12-02 45 views
4

我有2类:创建Hibernate对象的选择

class A { 
B b; 

public A() {} 
public A(B b) { setB(b); } 
... 
} 

class B { 
int id; 
public B(int id) { setId(id); } 
} 

在HQL我想选择这样的:

select new A(new B(a.b.id)) from A a 

,但我得到了错误

org.hibernate作为。 hql.PARSER - 行1:48:意外标记:,

是否可以在参数中创建对象,或者只选择字段并在构造函数中创建它?

+0

你究竟想要什么?为什么你需要在这里使用`new`,你不能只是'SELECT a ...'吗?这个类中的 – axtavt 2010-12-02 12:56:20

+0

是更多的字段,我不想选择它们。 – Dainius 2010-12-02 13:01:01

回答

1

不知道我是否完全明白你想达到什么。但是你可以在创建一个HQL查询(用投影)为你intereset列只能查询,如:

select a.whatever, b.id from A a join a.b b 

之后,您提供的接口ResultTransformer的实现,并将其与query.setResultTransformer(yourTransformer)设为您的query对象

你执行的结果变压器负责为AB

1

创建实例也许考虑编写自己的功能来构建你的查询和使用StrinBuilder类 - 使用hql.append我不写出查询!

import org.hibernate.Hibernate; import org.hibernate.search.FullTextQuery; import org.hibernate.search.FullTextSession; import org.hibernate.search.Search;

不知道它是否有帮助,否则去检查出cyclos网络应用程序 - 它包括大量的查询和根据Java文件。其开放源代码并使用全面休眠

关于