【发布时间】:2020-07-18 03:23:28
【问题描述】:
我有一个实体 Person,它有一个属性 Name。名称未标记为实体。对于 Name 类,我有一个 AttributeConverter,它通过名字和姓氏的字符串连接创建一个全名。所以对于 person 实体,列名的类型是 String。
@Entity
public class Person {
// ...
@Convert(converter = NameAttributeConverter.class)
@Column
private Name name;
// ...
}
public final class Name implements Comparable, Serializable {
// ...
private String firstName;
private String lastName;
// ...
}
这行得通。但现在我想扩展我的个人存储库。我想按她的姓查找人。但是
List<Person> findByName_LastName(String lastName);
或
List<Person> findByNameLastName(String lastName);
不起作用。我得到以下异常:
PersonRepository.findByName_LastName(java.lang.String)! Illegal attempt to dereference path source [null.name] of basic type
我该如何解决这个问题?谢谢和问候
编辑:
public interface PersonRepository extends CrudRepository<Person, Long>{
// The repository works without the findByName...
// List<Person> findByName_LastName(String lastName);
}
【问题讨论】:
-
显示您的个人存储库的其余部分。这也是一个数据库实体吗?
-
这行不通。为什么不将 Name 定义为可嵌入的?
-
@SimonMartinelli 由于 AttributeConverter(我将其添加到代码示例中),这已经可以在没有嵌入的情况下工作。 findAll 之类的标准查询也有效。但是 findByNameLastName 不起作用...
-
是的,为此您应该使用可嵌入的。这就是我要说的
-
@SimonMartinelli 你的意思是我应该把 Embeddable 放到 AttributeConverterClass 中?
标签: java spring jpa spring-data-jpa