How to convert javassist instance into instance of a specific domain

北战南征 提交于 2019-12-06 05:28:55

It depends what you're actually trying to do. The intersect method ultimately relies on equals, so if you implement equals and hashCode in Bar then it will do what you want. But you shouldn't generally implement equals in terms of the object ID, as IDs are only assigned when an object is saved so you wouldn't be able to compare a newly-created object with a previously-saved one. Hibernate recommends that you implement it based on a business key (something that isn't the generated ID but which is stable and unlikely to change throughout the lifetime of the object)

class UserAccount {
  String username
  String realname

  public boolean equals(that) {
    return ((that instanceof UserAccount)
        && (this.username == that.username))
  }

  public int hashCode() {
    username.hashCode()
  }
}

So if you do want an ID comparison then it's clearer to do that explicitly.

def userBarIds = allBarsOfUser*.id
def intersectedBars = bars.findAll { it.id in userBarIds }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!