Mapping Reserved Words as Entity Fields in JPA
JPA's use of SQL can conflict with reserved words in database dialects. To address this issue, let's explore how to map an entity field with a reserved word while avoiding the common "use your own dialect" solution.
Hibernate's Approach
Hibernate allows you to escape reserved words within backticks (`). For JPA 1.0, use:
@Column(name="`open`")
Copy after login
For JPA 2.0 and later, the syntax is:
@Column(name="\"open\"")
Copy after login
References
- [Hibernate Reference Guide](https://docs.jboss.org/hibernate/stable/orm/reference/en/html_single/#d0e3171)
- [5.4. SQL Quoted Identifiers](https://docs.jboss.org/hibernate/stable/orm/reference/en-US/html/dialect-options.html#d0e3171)
- [JPA 2.0 Specification](https://docs.oracle.com/javaee/7/api/javax/persistence/Column.html#name())
- [2.13 Naming of Database Objects](https://docs.oracle.com/javaee/7/api/javax/persistence/Table.html#name())
Additional Resources
- [Hibernate, MySQL, and "Repeat" Table](https://stackoverflow.com/questions/18211310/hibernate-mysql-and-table-named-repeat-strange-behavior)
The above is the detailed content of How Can I Map JPA Entity Fields with Database Reserved Words?. For more information, please follow other related articles on the PHP Chinese website!