Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Defaults for @Table and @Column names

Hi Nate,

    EclipseLink implements the defaults according to the JPA spec but you can use a customizer to override those defaults.   I've attached a quickly thrown together example.  It doesn't cover all the cases but should give you an idea of what you can do.  In my example I prefixed all table names with "T_" but you can do the camel to underscore conversion instead.

   The essential idea is to use a session customizer to change the default table names after EclipseLink has set them.  The customerizer is "model.MyCustomizer" and it's enabled by adding
           <property name="eclipselink.session.customizer" value="model.MyCustomizer"/>
 to the persistence.xml.

    I'm not sure if the JPA 2.0 spec will support a pluggable or configurable Java name/Database naming strategy but I've passed your email on to Mike Kieth who's on the JPA expert group.


natjohns wrote:

Does anyone know if there is a way to change the way that JPA (or the eclipselink implemenation) generates default names for tables and columns when using the @Table and @Column annotations?  Or if this is being talked about for version 2 of the JPA spec?

What I'd like is something similar to what Grails does, where they change the camel capped Java names to underscores.  Here's an example:

Say you have an EmployeeInfo class and one of the members is defaultHomePhoneNumber.

Current Default in JPA:


It's kind of hard to read.  If we could specify a policy, say the camel caps to underscore policy, it would look like this:

Table name:  EMPLOYEE_INFO

Thanks all!  And I apoligize if this has been talked about already... I couldn't find anything in the searches.

eclipselink-users mailing list


Shaun Smith | Principal Product Manager, TopLink | +1.905.502.3094
Oracle Fusion Middleware
110 Matheson Boulevard West, Suite 100
Mississauga, Ontario, Canada L5R 3P4
package model;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import model.User;

public class Demo {

	public static void main(String[] args) {
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("demo");
		try {
			EntityManager em = emf.createEntityManager();
			User user = new User();
		} finally {

package model;
import java.util.Collection;
import java.util.Map;
import java.util.Vector;

import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.internal.helper.Helper;
import org.eclipse.persistence.internal.sessions.factories.SessionCustomizer;
import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.mappings.DirectToFieldMapping;
import org.eclipse.persistence.sessions.Session;

public class MyCustomizer implements SessionCustomizer {

	public void customize(Session session) throws Exception {
		Map<Class<?>, ClassDescriptor> descs = session.getDescriptors();
		Collection<ClassDescriptor> descriptors = descs.values();
		// This code assumes single table per descriptor!
		for (ClassDescriptor desc : descriptors) {
			String fullClassName = desc.getJavaClassName();
			String className=Helper.getShortClassName(fullClassName);
			String tableName = customizeTableName(className);
			Vector<String> tableNames = new Vector<String>();
			updateMappings(desc, tableName);

	private void updateMappings(ClassDescriptor desc, String tableName) {
		for (DatabaseMapping mapping : desc.getMappings()) {
			if (mapping.isDirectToFieldMapping()) {
				DirectToFieldMapping directMapping = (DirectToFieldMapping) mapping;

	private String customizeTableName(String className) {
		return "T_" + className; 

package model;

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

public class User {
	private long id;
	private String fullName;
	public String getFullName() {
		return fullName;
	public void setFullName(String fullName) {
		this.fullName = fullName;
	public long getId() {
		return id;
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns=""; xmlns:xsi=""; version="1.0" xsi:schemaLocation="";>
	<persistence-unit name="demo">
			<property name="eclipselink.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
			<property name="eclipselink.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:ORCL"/>
			<property name="eclipselink.jdbc.user" value="scott"/>
			<property name="eclipselink.jdbc.password" value="tiger"/>

			<property name="eclipselink.logging.level" value="FINE"/>
            	<property name="eclipselink.logging.timestamp" value="false"/>
            	<property name="eclipselink.logging.thread" value="false"/>
            	<property name="eclipselink.logging.session" value="false"/>
            	<property name="eclipselink.session.customizer" value="model.MyCustomizer"/>
           	 	<property name="eclipselink.ddl-generation" value="create-tables"/>

Back to the top