0
假設我正在MyEntity
表中name
字段中的表必須是唯一的(但不是主鍵)。使用QueryDSL查找唯一字段的實體
我想通過使用QueryDSL來定義findByName
方法。
我目前的執行情況如下:
public MyEntity findByName(final String name) {
JPAQuery query = new JPAQuery(this.entityManager);
QMyEntity myEntity;
List<MyEntity> result = jpaQuery.from(myEntity).where(myEntity.name.eq(name)).list(myEntity);
if (result.isEmpty())
throw new EntityNotFoundException();
else if (result.size() == 1)
return result.get(0);
else
throw new PersistenceException();
}
是我爲了一個妥善的解決辦法來實現這個任務,或是否有更好的辦法?