我在Grails中有两个实体,它们之间具有一对多的关系Location和LocationImage

class Location {

  Long phone;
  String url;

  static hasMany = [locationImage: LocationImage]

  static constraints = {
    phone(blank: true, nullable: true)
    url(blank: true, nullable: true)
    locationImage(nullable: true)
  }
}




 class LocationImage {

    String description
    String path

    static belongsTo = [location:Location]


   static constraints = {
    description(blank:true,nullable:true)
}

}

如何通过外键位置ID在locationImage表中找到路径属性?

最佳答案

好,我解决了,这就是我需要的

 def getLocationImage(String locationName){
   def location = Location.findByName(locationName)
   def locationImage = LocationImage.createCriteria().list{
      and {
      eq('location',location)
   }
   }
    return locationImage.path
  }

07-26 09:30