【问题标题】:How to get object list from objects that's got two nested objects in hibernate?如何从在休眠中有两个嵌套对象的对象中获取对象列表?
【发布时间】:2018-09-06 08:49:19
【问题描述】:

我有一个 Product、Location 和 ProductLocation 表。检索 Location 对象后,我希望获取属于该位置的所有产品的列表。我尝试设置一个标准,但它返回的是 ProductLocation 对象而不是产品列表。我的问题是如何从 ProductLocation 对象中获取产品列表?

List<Product> findByLocation(Location location){

        def criteria = ProductLocation.where {
            eq('location', location)

        }
        criteria.list()
    }



class Product {

        String title
        String imagepath
        String description
        Category category

        static belongsTo = [locations: Location]

        static mapping = {
            version false
        }

    static constraints = {
        title nullable: false
        imagepath nullable: false
        description nullable: true
    }

    static hasMany = [locations: Location]


}

class ProductLocation {

    Product product
    Location location


    static mapping = {
        version false
    }

    static constraints = {

    }

    static belongsTo = [products: Product, locations: Location]


}

class Location  {

    Company customer
    Location parent
    String name
    String description
    String objectnumber
    String address
    String zipcode
    String province
    String city
    String country
    Long longitude
    Long latitude
    Integer order
    String timezone
    AlertConfiguration alertConfiguration
    boolean deleted
    LocationType type


    static hasMany = [products: Product]

    static mapping = {

    }


}

【问题讨论】:

    标签: hibernate groovy


    【解决方案1】:

    尝试以下方法:

    List<Product> findByLocation(Location location){
        ProductLocation.createCriteria().list {
            eq('location', location)
            projections {
                property( 'product' )
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      • 2017-04-08
      • 1970-01-01
      • 1970-01-01
      • 2018-10-12
      相关资源
      最近更新 更多