Java 带有不正确下划线的 JPA 列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28544541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPA column with incorrect underscore
提问by KenavR
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
我使用 JPA 进行数据库访问,并用正确的名称注释每一列。现在,如果我执行查询(例如findAll()
),它会返回
Unknown column 'program0_.program_id' in 'field list'
The error message is correct program_id
is unknown because the real name is programId
.
错误信息是正确program_id
的未知,因为真实姓名是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?
JPA 是否有理由将“programId”更改为“program_id”,或者我是否缺少任何配置?
thanks
谢谢
Edit: Oh sorry forgot to add query code/information.
编辑:哦对不起忘记添加查询代码/信息。
I use the Spring Data's JpaRepository
interface and tried the findAll()
query.
我使用 Spring Data 的JpaRepository
接口并尝试了findAll()
查询。
@Repository
public interface ProgramRepository extends JpaRepository<Program, Long> {}
采纳答案by David SN
As described in spring-boot-jpa-column-name-annotation-ignored, your column name is being converted to snake case.
如spring-boot-jpa-column-name-annotation-ignored 中所述,您的列名正在转换为蛇形大小写。
Possible solutions:
可能的解决方案:
- Setup a Naming Strategy
- Use lowercase column names in your annotations
- 设置命名策略
- 在注释中使用小写列名
回答by Mohammed Rafeeq
http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
spring.jpa.hibernate.naming.strategy is not a supported property for Spring JPA implementation using Hibernate 5.
spring.jpa.hibernate.naming.strategy 不是使用 Hibernate 5 的 Spring JPA 实现支持的属性。
Use the below property in application.properties
使用以下属性 application.properties
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
回答by Shuchita Bora
Were able to map
能够映射
@Column(name = "PersonFullName")
@Column(name = "PersonFullName")
private String PersonFullName;
私人字符串 PersonFullName;
to the database table column name "PersonFullName" without the underscore.
到不带下划线的数据库表列名“ PersonFullName”。
The below worked for me. Add this in the application settings and then use @Column to specify the physical database column name for the model's property.
以下为我工作。在应用程序设置中添加它,然后使用@Column 为模型的属性指定物理数据库列名称。
@Column(name = "PersonFullName")
@Column(name = "PersonFullName")
In Application.properties
在Application.properties 中
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
回答by Vivek Goel
use below in application.properties
在 application.properties 下面使用
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl