如何使用下划线映射类属性“_”在Spring-data JPA的名字中 [英] How to map class attributes with underscores "_" in their names in Spring-data JPA

查看:1591
本文介绍了如何使用下划线映射类属性“_”在Spring-data JPA的名字中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Spring-Data的JPA在其名称中带有下划线_的属性有问题吗?这是我的接口,它扩展了 JpaRepository

  public interface I_My_Class extends JpaRepository< ; MyClass,Long> {

MyClass findByA_my_table_id(Long headerId);

$ / code>

这行: findByA_my_table_id(Long headerId); 给出这个错误:


派生查询无效!没有找到类型为MyClass的属性a!

如果我将方法命名为 public MyClass findBya_my_table_id(Long headerId ); 它给了我同样的错误。如果我将没有下划线的属性命名为 amytableid ,我不会收到错误信息,但如果我这样做,稍后再阅读并不容易。这是我有table属性的类:

pre $实例
@Table(name =MyTable)
public class MyClass implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@Column(name =MY_TABLE_ID,nullable = false)
private长a_my_table_id; //< - 这是我试图放入命名查询中的属性


解决方案

是Spring数据在实体属性名称中存在下划线问题。原因是JpaRepository简单地期望具有适当的Java标准命名约定的属性,例如属性名称应该是小写。 (如果你可以添加多个名词,使它更具意义上的完整性,那么最好使第一个字母的大写字母除第一个以外的大写)

  String aMyTableId; 

上面的属性会创建一个告诉JpaRepository来创建一个像

 列表< MyClass> findByAMyTableId(String aMyTableId); 

这不会导致编译错误。

如果你想编写自定义查询,那么你可以使用@Query API。在这里你可以写出面向对象的查询。

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ MyClass myclass myclass where myclass.aMyTableId =?1 myclass .activeFlag ='1')
List< MyClass> findByAMyTableIdWithActiveFlagOn(String aMyTableId);

您可以找到许多教程和网站来解释如何编写自定义查询。

Does JPA with Spring-Data have a problem with attributes with underscores "_" in their names? This is my interface that extends JpaRepository:

public interface I_My_Class extends JpaRepository<MyClass, Long> {

    public MyClass findByA_my_table_id (Long headerId);
}

This line: findByA_my_table_id (Long headerId); gives this error:

Invalid derived query! No property "a" found for type MyClass !

If I name the method public MyClass findBya_my_table_id (Long headerId); it gives me the same error. If I name the attribute amytableid without the underscores I don't get the error but if I do that it's not easy to read later on. This is the class where I have the table attribute:

@Entity
@Table(name="MyTable")
public class MyClass implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column (name="MY_TABLE_ID", nullable=false)
    private Long a_my_table_id; // <-- this is the attribute that I try to put in the named query
}       

解决方案

Yes Spring Data will have problem with underscores in Entity attribute names. Reason being JpaRepository simply expects attributes with proper Java Standard naming conventions eg property names should be in lower case. (if you can add multiple nouns to make it more meaning full then better make first letter of nouns in upper case except the first one)

String aMyTableId;

Above property would create tell JpaRepository to create a method like

List<MyClass> findByAMyTableId(String aMyTableId);

This would not give compilation error.

In case you want to write custom queries then you can use @Query API. Here you can write Object oriented query.

@Query("Select myclass from MyClass myclass where myclass.aMyTableId=?1 and myclass.activeFlag='1'")
List<MyClass> findByAMyTableIdWithActiveFlagOn(String aMyTableId);

You can find many tutorials and sites explaining how to write custom queries.

这篇关于如何使用下划线映射类属性“_”在Spring-data JPA的名字中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆