如果使用双引号,则无法使用Hibernate插入行 [英] Unable insert row using Hibernate if double quotes are used

查看:97
本文介绍了如果使用双引号,则无法使用Hibernate插入行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为确保故障安全,我总是在映射类中使用双引号.这仅适用于 PostgreSQL

For fail safe am always using double quotes in my mapping class. And this only for PostgreSQL

这是我的课程:

@Entity
@Table(name="`Person`"
   ,schema="public"
)
public class Person  implements java.io.Serializable {

    private Integer id;
    private String name;
    private String address;

    public Person() {
    }


    @Id @GeneratedValue(strategy=IDENTITY)
    @Column(name="`ID`", nullable=false)
    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name="`Name`")
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name="`Address`")
    public String getAddress() {
        return this.address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

当我尝试使用Am插入以下异常时:

When am trying insert using Am getting below exception:

org.postgresql.util.PSQLException: The column name ID was not found in this ResultSet.

查询是否要插入是

insert into public."Person" ("Address", "Name") values (?, ?)

由于其他原因,我无法删除双引号.

I can't remove double quotes for other reasons.

请帮助我解决此问题.

更新: 数据库架构:

CREATE TABLE "Person"
(
    "ID" INTEGER PRIMARY KEY NOT NULL DEFAULT nextval('"Person_ID_seq"'::regclass),
    "Name" VARCHAR(255),
    "Address" VARCHAR(255)
);

推荐答案

如果需要引用所有内容,请在hibernate.properties或persistence.xml文件中添加以下标志

If everything need to be quoted, add the following flags to hibernate.properties or persistence.xml file

hibernate.globally_quoted_identifiers=true

并从Person类中删除所有单引号.但是,请注意,对于Postgresql,将表/列名称等放在双引号中实际上会将它们变成区分大小写.因此,数据库中表/列的大小写必须与@Table和@Column批注中的相应名称完全匹配.

and remove all the single quote from the class Person. However, take note that for Postgresql, placing table/column name etc in double quote effectively turn them into case sensitives. So the case for the table/column in the database must be exact match of the corresponding name in the @Table and @Column annotation.

这篇关于如果使用双引号,则无法使用Hibernate插入行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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