But then come your local friendly DBAs, who will be glad to tell you about their set of database naming conventions -- conventions that date from before the demise of the dinosaurs, and that aren't about to change! These database conventions usually have quite valid reasons to exist, and in any case DBAs tend to be a conservative lot. What's a developer to do?
One simple solution is to use the name
attribute of the @Entity
and @Column
annotations to override the default name generation, as shown in Listing 4.
Listing 4. Annotating the Client class to customize the generated SQL schema
@Entity(name="T_CLIENT")
public class Client implements Serializable {
...
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="CLIENT_ID")
private Long id;
@Column(name="FIRST_NAME")
private String firstName;
@Column(name="LAST_NAME")
private String lastName;
...
}
This will work, but it becomes a wee bit tiresome when you have a lot of tables. Indeed, you have to remember to do it for each and every table and field. There has to be a better way, right? Well, there is! You can define a naming strategy in your Hibernate session factory to override the default behavior. This basically involves writing a class that tells Hibernate how you want table and field names formatted.
A good place to start is the ImprovedNamingStrategy
class, which basically converts camel case class names (SomeDomainEntity
) to names in lower case with underscores (some_domain_entity
). You need to provide this class when you create your Hibernate session at startup. If you are using Spring, you simply create a naming strategy bean and provide it to the session factory. Listing 5 illustrates what a typical Spring configuration would look like.
Listing 5. Configuring automatic schema generation in Spring
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:/hibernate.cfg.xml" />
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
<property name="namingStrategy" ref="namingStrategy" />
</bean>
<bean id="namingStrategy" class="org.hibernate.cfg.ImprovedNamingStrategy"/>
With this naming strategy, Hibernate would generate a script that looks something like the one in Listing 6.
Listing 6. The SQL script created using the Spring Hibernate configuration
create table client (
id bigint generated by default as identity (start with 1),
first_name varchar(255),
last_name varchar(255),
...
primary key (id)
);
That's nice, but it doesn't always solve all your problems. Typically, database naming conventions are more demanding. For example, each table might have to start with T_ and be in uppercase (T_CLIENT for the Client
class, for example). Or each field in a table might need to start with a table-specific prefix (CLI_FIRST_NAME and CLI_LAST_NAME, for instance). To automate this sort of constraint, you need to write your own naming strategy implementation.
分享到:
相关推荐
赠送jar包:nacos-naming-2.0.4.RELEASE.jar; 赠送原API文档:nacos-naming-2.0.4.RELEASE-javadoc.jar; 赠送源代码:nacos-naming-2.0.4.RELEASE-sources.jar; 赠送Maven依赖信息文件:nacos-naming-2.0.4....
naming-common.jar
spring 配置文件
赠送jar包:nacos-naming-2.0.4.RELEASE.jar; 赠送原API文档:nacos-naming-2.0.4.RELEASE-javadoc.jar; 赠送源代码:nacos-naming-2.0.4.RELEASE-sources.jar; 赠送Maven依赖信息文件:nacos-naming-2.0.4....
naming-factory.jar
javax.naming.NamingException: Cannot create resource instance类加载异常,希望可以帮助跟我一样错误的人。
util-naming-2.0.1-450.jarutil-naming-2.0.1-450.jarutil-naming-2.0.1-450.jarutil-naming-2.0.1-450.jarutil-naming-2.0.1-450.jarutil-naming-2.0.1-450.jarutil-naming-2.0.1-450.jarutil-naming-2.0.1-450....
naming-resources.jar
hibernate.properties # # Hibernate, Relational Persistence for Idiomatic Java # # License: GNU Lesser General Public License (LGPL), version 2.1 or later. # See the lgpl.txt file in the root directory...
随机网络中Naming Game的作用规则,宋倩倩,郑志明,Naming Game(NG)在语言动力学中有着广泛的应用,它研究了智能主体基于局部的相互作用,通过自组织演化最终达到全局共识的一个过程��
With naming an easily reversable act, the naming system of a program is more fluid than at any other point in software history. This is a good thing. Now there is no need to leave poor names in place...
naming-factory-dbcp.jar
The Naming Convention for Clock Tree Optimization Repeaters Description Question: When clock tree synthesis or clock tree optimization is run, a lot of buffers with the cto_st* naming convention are ...
pep8-naming, python的命名约定检查器 PEP-8 命名约定检查 PEP-8 命名约定。这个模块为 flake8 提供了一个插件,python 代码检查器。( 它替换了 flint 检查器的插件 flint-naming 。)安装你可以使用以下命令安装,...
博文链接:https://yanghaiskys.iteye.com/blog/223317
工作流接口规范(API) 术语表 命名规约
JNDI(Java Naming and Directory Interface)是SUN公司提供的一种标准的Java命名系统接口,JNDI提供统一的客户端API,通过不同的访问提供者接口JNDI SPI的实现,由管理者将JNDI API映射为特定的命名服务和目录系统,...
Unix系统管理高手,介绍Unix系统管理方面知识,本文主要包括Naming和Directory方面,介绍了Unix下LDAP的建立与管理。
多维材质及标准材质批量命名小脚本