带有不正确下划线的JPA列 [英] JPA column with incorrect underscore

查看:513
本文介绍了带有不正确下划线的JPA列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JPA进行数据库访问,并使用正确的名称为每列添加注释。现在,如果我执行查询(例如findAll()),它将返回

 '字段列表'中的未知列'program0_.program_id' 

错误信息正确 program_id 未知因为真名是 programId

  @实体
@Table(name =programs)
@XmlRootElement
public class Program实现Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(可选的= false)
@Column(name =programId)
private long programId;
@ManyToMany
@JoinTable(
name =programlabels,
joinColumns = {
@JoinColumn(name =program,referencedColumnName =programId)} ,
inverseJoinColumns = {
@JoinColumn(name =label,referencedColumnName =labelId)})
private Collection< Label>标签;
}

标签

  @Entity 
@Table(name =labels)
@XmlRootElement
public class Label实现Serializable {
@Id
@Basic(可选= false)
@NotNull
@Size(min = 1,max = 100)
@Column(name =labelId)
private String labelId;
}

查询

 选择program0_.program_id作为program_1_5_,... 

是否有原因让JPA将programId更改为program_id或者我缺少配置?

谢谢

编辑:哦,对不起忘了添加查询代码/信息。

我使用Spring Data的JpaRepository接口并尝试findAll()查询。 / p>

  @Repository 
公共接口ProgramRepository扩展JpaRepository< Program,Long> {}


解决方案

spring-boot-jpa-column-name-annotation-ignored ,你的列名正在被转换以蛇的情况。



可能的解决方案:


  • 设置命名策略

    li>
  • 在注释中使用小写字母名称


I use JPA for database access and annotated every column with the correct name. Now if I execute a query (e.g. findAll()) it returns

Unknown column 'program0_.program_id' in 'field list'

The error message is correct program_id is unknown because the real name is programId.

Models: Program

  @Entity
  @Table(name = "programs")
  @XmlRootElement
  public class Program implements Serializable {
          @Id
          @GeneratedValue(strategy = GenerationType.IDENTITY)
          @Basic(optional = false)
          @Column(name = "programId")
          private Long programId;
          @ManyToMany
          @JoinTable(
                  name = "programlabels",
                  joinColumns = {
                    @JoinColumn(name = "program", referencedColumnName = "programId")},
                  inverseJoinColumns = {
                    @JoinColumn(name = "label", referencedColumnName = "labelId")})
          private Collection<Label> labels;
        }

Label

@Entity
@Table(name = "labels")
@XmlRootElement
public class Label implements Serializable {
  @Id
  @Basic(optional = false)
  @NotNull
  @Size(min = 1, max = 100)
  @Column(name = "labelId")
  private String labelId;  
}

Query

select program0_.program_id as program_1_5_, ...

Is there a reason why JPA changes "programId" to "program_id" or am I missing any configuration?

thanks

Edit: Oh sorry forgot to add query code/information.

I use the Spring Data's JpaRepository interface and tried the findAll() query.

@Repository
public interface ProgramRepository extends JpaRepository<Program, Long> {}

解决方案

As described in spring-boot-jpa-column-name-annotation-ignored, your column name is being converted to snake case.

Possible solutions:

  • Setup a Naming Strategy
  • Use lowercase column names in your annotations

这篇关于带有不正确下划线的JPA列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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