Changing naming strategy of @Table and @Column wit

2019-07-30 05:58发布

问题:

When defining a JPA Entity like this:

@Entity
@Table
public class CaseExample implements Serializable {
    @Id
    Long id;
    @Basic
    String fooBar;
}

the automatically created SQL table name is "CASEEXAMPLE" and the column name "FOOBAR". How can I change that from upper-case to lower-case-with-underscore e.g. "case_example" and "foo_bar" without having to add a name="foo_bar" to every single @Table and @Column?

Is the naming strategy defined by JPA or implemenation dependend? I use JPA 2.0 with EclipseLink 2.5.0.

回答1:

JPA standardizes the names. I would leave them using the standard, or use @Column to change specific ones.

With EclipseLink you could modify you column names using your own code in a DescriptorCustomizer or SessionCustomizer. You would just iterate over your descriptor's mapping and reset the fieldNames based on your naming convention.