postgresql JPA 大写表名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36353492/
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 Uppercase table names
提问by Kamel Mili
I have a table in Postgresql:
我在 Postgresql 中有一个表:
CREATE TABLE "UTILISATEUR"(
"IdUtilisateur" serial NOT NULL,
"Nom" character varying(50),
"Prenom" character varying(50),
"Profil" character varying(50),
"Pseudo" character varying(20),
"IdSite" integer DEFAULT 0,
"Password" character varying(1024),
id_role integer,
)
and I am trying Map on this Table So I used @Table
JPA annotation (see below). This is my application.propreties
:
我正在这个表上尝试 Map 所以我使用了@Table
JPA 注释(见下文)。这是我的application.propreties
:
spring.datasource.url = jdbc:postgresql://localhost/baseecu
spring.datasource.username = postgres
spring.datasource.password =root
spring.datasource.driverClassName=org.postgresql.Driver
spring.jpa.database = MYSQL
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
and finaly this is my entity class:
最后这是我的实体类:
@Entity
@Table(name="UTILISATEUR")
public class Utilisateur {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="IdUtilisateur")
public Long id ;
public String Nom ;
public String Prenom ;
public String Profil ;
public String Pseudo ;
public String Password ;
@ManyToOne
@JoinColumn(name="id_role")
public Role role ;
public Long getId() {
return id;
If I have it like this @Table(name="UTILISATEUR")
I get msg from PostgreSQL
如果我有这样的信息,@Table(name="UTILISATEUR")
我会从 PostgreSQL 收到 msg
ERREUR: la relation ? utilisateur ? n'existe pas
When I try to escape quote @Table(name="\"UTILISATEUR\"")
当我试图逃避报价时 @Table(name="\"UTILISATEUR\"")
ERROR: syntax error at or near "`"
I tried the responses from this question but it doesn't work
我尝试了这个问题的回答,但它不起作用
Spring boot JPA insert in TABLE with uppercase name with Hibernate
Spring Boot JPA 在 TABLE 中插入带有 Hibernate 的大写名称
update
更新
I've been searching about ImprovedNamingstrategy it's seems like an issue in spring boot instead I've used EJB3NamingStrategy and also DefaultNamingStrategy it's not working what funny it's when Hibernate execute the SQL the result is pretty confusing :
我一直在搜索改进的Namingstrategy它似乎是spring boot中的一个问题,而不是我使用了EJB3NamingStrategy和DefaultNamingStrategy它不起作用当Hibernate执行SQL时结果非常令人困惑:
Hibernate: select utilisateu0_.IdUtilisateur as IdUtilis1_2_, utilisateu0_.Nom as Nom2_2_, utilisateu0_.Password as Password3_2_, utilisateu0_.Prenom as Prenom4_2_, utilisateu0_.Profil as Profil5_2_, utilisateu0_.Pseudo as Pseudo6_2_, utilisateu0_.id_role as id_role7_2_ from UTILISATEUR utilisateu0_
It's like it knows the table name is in UPPERCASE but does not want to map or I don't know what wrong with it this is the result of using
就好像它知道表名是大写的,但不想映射,或者我不知道它有什么问题,这是使用的结果
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.DefaultNamingStrategy
or
spring.jpa.hibernate.naming_strategy = org.hibernate.cfg.DefaultNamingStrategy
and also the EJB3NamingStrategy
以及 EJB3NamingStrategy
This is also the result when I use the annotation like that
这也是我使用这样的注释时的结果
@Table(name="UTILISATEUR")
and I also tried like that
我也这样试过
@Table(name="\"UTILISATEUR\"")
and I get the issue
我明白了这个问题
ERROR: syntax error at or near "`"
Hibernate: select utilisateu0_.IdUtilisateur as IdUtilis1_2_, utilisateu0_.Nom as Nom2_2_, utilisateu0_.Password as Password3_2_, utilisateu0_.Prenom as Prenom4_2_, utilisateu0_.Profil as Profil5_2_, utilisateu0_.Pseudo as Pseudo6_2_, utilisateu0_.id_role as id_role7_2_ from `UTILISATEUR` utilisateu0_
finally this is my pom.xml
最后这是我的 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xx.MDValidation</groupId>
<artifactId>xx.MDValidation</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>xx.MDValidation</name>
<description>Projet Validation xx</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<hibernate.version>4.2.21.Final</hibernate.version>
<commons-dbcp.version>1.2.2</commons-dbcp.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jooq</artifactId>
</dependency>
<dependency>
<groupId>bsf</groupId>
<artifactId>bsf</artifactId>
<version>2.4.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
采纳答案by amani92
maybe because you are using MYSQL5DIALECT there's a Postgres Dialect just used post it like this and for the improved naming strategy use EJB3 like Spring boot JPA insert in TABLE with uppercase name with Hibernate
也许是因为您使用的是 MYSQL5DIALECT 有一个 Postgres 方言只是使用了这样的帖子,并且为了改进的命名策略使用 EJB3,如 Spring boot JPA insert in TABLE with largecase name with Hibernate
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
hope it works for you
希望这对你有用
回答by riskop
I reproduced your UTILISATEUR table (role removed) in postgres 8.4 and hibernate 5.0.3.
我在 postgres 8.4 和 hibernate 5.0.3 中复制了您的 UTILISATEUR 表(删除了角色)。
It works as expected with explicit table and column names annotation:
它与显式表名和列名注释一起按预期工作:
@Entity(name="\"UTILISATEUR\"")
public class Utilisateur {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="\"IdUtilisateur\"")
private Long id ;
@Column(name="\"Nom\"")
private String Nom ;
@Column(name="\"Prenom\"")
private String Prenom ;
@Column(name="\"Profil\"")
private String Profil ;
@Column(name="\"Pseudo\"")
private String Pseudo ;
@Column(name="\"Password\"")
private String Password ;
... getter / setters
}
回答by German Andres Ariza Casta?eda
You can configure your application with the next line depend the database:
您可以使用下一行配置您的应用程序,具体取决于数据库:
MySql
数据库
spring.jpa.hibernate.naming_strategy = org.hibernate.cfg.DefaultNamingStrategy
Postgres
Postgres
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
Oracle
甲骨文
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
回答by CherOliv
I faced the same issue and I back quoted ( ` ) my table name in the entity declaration like this
"`UTILISATEUR`".
@Table(name = "
`UTILISATEUR`")
我遇到了同样的问题,我在实体声明中重新引用了(`)我的表名,比如“`UTILISATEUR`”。
@Table(name = "
`实用程序`")
` using postgresql 11 with dialect : org.hibernate.dialect.PostgreSQL95Dialect it generated me an upercased table name on database.
` 使用带有方言的 postgresql 11 :org.hibernate.dialect.PostgreSQL95Dialect 它在数据库上为我生成了一个大写的表名。