HQL选择new作为列

时间:2015-12-23 10:23:12

标签: hibernate hql

在HQL中是否可以将列作为对象?我的意思是这样的:

#app/controllers/products_controller.rb
class ProductsController < ApplicationController
   def new
      @product = Product.new
      @product.product_images.build
      @product.product_sizes.build
   end

   def create
      @product = Product.new product_params
      @product.save
   end

   private

   def product_params
      params.require(:product).permit(:x, :y, :z, product_images_attributes: [:image], product_sizes_attributes: [...])
   end
end

将结果转换为:

select p.name as name, new Property(p.color, p.brand) as properties from Product p

之前的查询无效:

public class ProductInfo {
    private String name;
    private Property properties;

    /* getters and setters */

}

所以我想知道是否有办法实现这一目标。

1 个答案:

答案 0 :(得分:0)

在这种情况下,快速解决方案是让Product然后继续创建ProductInfo。

请注意,它只适用于一个结果,如果您必须处理许多其他结果,此解决方案可能不是最佳选择。

相关问题