休眠。我们怎么知道java字段名是该数据库的关键字

时间:2015-08-04 06:51:16

标签: java hibernate

我正在尝试使用Oracle作为Backend在Hibernate中编写一个简单的程序。

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;

@Entity(name ="superclass_tab")
public class SuperClass {

    @Id
    @GeneratedValue(generator="FAMILY_SEQ", strategy=GenerationType.SEQUENCE)
    @SequenceGenerator(name="FAMILY_SEQ",sequenceName="FAMILY_SEQ",initialValue=5,allocationSize=1)
    int rownum;
    private String vehicleName;
    private String vehicleMake;
    private int yearManufacture;


    /**
     * @return the rownum
     */
    public synchronized int getRownum() {
        return rownum;
    }
    /**
     * @param rownum the rownum to set
     */
    public synchronized void setRownum(int rownum) {
        this.rownum = rownum;
    }
    /**
     * @return the vehicleName
     */
    public String getVehicleName() {
        return vehicleName;
    }
    /**
     * @param vehicleName the vehicleName to set
     */
    public void setVehicleName(String vehicleName) {
        this.vehicleName = vehicleName;
    }
    /**
     * @return the vehicleMake
     */
    public String getVehicleMake() {
        return vehicleMake;
    }
    /**
     * @param vehicleMake the vehicleMake to set
     */
    public void setVehicleMake(String vehicleMake) {
        this.vehicleMake = vehicleMake;
    }
    /**
     * @return the yearManufacture
     */
    public int getYearManufacture() {
        return yearManufacture;
    }
    /**
     * @param yearManufacture the yearManufacture to set
     */
    public void setYearManufacture(int yearManufacture) {
        this.yearManufacture = yearManufacture;
    }

}

当我尝试运行该程序时,我遇到异常

Aug 04, 2015 12:13:14 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1747, SQLState: 42000
Aug 04, 2015 12:13:14 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ORA-01747: invalid user.table.column, table.column, or column specification

Aug 04, 2015 12:13:14 PM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
INFO: HHH000010: On release of batch it still contained JDBC statements
org.hibernate.exception.SQLGrammarException: could not execute statement
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:80)

我花了1个小时才发现rownum是Oracle中的关键字,它不能用作java中的字段名,直到我在Sql开发人员中运行create script。我们怎么知道哪个字段名不应该在hibernate中使用?是否存在关于应该使用什么名称以及什么不在休眠状态的规定,假设我没有任何DB特定知识

1 个答案:

答案 0 :(得分:2)

不,但您可以搜索一般的数据库关键字,其中大多数在sql方言中重复,特别是在您的数据库中重复。您可以使用@Column注释重命名JPA列,例如:

 @Column(name = "sql_friendly_name")
 private String javaFriendlyName;

当您使用Hibernate时,您也可以尝试different naming strategy

  

hibernate.ejb.naming_strategy “可选的命名策略。使用的默认命名策略是EJB3NamingStrategy。您可能还需要考虑DefaultComponentSafeNamingStrategy。”