Search code examples
postgresqlhibernatehibernate-enversspring-data-envers

Envers + Spring JPA: revinfo does not allow 'user' column


I set up Spring JPA + Envers (PostgreSQL) and annotated some entity classes with @Audited. I added my revinfo table because I want to log the account a change came from:

@Entity(name = "Audit")
@Table(name = "revinfo")
@RevisionEntity(AuditRevisionListener::class)
class AuditRevisionEntity(

    @Id
    @GeneratedValue
    @RevisionNumber
    var rev : Int = 0,

    @RevisionTimestamp
    var timestamp: Long = 0,

    var username: String      <<< CULPRIT
)

The above code snipped works! BUT - the moment I rename username to user everything crashes and burns. I receive a cryptic message

ERROR: syntax error at or near "user" at character 33
STATEMENT: insert into revinfo (timestamp, user, rev) values ($1, $2, $3)

My first assumption was that my types don't match, so I verified against my liquibase setup

<changeSet id="create-revinfo-table" author="me">
    <createTable tableName="revinfo">
        <column name="rev" type="SERIAL" autoIncrement="true">
            <constraints primaryKey="true" primaryKeyName="revinfo_pkey"/>
        </column>
        <column name="timestamp" type="bigint"/>
        <column name="username" type="VARCHAR(255)"/>
    </createTable>
</changeSet>

The moment I rename username to user, it again crashes with the above mentioned error message.

Why can I not use a column called user in revinfo?
Are there any more of these magic column names?
Where do I find information about this behavior?


Solution

  • user is a special keyword in PostgreSQL.

    I would pick a different name but if you feel strong about it you can quote the name with @Column:

    @Column(name = "`user`")
    var username: String