Hi,
the problem is to get the class Book mapped not to the table Books, as it would be by default,
but to get it mapped to a table with the name depending on the value returned by book.getBookName()
ExampleThe Book 'The Lord of the Rings' should not be mapped to the table called 'Books' but to the table
'The_Lord_of_the_Rings'.
First attempt: NamingStrategy
First readings leaded me to the interface
NamingStrategy, where the methodname '
classToTableName' sounded perfect for the problem.
But the parameter
(String className) stoped my hope, because the String value doesn't allow to make the name depended
on a value of a class instance.
Second attempt: Custom persister
The second approach was to annotate the Book-class with a custom persister MyPersister (see code below).
MyPersister inherits from SingleTableEntityPersister and for the beginning just changes the default names by adding "Changed" in front of the defaultname.
I thought it could be the right way, because of the method-names.
But running the code I got an error-msg that the corresponding relation does not exist (for example the table 'ChangedBook').
So a custom persister doesn't get involved to
create the tables and this makes me think that even a custom persister is not the right way
to change the resulting table name of a mapped class.
Since I am stucked for several weeks now I am thankful for any help, example, experience or any tip.
With best regards,
Thomas
Code:
@Entity
@Persister(impl=MyPersister.class) // <----------------------- Custom Persister
public class Book
{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id = null;
private String bookName = null;
protected Book(){}
public String getBookName() {
return bookName;
}
}
Code:
import java.io.Serializable;
import java.util.Comparator;
import java.util.Map;
import org.hibernate.EntityMode;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.MappingException;
import org.hibernate.bytecode.spi.EntityInstrumentationMetadata;
import org.hibernate.cache.spi.access.EntityRegionAccessStrategy;
import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy;
import org.hibernate.cache.spi.entry.CacheEntryStructure;
import org.hibernate.engine.spi.CascadeStyle;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.engine.spi.ValueInclusion;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.internal.FilterAliasGenerator;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.metamodel.domain.Superclass;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.persister.entity.SingleTableEntityPersister;
import org.hibernate.tuple.entity.EntityMetamodel;
import org.hibernate.tuple.entity.EntityTuplizer;
import org.hibernate.type.Type;
import org.hibernate.type.VersionType;
public class MyPersister extends SingleTableEntityPersister {
public MyPersister(PersistentClass persistentClass,
EntityRegionAccessStrategy cacheAccessStrategy,
NaturalIdRegionAccessStrategy naturalIdRegionAccessStrategy,
SessionFactoryImplementor factory, Mapping mapping)
throws HibernateException {
super(persistentClass, cacheAccessStrategy, naturalIdRegionAccessStrategy,
factory, mapping);
System.out.println("___ MyPersister.Constructor");
// TODO Auto-generated constructor stub
}
@Override
public String getRootTableName() {
// TODO Auto-generated method stub
return "Changed" + super.getRootTableName();
}
@Override
public String getRootEntityName() {
// TODO Auto-generated method stub
String name = super.getRootEntityName();
String[] parts = name.split("\\.");
if (parts.length > 0)
name = parts[0] + "Changed" + parts[1];
else {
System.out.println("RootEntityName: " + name );
}
return name;
}
@Override
public String getTableName() {
// TODO Auto-generated method stub
String name = "Changed" + super.getTableName();
System.out.println("TableName: " + name );
return name;
}
@Override
protected String getTableName(int j) {
// TODO Auto-generated method stub
String name = "Changed" + super.getTableName(j);
System.out.println("TableName(j): j= " + j + ": " + name + "; " + j);
return name;
}
@Override
public Serializable[] getPropertySpaces() {
// TODO Auto-generated method stub
System.out.println("Changed" + super.getPropertySpaces());
return super.getPropertySpaces();
}
@Override
public Comparator getVersionComparator() {
// TODO Auto-generated method stub
return super.getVersionComparator();
}
@Override
public void postInstantiate() throws MappingException {
// TODO Auto-generated method stub
super.postInstantiate();
}
@Override
public SessionFactoryImplementor getFactory() {
// TODO Auto-generated method stub
return super.getFactory();
}
@Override
public EntityMetamodel getEntityMetamodel() {
// TODO Auto-generated method stub
EntityMetamodel metamodel = super.getEntityMetamodel();
System.out.println("___MetaModel " + metamodel);
return metamodel;
// return super.getEntityMetamodel();
}
@Override
public boolean isSubclassEntityName(String entityName) {
// TODO Auto-generated method stub
return super.isSubclassEntityName(entityName);
}
@Override
public Serializable[] getQuerySpaces() {
// TODO Auto-generated method stub
System.out.println("query spaces: " + super.getQuerySpaces());
return super.getQuerySpaces();
}
@Override
public boolean hasProxy() {
// TODO Auto-generated method stub
return super.hasProxy();
}
@Override
public boolean hasCollections() {
// TODO Auto-generated method stub
return super.hasCollections();
}
@Override
public boolean hasMutableProperties() {
// TODO Auto-generated method stub
return super.hasMutableProperties();
}
@Override
public boolean hasSubselectLoadableCollections() {
// TODO Auto-generated method stub
return super.hasSubselectLoadableCollections();
}
@Override
public boolean hasCascades() {
// TODO Auto-generated method stub
return super.hasCascades();
}
@Override
public boolean isMutable() {
// TODO Auto-generated method stub
return super.isMutable();
}
@Override
public boolean isInherited() {
// TODO Auto-generated method stub
return super.isInherited();
}
@Override
public boolean isIdentifierAssignedByInsert() {
// TODO Auto-generated method stub
return super.isIdentifierAssignedByInsert();
}
@Override
public Type getPropertyType(String propertyName) throws MappingException {
// TODO Auto-generated method stub
return super.getPropertyType(propertyName);
}
@Override
public int[] findDirty(Object[] currentState, Object[] previousState,
Object owner, SessionImplementor session) {
// TODO Auto-generated method stub
return super.findDirty(currentState, previousState, owner, session);
}
@Override
public int[] findModified(Object[] old, Object[] current, Object object,
SessionImplementor session) {
// TODO Auto-generated method stub
return super.findModified(old, current, object, session);
}
@Override
public boolean hasIdentifierProperty() {
// TODO Auto-generated method stub
return super.hasIdentifierProperty();
}
@Override
public boolean canExtractIdOutOfEntity() {
// TODO Auto-generated method stub
return super.canExtractIdOutOfEntity();
}
@Override
public boolean isVersioned() {
// TODO Auto-generated method stub
return super.isVersioned();
}
@Override
public VersionType getVersionType() {
// TODO Auto-generated method stub
return super.getVersionType();
}
@Override
public int getVersionProperty() {
// TODO Auto-generated method stub
return super.getVersionProperty();
}
@Override
public boolean hasNaturalIdentifier() {
// TODO Auto-generated method stub
return super.hasNaturalIdentifier();
}
@Override
public int[] getNaturalIdentifierProperties() {
// TODO Auto-generated method stub
return super.getNaturalIdentifierProperties();
}
@Override
public Object[] getNaturalIdentifierSnapshot(Serializable id,
SessionImplementor session) {
// TODO Auto-generated method stub
return super.getNaturalIdentifierSnapshot(id, session);
}
@Override
public IdentifierGenerator getIdentifierGenerator() {
// TODO Auto-generated method stub
return super.getIdentifierGenerator();
}
@Override
public boolean hasLazyProperties() {
// TODO Auto-generated method stub
return super.hasLazyProperties();
}
@Override
public Serializable loadEntityIdByNaturalId(Object[] naturalIdValues,
LockOptions lockOptions, SessionImplementor session) {
// TODO Auto-generated method stub
System.out.println("___LOADING");
return super.loadEntityIdByNaturalId(naturalIdValues, lockOptions, session);
}
@Override
public Object load(Serializable id, Object optionalObject,
LockMode lockMode, SessionImplementor session)
throws HibernateException {
// TODO Auto-generated method stub
System.out.println("___LOADING");
return super.load(id, optionalObject, lockMode, session);
}
@Override
public Object load(Serializable id, Object optionalObject,
LockOptions lockOptions, SessionImplementor session)
throws HibernateException {
// TODO Auto-generated method stub
System.out.println("___LOADING");
return super.load(id, optionalObject, lockOptions, session);
}
@Override
public void lock(Serializable id, Object version, Object object,
LockMode lockMode, SessionImplementor session)
throws HibernateException {
// TODO Auto-generated method stub
super.lock(id, version, object, lockMode, session);
}
@Override
public void lock(Serializable id, Object version, Object object,
LockOptions lockOptions, SessionImplementor session)
throws HibernateException {
// TODO Auto-generated method stub
super.load(id, object, lockOptions, session);
}
@Override
public void insert(Serializable id, Object[] fields, Object object,
SessionImplementor session) throws HibernateException {
// TODO Auto-generated method stub
System.out.println("___ Insert 1");
super.insert(id, fields, object, session);
}
@Override
public Serializable insert(Object[] fields, Object object,
SessionImplementor session) throws HibernateException {
// TODO Auto-generated method stub
System.out.println("___ Insert 2");
return super.insert(fields, object, session);
}
@Override
public void delete(Serializable id, Object version, Object object,
SessionImplementor session) throws HibernateException {
// TODO Auto-generated method stub
super.delete(id, version, object, session);
}
@Override
public void update(Serializable id, Object[] fields, int[] dirtyFields,
boolean hasDirtyCollection, Object[] oldFields, Object oldVersion,
Object object, Object rowId, SessionImplementor session)
throws HibernateException {
// TODO Auto-generated method stub
System.out.println("___ update");
super.update(id, fields, dirtyFields, hasDirtyCollection, oldFields, oldVersion, object, rowId, session);
}
@Override
public Type[] getPropertyTypes() {
// TODO Auto-generated method stub
return super.getPropertyTypes();
}
@Override
public String[] getPropertyNames() {
// TODO Auto-generated method stub
return super.getPropertyNames();
}
@Override
public boolean[] getPropertyInsertability() {
// TODO Auto-generated method stub
return super.getPropertyInsertability();
}
@Override
public ValueInclusion[] getPropertyInsertGenerationInclusions() {
// TODO Auto-generated method stub
return super.getPropertyInsertGenerationInclusions();
}
@Override
public ValueInclusion[] getPropertyUpdateGenerationInclusions() {
// TODO Auto-generated method stub
return super.getPropertyUpdateGenerationInclusions();
}
@Override
public boolean[] getPropertyUpdateability() {
// TODO Auto-generated method stub
return super.getPropertyUpdateability();
}
@Override
public boolean[] getPropertyCheckability() {
// TODO Auto-generated method stub
return super.getPropertyCheckability();
}
@Override
public boolean[] getPropertyNullability() {
// TODO Auto-generated method stub
return super.getPropertyNullability();
}
@Override
public boolean[] getPropertyVersionability() {
// TODO Auto-generated method stub
return super.getPropertyVersionability();
}
@Override
public boolean[] getPropertyLaziness() {
// TODO Auto-generated method stub
return super.getPropertyLaziness();
}
@Override
public CascadeStyle[] getPropertyCascadeStyles() {
// TODO Auto-generated method stub
return super.getPropertyCascadeStyles();
}
@Override
public Type getIdentifierType() {
// TODO Auto-generated method stub
return super.getIdentifierType();
}
@Override
public String getIdentifierPropertyName() {
// TODO Auto-generated method stub
return super.getIdentifierPropertyName();
}
@Override
public boolean isCacheInvalidationRequired() {
// TODO Auto-generated method stub
return super.isCacheInvalidationRequired();
}
@Override
public boolean isLazyPropertiesCacheable() {
// TODO Auto-generated method stub
return super.isLazyPropertiesCacheable();
}
@Override
public boolean hasCache() {
// TODO Auto-generated method stub
return super.hasCache();
}
@Override
public EntityRegionAccessStrategy getCacheAccessStrategy() {
// TODO Auto-generated method stub
return super.getCacheAccessStrategy();
}
@Override
public CacheEntryStructure getCacheEntryStructure() {
// TODO Auto-generated method stub
return super.getCacheEntryStructure();
}
@Override
public boolean hasNaturalIdCache() {
// TODO Auto-generated method stub
return super.hasNaturalIdCache();
}
@Override
public NaturalIdRegionAccessStrategy getNaturalIdCacheAccessStrategy() {
// TODO Auto-generated method stub
return super.getNaturalIdCacheAccessStrategy();
}
@Override
public ClassMetadata getClassMetadata() {
// TODO Auto-generated method stub
// return super.getClassMetadata();
return null;
}
@Override
public boolean isBatchLoadable() {
// TODO Auto-generated method stub
return super.isBatchLoadable();
}
@Override
public boolean isSelectBeforeUpdateRequired() {
// TODO Auto-generated method stub
return super.isSelectBeforeUpdateRequired();
}
@Override
public Object[] getDatabaseSnapshot(Serializable id,
SessionImplementor session) throws HibernateException {
// TODO Auto-generated method stub
return super.getDatabaseSnapshot(id, session);
}
@Override
public Serializable getIdByUniqueKey(Serializable key,
String uniquePropertyName, SessionImplementor session) {
// TODO Auto-generated method stub
return super.getIdByUniqueKey(key, uniquePropertyName, session);
}
@Override
public Object getCurrentVersion(Serializable id, SessionImplementor session)
throws HibernateException {
// TODO Auto-generated method stub
return super.getCurrentVersion(id, session);
}
@Override
public Object forceVersionIncrement(Serializable id, Object currentVersion,
SessionImplementor session) throws HibernateException {
// TODO Auto-generated method stub
return super.forceVersionIncrement(id, currentVersion, session);
}
@Override
public boolean isInstrumented() {
// TODO Auto-generated method stub
return super.isInstrumented();
}
@Override
public boolean hasInsertGeneratedProperties() {
// TODO Auto-generated method stub
return super.hasInsertGeneratedProperties();
}
@Override
public boolean hasUpdateGeneratedProperties() {
// TODO Auto-generated method stub
return super.hasUpdateGeneratedProperties();
}
@Override
public boolean isVersionPropertyGenerated() {
// TODO Auto-generated method stub
return super.isVersionPropertyGenerated();
}
@Override
public void afterInitialize(Object entity,
boolean lazyPropertiesAreUnfetched, SessionImplementor session) {
// TODO Auto-generated method stub
super.afterInitialize(entity, lazyPropertiesAreUnfetched, session);
}
@Override
public void afterReassociate(Object entity, SessionImplementor session) {
// TODO Auto-generated method stub
super.afterReassociate(entity, session);
}
@Override
public Object createProxy(Serializable id, SessionImplementor session)
throws HibernateException {
// TODO Auto-generated method stub
return super.createProxy(id, session);
}
@Override
public Boolean isTransient(Object object, SessionImplementor session)
throws HibernateException {
// TODO Auto-generated method stub
return super.isTransient(object, session);
}
@Override
public Object[] getPropertyValuesToInsert(Object object, Map mergeMap,
SessionImplementor session) throws HibernateException {
// TODO Auto-generated method stub
return super.getPropertyValuesToInsert(object, mergeMap, session);
}
@Override
public void processInsertGeneratedProperties(Serializable id,
Object entity, Object[] state, SessionImplementor session) {
// TODO Auto-generated method stub
super.processInsertGeneratedProperties(id, entity, state, session);
}
@Override
public void processUpdateGeneratedProperties(Serializable id,
Object entity, Object[] state, SessionImplementor session) {
// TODO Auto-generated method stub
super.processUpdateGeneratedProperties(id, entity, state, session);
}
@Override
public boolean implementsLifecycle() {
// TODO Auto-generated method stub
return super.implementsLifecycle();
}
@Override
public Class getConcreteProxyClass() {
// TODO Auto-generated method stub
return super.getConcreteProxyClass();
}
@Override
public void setPropertyValues(Object object, Object[] values) {
// TODO Auto-generated method stub
super.setPropertyValues(object, values);
}
@Override
public void setPropertyValue(Object object, int i, Object value) {
// TODO Auto-generated method stub
super.setPropertyValue(object, i, value);
}
@Override
public Object[] getPropertyValues(Object object) {
// TODO Auto-generated method stub
return super.getPropertyValues(object);
}
@Override
public Object getPropertyValue(Object object, int i)
throws HibernateException {
// TODO Auto-generated method stub
return super.getPropertyValue(object, i);
}
@Override
public Object getPropertyValue(Object object, String propertyName) {
// TODO Auto-generated method stub
return super.getPropertyValue(object, propertyName);
}
@Override
public Serializable getIdentifier(Object object) throws HibernateException {
// TODO Auto-generated method stub
return super.getIdentifier(object);
}
@Override
public Serializable getIdentifier(Object entity, SessionImplementor session) {
// TODO Auto-generated method stub
return super.getIdentifier(entity, session);
}
@Override
public void setIdentifier(Object entity, Serializable id,
SessionImplementor session) {
// TODO Auto-generated method stub
super.setIdentifier(entity, id, session);
}
@Override
public Object getVersion(Object object) throws HibernateException {
// TODO Auto-generated method stub
return super.getVersion(object);
}
@Override
public Object instantiate(Serializable id, SessionImplementor session) {
// TODO Auto-generated method stub
return super.instantiate(id, session);
}
@Override
public boolean isInstance(Object object) {
// TODO Auto-generated method stub
return super.isInstance(object);
}
@Override
public boolean hasUninitializedLazyProperties(Object object) {
// TODO Auto-generated method stub
return super.hasUninitializedLazyProperties(object);
}
@Override
public void resetIdentifier(Object entity, Serializable currentId,
Object currentVersion, SessionImplementor session) {
// TODO Auto-generated method stub
super.resetIdentifier(entity, currentId, currentVersion, session);
}
@Override
public EntityPersister getSubclassEntityPersister(Object instance,
SessionFactoryImplementor factory) {
// TODO Auto-generated method stub
return super.getSubclassEntityPersister(instance, factory);
}
@Override
public EntityMode getEntityMode() {
// TODO Auto-generated method stub
return super.getEntityMode();
}
@Override
public EntityTuplizer getEntityTuplizer() {
// TODO Auto-generated method stub
System.out.println("_____Tuplizer");
return super.getEntityTuplizer();
}
@Override
public EntityInstrumentationMetadata getInstrumentationMetadata() {
// TODO Auto-generated method stub
return super.getInstrumentationMetadata();
}
@Override
public FilterAliasGenerator getFilterAliasGenerator(String rootAlias) {
// TODO Auto-generated method stub
return super.getFilterAliasGenerator(rootAlias);
}
}