【问题标题】:Spring Jpa Hibernate Column not translated to querySpring Jpa Hibernate Column 未转换为查询
【发布时间】:2020-07-23 13:22:23
【问题描述】:

JPA 不会在 sql 语句中翻译 @Column(name="...") 错误日志

[6418c947-1] There was an unexpected error (type=Internal Server Error, status=500).
PreparedStatementCallback; bad SQL grammar [SELECT mwo_main_status._id AS _id, mwo_main_status.oid AS oid, mwo_main_status.label AS label, mwo_main_status.details AS details FROM mwo_main_status]; nested exception is org.postgresql.util.PSQLException: ERROR: column mwo_main_status.oid does not exist Dica: Perhaps you meant to reference the column "mwo_main_status._id". Posição: 36

我的 ORM

@Builder
@Data
@Entity
@Table("mwo_sub_status")
public class WorkOrderSubStatusDbMapping {

    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private @Id Integer _id;

    @Column(name="ref_id")
    private Integer oid;
    private String label;
    private String details;
}

我希望生成的 SQL 是:

SELECT mwo_main_status._id AS _id, mwo_main_status.ref_id AS oid, mwo_main_status.label AS label, mwo_main_status.details AS details FROM mwo_main_status

我正在创建一个带有子模块(Web、域、持久性)的 Spring-Boot 应用程序
在持久性模块中,我的 gradle.build 文件是

apply plugin: 'java-library'

dependencies {
    implementation project(':domain')
    implementation 'io.projectreactor:reactor-core:3.3.3.RELEASE'
    implementation 'org.springframework:spring-context:5.2.4.RELEASE'

    implementation 'org.springframework:spring-orm:5.2.4.RELEASE'
    implementation 'org.springframework.data:spring-data-jdbc:1.1.5.RELEASE'

    implementation 'org.hibernate:hibernate-core:5.4.11.Final'
    runtimeOnly('org.postgresql:postgresql')

    runtimeOnly('org.postgresql:postgresql')

    testImplementation 'junit:junit:4.12'
}

我错过了什么?


正如Using lomboks @Data and @Builder on entity 中所说,Lombok 不适用于 jdbc,因此我决定使用 spring-data-jpa 而不是 spring-data-jdbc

谢谢Alex Rudenko

【问题讨论】:

  • 在 WorkOrderSubStatusDbMapping.class 中使用@Entity 注解
  • 其他注释有用吗?您是否也在项目中使用 xml 进行持久性配置?

标签: java spring hibernate jpa


【解决方案1】:

可能是这样的

@Entity
@Table(name = "mwo_main_status")
public class WorkOrderSubStatusDbMapping {
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Integer _id;

    @Column(name = "ref_id")
    private Integer oid;
    private String label;
    private String details;    
}

【讨论】:

  • 我忘记复制注释了,抱歉。而且不是@Entity =/
  • 不建议将 lombok 的注释与 JPA @Entity 混合使用,也许您需要按照此处的建议更新它们:stackoverflow.com/questions/40516058/…
猜你喜欢
  • 2012-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-22
  • 2018-03-13
  • 2015-10-10
  • 1970-01-01
  • 2018-03-17
相关资源
最近更新 更多