Hibernate entity only one column, no name

让人想犯罪 __ 提交于 2019-12-10 11:41:01

问题


I want to map one column, without using a column name.

I am using a count entity, and the want to use mulple different queries with the same entity :

@Entity
public class CountDTO extends Number {

    @Id
    // below causes an error in my test, hsql not same syntax
    @Column(name = 'COUNT') 
    private Long count;

In my prod (oracle) database I can just do select count() as COUNT from ... however, the same syntax doesn't work using hypersql in-memory db ?

Is their an oracle/hsql compatible way to map a single column alias in HQL ?


回答1:


Your issue is that COUNT is a reserved keyword for HSQL, but not for Oracle.

According to the HSQL documentation, it might still be possible to use COUNT as identifier, if you either

  • Mask it as described in the Hibernate documentation or in the JPA spec (cf. chapter 2.13 from the JPA 2 spec; you'll need to accept their license agreement). Note that the JPA spec speaks of double quotes whereas the Hibernate documentation mentions backticks (which will be converted to the appropriate character according to the database dialect in use).

    From the hibernate documentation:

    You can force Hibernate to quote an identifier in the generated SQL by enclosing the table or column name in backticks in the mapping document. Hibernate will use the correct quotation style for the SQL Dialect. This is usually double quotes, but the SQL Server uses brackets and MySQL uses backticks.

    From the JPA 2 spec:

    Using annotations, a name is specified as a delimited identifier by enclosing the name within double quotes, whereby the inner quotes are escaped, e.g., @Table(name="\"customer\"").

  • Configure HSQL to allow it by executing SET DATABASE SQL NAMES FALSE (however, this should already be the default setting and it will only allow "the use of most keywords", not all - edit: COUNT will still be disallowed as per documentation)

My recommendation would be to avoid using identifiers if possible as you never know what problems may arise elsewhere (e.g. one might think Hibernate would be able to mask keywords itself) and use something like COUNT1 instead as column name.

The above part of the JPA spec also explains why Hibernate does not mask the name itself:

By default, the names of database objects must be treated as undelimited identifiers and passed to the database as such.

The JPA spec also mentions a <delimited-identifiers/> option "to specify that all database identifiers in use for a persistence unit be treated as delimited identifiers", but this seems to be only usable with an XML mapping file.



来源:https://stackoverflow.com/questions/31247046/hibernate-entity-only-one-column-no-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!