小编典典

JPA大写表名称

spring-boot

我在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,
  )

并且我正在此表上尝试Map,所以我使用了@TableJPA注释(请参见下文)。这是我的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

最后这是我的实体类:

@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;

如果我有这样的话,@Table(name="UTILISATEUR")我会从PostgreSQL获取消息

ERREUR: la relation « utilisateur » n'existe pas

当我尝试转义报价 @Table(name="\"UTILISATEUR\"")

ERROR: syntax error at or near "`"

我尝试了这个问题的答案,但是没有用

更新

我一直在寻找关于EnhancedNamingstrategy的问题,这似乎在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_

就像它知道表名是大写字母,但不想映射,或者我不知道这是什么错误,这是使用的结果

spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.DefaultNamingStrategy
or 
spring.jpa.hibernate.naming_strategy = org.hibernate.cfg.DefaultNamingStrategy

还有EJB3NamingStrategy

这也是我使用注释时的结果

@Table(name="UTILISATEUR")

我也尝试过

@Table(name="\"UTILISATEUR\"")

我得到了问题

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_

终于这是我的 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>

阅读 470

收藏
2020-05-30

共1个答案

小编典典

也许是因为您使用的是MYSQL5DIALECT,所以使用了Postgres方言就可以了,对于这种改进的命名策略,请使用EJB3,例如

spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect

希望这对你有用

2020-05-30