JPA 大写表名 [英] JPA Uppercase table names

查看:26
本文介绍了JPA 大写表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Postgresql 中有一个表:

I have a table in 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 所以我使用了 @Table JPA 注释(见下文).这是我的application.propreties:

and I am trying Map on this Table So I used @Table JPA annotation (see below). This is my 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 得到 msg

If I have it like this @Table(name="UTILISATEUR") I get msg from PostgreSQL

ERREUR: la relation « utilisateur » n'existe pas 

当我尝试转义引用时 @Table(name=""UTILISATEUR"")

ERROR: syntax error at or near "`"

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

I tried the responses from this question but it doesn't work

Spring Boot JPA 插入 TABLE 中的大写名称和 Hibernate

我一直在搜索有关改进的Namingstrategy 它似乎是spring boot 中的一个问题,而不是我使用了EJB3NamingStrategy 和DefaultNamingStrategy 它不起作用当Hibernate 执行SQL 时结果很令人困惑:

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 :

<代码>休眠:选择utilisateu0_.IdUtilisateur如IdUtilis1_2_,utilisateu0_.Nom如Nom2_2_,utilisateu0_.Password如Password3_2_,utilisateu0_.Prenom如Prenom4_2_,utilisateu0_.Profil如Profil5_2_,utilisateu0_.Pseudo如Pseudo6_2_,utilisateu0_.id_role如id_role7_2_来自 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

还有 EJB3NamingStrategy

and also the EJB3NamingStrategy

这也是我这样使用注解的结果

This is also the result when I use the annotation like that

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

推荐答案

也许是因为你使用的是 MYSQL5DIALECT 有一个 Postgres Dialect 刚刚使用过像这样发布它并且为了改进的命名策略使用 EJB3 就像Spring Boot JPA 插入 TABLE 中的大写名称和 Hibernate

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

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

希望对你有用

这篇关于JPA 大写表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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