Dilip Kumar
Dilip Kumar

Reputation: 2534

Unable insert row using Hibernate if double quotes are used

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

Here is my class:

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

When am trying insert using Am getting below exception:

org.postgresql.util.PSQLException: The column nameIDwas not found in this ResultSet.

Query it ran for insert was:

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

I can't remove double quotes for other reasons.

Please help me to get this problem fixed.

Update: Database Schema:

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

Upvotes: 2

Views: 2620

Answers (1)

Dukefirehawk
Dukefirehawk

Reputation: 476

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

hibernate.globally_quoted_identifiers=true

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.

Upvotes: 5

Related Questions