简体   繁体   中英

How to change the Hibernate table name(@Table(name=“tableName”)) at runtime?

I am working in spring mvc with hibernate. I am new to this technology. I need to change the hibernate entity class table name at runtime?

@Entity
@Table(name="tableName")
public class DynamicTable{

// column name with get set methods.
....
}

Here i need change the table name at runtime. My column is going to same only. Is it possible to change table name? If possible kindly explain me? if not tell me the alternate solution for this problem.

package net.petrikainulainen.hibernate.util;
import org.hibernate.cfg.ImprovedNamingStrategy;


public class CustomNamingStrategy extends ImprovedNamingStrategy {

    private static final String PLURAL_SUFFIX = "s";


    @Override
    public String classToTableName(String className) {
        String tableNameInSingularForm = super.classToTableName(className);
        return transformToPluralForm(tableNameInSingularForm);
    }

    private String transformToPluralForm(String tableNameInSingularForm) {
        StringBuilder pluralForm = new StringBuilder();

        pluralForm.append(tableNameInSingularForm);
        pluralForm.append(PLURAL_SUFFIX);

        return pluralForm.toString();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM