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....
官方建议如果在Hibernate3中必须使用DBCP,最好通过JNDI(Java Naming and Directory Interface)方式进行配置,以避免潜在的问题。 其次,Hibernate内置的连接池虽然方便,但在性能和稳定性上并不理想,尤其是在...
5. **hibernate.session_factory_name**:这个参数用于在JNDI(Java Naming and Directory Interface)中注册SessionFactory的名称,便于其他组件查找和使用。 6. **hibernate.max_fetch_depth**:它限制了外连接...
JNDI(Java Naming and Directory Interface)部分涉及到如何使用Hibernate通过JNDI查找数据源。 锁定机制(Locking)探讨了Hibernate如何控制数据库锁的获取和释放,以保证数据的一致性。 抓取策略(Fetching)...
当运行代码时出现“Not binding factory to JNDI, no JNDI name configured”这一提示,表示Hibernate没有将SessionFactory绑定到JNDI(Java Naming and Directory Interface)中,通常是因为配置中没有指定JNDI名称...
最后,本文还提到了几个关键的技术点,例如JNDI数据源的使用,这允许开发者从Java命名和目录接口(Java Naming and Directory Interface)获取数据库连接,有助于在企业环境中管理数据库连接。此外,还提到了...
9. **JNDI 数据源配置**:在企业环境中,通常通过 JNDI(Java Naming and Directory Interface)查找数据源,Spring 支持从 JNDI 获取并配置数据源,供 Hibernate 使用。 10. **配置文件**:整合过程中,需要配置 ...
例如,使用`org.hibernate.boot.model.naming.PhysicalNamingStrategy`接口来自定义表名生成策略,或者利用`Session.createSQLQuery()`方法直接执行原生SQL。 2. **多数据库支持**:为了适应不同的数据库,如MySQL...
JNDI和锁:JNDI(Java Naming and Directory Interface)用于在Java应用程序中访问命名和目录服务。锁部分则介绍了乐观锁和悲观锁的概念及其使用方法。 Fetching(抓取):抓取是指Hibernate如何从数据库中获取数据...
naming-common.jar
如果由于某些原因需要使用DBCP,官方建议通过JNDI(Java Naming and Directory Interface)方式进行配置,以避免直接在Hibernate配置文件中使用。 默认情况下,Hibernate会使用一个内置的连接池。尽管简单易用,但...
### hibernate 3.1+tomcat 5.5.x(配置jndi) #### 一、引言 随着Tomcat从5.0版本升级到5.5.x版本,其配置发生了诸多变化,特别是对Tomcat JNDI的配置方式产生了显著的影响。这对希望在Hibernate安装中利用Tomcat ...
spring 配置文件
JNDI(Java Naming and Directory Interface)部分说明了如何在Hibernate中配置JNDI,使得应用服务器能够管理Hibernate的资源。 8. 锁: 锁章节讲解了乐观锁和悲观锁的使用和管理,包括乐观锁的版本号和时间戳机制...
而JNDI查找则是通过Java Naming and Directory Interface(JNDI)服务来查找数据源。 1. JDBC直连配置示例: ```xml <property name="connection.driver_class">com.mysql.jdbc.Driver</property> ...
接下来,我们需要配置Hibernate以使用这个通过JNDI(Java Naming and Directory Interface)提供的数据源。在Hibernate的配置文件(通常是`hibernate.cfg.xml`)中,我们需要指定数据源的JNDI名称,而不是直接提供...
naming-factory.jar
javax.naming.NamingException: Cannot create resource instance类加载异常,希望可以帮助跟我一样错误的人。
在IT行业中,我们经常遇到各种异常,其中之一是“Javax.naming.NameNotFoundException”。这个异常通常在Java命名和目录接口(JNDI)中出现,当我们尝试查找一个在命名上下文中不存在的名称时,就会抛出这个异常。...