java如何定制springdata存储库方法名称?
我有很多实体在字段名之前使用下划线前缀,或者使用camelcase
@Entity
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long _id;
private String _firstName;
private String _lastName;
@OneToOne
private Foo _foo;
// … methods omitted
}
存储库
public interface CustomerRepository extends PagingAndSortingRepository<Customer, Long> {
Iterable<Customer> findByFoo(Foo foo);
}
表中的相应字段也使用此命名方案:
Customer: _id, _firstName, _lastName, _foo__id
现在,我正在将此项目迁移到spring数据,我得到了许多IllegalArgumentException,包括:
Could not create query metamodel for method
public abstract java.lang.Iterable
com.example.repository.CustomerRepository.findByFoo(com.example.model.Foo)!
Unable to locate Attribute with the given name [foo] on this ManagedType [com.example.model.Customer]
我不需要更改hibernate的命名策略,但是如何将查询方法生成命名算法更改为映射“findByFoo”->;实体上的“_foo”,或在JPQL术语中的“其中x._foo_id=?1”
我使用的是老式的xml配置,没有spring引导
Edit:找到此in the docs,这没有帮助强>
"As we treat underscore as a reserved character we strongly advise to follow standard Java naming conventions (i.e. not using underscores in property names but camel case instead)."
也许我应该重构字段名,删除下划线,然后实现一个hibernate命名策略,将下划线添加回
# 1 楼答案
我可以重复文档中的内容(尽管我对“没有帮助”部分感兴趣)。对Java代码使用标准Java约定。使用特定于存储区的方法自定义属性映射到数据库表的方式。JPA为此提供了
@Column
注释