/* Copyright (C) 2007 Viavansi Servicios Avanzados para las Instituciones S.L. (VIAVANSI) Se permite la libre distribución y modificación de esta librería bajo los términos de la licencia GPL siempre que se indique de forma clara la autoría de Servicios Avanzados para las Instituciones S.L. (VIAVANSI). Para usos comerciales de este software contacte con info@viavansi.com. This library is free software; you can redistribute it and/or modify it under the terms of the GPL GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. For any commercial use of this software contact info@viavansi.com. ------------------------------------------------------------------------------------- Encoding: UTF-8 Cadena de comprobación de encoding: 'El veloz murciélago hindú comía feliz cardillo y kiwi.' 'La cigüeña tocaba el saxofón detrás del palenque de paja.' */ package com.viavansi.framework.persistencia.jpa; import org.apache.commons.lang.StringUtils; import org.hibernate.cfg.DefaultComponentSafeNamingStrategy; /** * Estrategia para generación de nombres de tablas asociadas a anotaciones Table EJB3.0. * De acuerdo con la especificación EJB 3.0 se permite definir mapeadores de nombres de tabla desde la anotación @Table, * para ello extiende de la implementación por defecto de Hibernate-annotations para añadir a este comportamiento el soporte de prefijos. * * Hibernate goes beyond the EJB3 spec and allows you to enhance the defaulting mechanism through the NamingStrategy, * it class override the default Hibernate mechanism for add support to prefix table. * * EJ: Si las tablas se llaman SB_PERSONA siendo el prefijo SB_ el soporte de prefijos sustituye "${PREFIX}PERSONA" por "SB_PERSONA". * * @author Félix García Borrego(fgarcia@viavansi.com) * */ public class NamingStrategy extends DefaultComponentSafeNamingStrategy{ /** * Transforma el nombre de la tabla indicado en la anotación añadiendo el prefijo si es necesario. * * @see org.hibernate.cfg.EJB3NamingStrategy#tableName(java.lang.String) */ @Override public String tableName(String tableName) { String name=super.tableName(tableName); // si el nombre de la tabla contiene un prefijo if(name.contains("$")){ // recuperamos el prefijo y lo procesamos String idPrefijo=StringUtils.substringBetween(name, "${","}"); String prefijo=ConfigPersistencia.getInstance().getPrefix(idPrefijo); name=name.replaceFirst("\\$\\{.*\\}", prefijo); } return name; } /** * Serial version for Lost 4-8-15-16-23-42 :p. */ private static final long serialVersionUID = 4815162342L; }