prred
prred

Reputation: 131

Hibernate. how do we know java field name is keyword for that database

i was trying to write a simple program in Hibernate using Oracle as Backend.

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

}

when i try to run the program i was getting exception

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)

it took me 1 hr to figure out rownum is keyword in Oracle which cant be used as field name in java until i run create script in Sql developer. how can we know which field name shouldn't use in hibernate? is there any provision on what names should be used and what not in hibernate assuming that i don't have any DB specific knowledge

Upvotes: 1

Views: 472

Answers (1)

Stefan
Stefan

Reputation: 12462

No, but you can search for database keywords in general, most of them repeat in sql dialects and then for your database in particular. You can rename JPA columns with the @Column annotation, for example:

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

As you are using Hibernate, you may also try a different naming strategy:

hibernate.ejb.naming_strategy "An optional naming strategy. The default naming strategy used is EJB3NamingStrategy. You also might want to consider the DefaultComponentSafeNamingStrategy."

Upvotes: 2

Related Questions