当前位置: 动力学知识库 > 问答 > 编程问答 >

orm - Hibernate: Column names based on table name?

问题描述:

In my company, we have this naming convention for table columns:

Table: TCARS

Columns: TCARS_ID, TCARS_BRAND_ID, TCARS_MODEL_ID... etc.

I currently have this entity class:

@Table(name="TCARS")

public class CarEntity {

@Column(name="TCARS_ID")

private int id;

@Column(name="TCARS_BRAND_ID")

private int brandId;

// etc

}

I would like to write my class like this:

@Table(name="TCARS")

public class CarEntity {

@Column(name="ID")

private int id;

@Column(name="BRAND_ID")

private int brandId;

// etc

}

...and use an Hibernate NamingStrategy to complete the column names based on table name. The problem I have is that on NamingStrategy interface, I can't access table names while I'm configuring column name, so I'm unable to prefix the column names with the table name.

Is there any solution tho this?

Thanks in advance.

网友答案:

NHibernate has a BeforeBind event on the configuration class, maybe Hibernate too.

// pseudocode
Hibernate.Cfg.Configuration config;

config.BeforeBindMapping + eventhandler
{
    HbmClass c = RootClasses[0];

    foreach(IColumnsMapping hascolumns in c.Items.OfType<IColumnsMapping>())
    {
        hascolumns.Columns[0].Text = c.Table.Name + hascolumns.Columns[0].Text;
    }
};
分享给朋友:
您可能感兴趣的文章:
随机阅读: