`
ponlya
  • 浏览: 164679 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

Hibernate 官网 config code

 
阅读更多

如下是官网的描述及部分示例代码
http://docs.jboss.org/hibernate/orm/4.1/quickstart/en-US/html_single/
http://docs.jboss.org/hibernate/orm/4.1/devguide/en-US/html_single/
作用介于OOP语言与DB之间的ORM,减少95%的代码量.

 

Hibernate Artifacts under groupId org.hibernate
hibernate-core        main artifact, needed to build applications using the native Hibernate APIs including defining metadata in both annotations as well as Hibernate's own hbm.xml format.
hibernate-entitymanager        depends on hibernate-core,Represents Hibernate's implementation of JPA
hibernate-envers    optional module that provides historical auditing of changes to your entities. depends on both hibernate-core and hibernate-entitymanager.
hibernate-c3p0    Provides integration between Hibernate and the C3P0 connection pool library. depends on hibernate-core, but is generally included in a project as a runtime dependency. It pulls in the C3P0 dependencies automatically.
hibernate-proxool    integration between Hibernate and the Proxool connection pool library.depends on hibernate-core, but is generally included in a project as a runtime dependency. It pulls in the Proxool dependencies automatically..    
hibernate-ehcache    integration between Hibernate and EhCache, as a second-level cache.depends on hibernate-core, but is generally included in a project as a runtime dependency. It pulls in the Ehcache dependencies automatically.
hibernate-infinispan    integration between Hibernate and Infinispan, as a second-level cache.depends on hibernate-core, but is generally included in a project as a runtime dependency. It pulls in the Infinispan dependencies automatically.

DB & Pool

Hibernate obtains JDBC connections as needed though the org.hibernate.service.jdbc.connections.spi.ConnectionProvider interface which is a service contract.
Applications may also supply their own org.hibernate.service.jdbc.connections.spi.ConnectionProvider

Most important Hibernate JDBC properties , basic
    hibernate.connection.driver_class
    hibernate.connection.url
    hibernate.connection.username
    hibernate.connection.password
    hibernate.connection.pool_size  #Hibernate's internal connection pooling algorithm is rudimentary, and is provided for development and testing purposes.
    #To use a third-party pool, replace the hibernate.connection.pool_size property with settings specific to your connection pool of choice.
   
hibernate.properties for a c3p0 connection pool
    hibernate.connection.driver_class = org.postgresql.Driver
    hibernate.connection.url = jdbc:postgresql://localhost/mydatabase
    hibernate.connection.username = myuser
    hibernate.connection.password = secret
    hibernate.c3p0.min_size=5
    hibernate.c3p0.max_size=20
    hibernate.c3p0.timeout=1800
    hibernate.c3p0.max_statements=50
    hibernate.dialect = org.hibernate.dialect.PostgreSQL82Dialect

    or
    <!-- Database connection settings -->
    <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
    <property name="connection.url">jdbc:hsqldb:hsql://localhost</property>
    <property name="connection.username">sa</property>
    <property name="connection.password"></property>
   
    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.HSQLDialect</property>

    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>

    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">update</property>

    or
    Configuration cfg = new Configuration()
            .addClass(org.hibernate.auction.Item.class)
            .addClass(org.hibernate.auction.Bid.class)
            .setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect")
            .setProperty("hibernate.connection.datasource", "java:comp/env/jdbc/test")
            .setProperty("hibernate.order_updates", "true"); //>
   
c3p0
    org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider
    Important configuration properties for the c3p0 connection pool
            hibernate.c3p0.min_size
            hibernate.c3p0.max_size
            hibernate.c3p0.timeout
            hibernate.c3p0.max_statements
Proxool connection pool
    org.hibernate.service.jdbc.connections.internal.ProxoolConnectionProvider
    hibernate.proxool.xml    Configure Proxool provider using an XML file (.xml is appended automatically)
    hibernate.proxool.properties    Configure the Proxool provider using a properties file (.properties is appended automatically)
    hibernate.proxool.existing_pool    Whether to configure the Proxool provider from an existing pool
    hibernate.proxool.pool_alias    Proxool pool alias to use. Required.
   
JNDI
    hibernate.connection.datasource (required)
    hibernate.jndi.url
    hibernate.jndi.class
    hibernate.connection.username
    hibernate.connection.password

 

Hibernate configuration file

hibernate.cfg.xml defines Hibernate configuration information.
connection.driver_class, connection.url, connection.username and connection.password property elements define JDBC connection information.ialect property specifies the particular SQL variant with which Hibernate will converse.
hbm2ddl.auto property enables automatic generation of database schemas directly into the database.

entity Java class
This class uses standard JavaBean naming conventions for property getter and setter methods, as well as private visibility for the fields. Although this is the recommended design, it is not required.
The no-argument constructor, which is also a JavaBean convention, is a requirement for all persistent classes. Hibernate needs to create objects for you, using Java Reflection. The constructor can be private. However, package or public visibility is required for runtime proxy generation and efficient data retrieval without bytecode instrumentation.
@javax.persistence.Entity annotation is used to mark a class as an entity.
@javax.persistence.Table annotation explicitly specifies the table name. Without this specification, the default table name would be EVENT).
@javax.persistence.Id marks the property which defines the entity's identifier.
@javax.persistence.GeneratedValue and @org.hibernate.annotations.GenericGenerator work in tandem to indicate that Hibernate should use Hibernate's increment generation strategy for this entity's identifier values.
    eg:
        @Id
        @GeneratedValue(generator="increment")
        @GenericGenerator(name="increment", strategy = "increment")

org.hibernate.SessionFactory which is a thread-safe object that is instantiated once to serve the entire application.
org.hibernate.SessionFactory acts as a factory for org.hibernate.Session instances. A org.hibernate.Session should be thought of as a corollary to a "unit of work".

xml config entity
    <property name="zip" length="5"/>
    <property name="balance" precision="12" scale="2"/>
   
    <many-to-one name="bar" column="barId" not-null="true"/>
    <element column="serialNumber" type="long" not-null="true" unique="true"/>

    <many-to-one name="org" column="orgId" unique-key="OrgEmployeeId"/>
    <property name="employeeId" unique-key="OrgEmployee"/>

    <property name="name" type="my.customtypes.Name"/>
        <column name="last" not-null="true" index="bar_idx" length="30"/>
        <column name="first" not-null="true" index="bar_idx" length="20"/>
        <column name="initial"/>
    </property>
   
    <property name="credits" type="integer" insert="false">
        <column name="credits" default="10"/>
    </property>
    <version name="version" type="integer" insert="false">
        <column name="version" default="0"/>
    </property>
   
    <property name="balance" type="float">
        <column name="balance" sql-type="decimal(13,3)"/>
    </property>
   
    <property name="foo" type="integer">
      <column name="foo" check="foo > 10"/>
    </property>
    <class name="Foo" table="foos" check="bar < 100.0">
      ...
      <property name="bar" type="float"/>
    </class>
   
    <class name="Customer" table="CurCust">
      <comment>Current customers only</comment>
      ...
    </class>
   
Dialects
    DB2    org.hibernate.dialect.DB2Dialect
    DB2 AS/400    org.hibernate.dialect.DB2400Dialect
    DB2 OS390    org.hibernate.dialect.DB2390Dialect
    Firebird    org.hibernate.dialect.FirebirdDialect
    FrontBase    org.hibernate.dialect.FrontbaseDialect
    HypersonicSQL    org.hibernate.dialect.HSQLDialect
    Informix    org.hibernate.dialect.InformixDialect
    Interbase    org.hibernate.dialect.InterbaseDialect
    Ingres    org.hibernate.dialect.IngresDialect
    Microsoft SQL Server 2005    org.hibernate.dialect.SQLServer2005Dialect
    Microsoft SQL Server 2008    org.hibernate.dialect.SQLServer2008Dialect
    Mckoi SQL    org.hibernate.dialect.MckoiDialect
    MySQL    org.hibernate.dialect.MySQLDialect
    MySQL with InnoDB    org.hibernate.dialect.MySQL5InnoDBDialect
    MySQL with MyISAM    org.hibernate.dialect.MySQLMyISAMDialect
    Oracle 8i    org.hibernate.dialect.Oracle8iDialect
    Oracle 9i    org.hibernate.dialect.Oracle9iDialect
    Oracle 10g    org.hibernate.dialect.Oracle10gDialect
    Pointbase    org.hibernate.dialect.PointbaseDialect
    PostgreSQL 8.1    org.hibernate.dialect.PostgreSQL81Dialect
    PostgreSQL 8.2 and later    org.hibernate.dialect.PostgreSQL82Dialect
    Progress    org.hibernate.dialect.ProgressDialect
    SAP DB    org.hibernate.dialect.SAPDBDialect
    Sybase ASE 15.5    org.hibernate.dialect.SybaseASE15Dialect
    Sybase ASE 15.7    org.hibernate.dialect.SybaseASE157Dialect
    Sybase Anywhere    org.hibernate.dialect.SybaseAnywhereDialect
    H2    org.hibernate.dialect.H2Dialect


Automatic schema generation with SchemaExport
    代码
    Configuration cfg = ....;
    new SchemaExport(cfg).create(false, true);
   
    or  命令行
    java -cp hibernate_classpaths org.hibernate.tool.hbm2ddl.SchemaExport options mapping_files
    --quiet    do not output the script to standard output
    --drop    only drop the tables
    --create    only create the tables
    --text    do not export to the database
    --output=my_schema.ddl output the ddl script to a file
    --naming=eg.MyNamingStrategy select a NamingStrategy
    --config=hibernate.cfg.xml read Hibernate configuration from an XML file
    --properties=hibernate.properties read database properties from a file
    --format    format the generated SQL nicely in the script
    --delimiter=; set an end-of-line delimiter for the script

分享到:
评论

相关推荐

    J2EE利用Hibernate采用B/S架构网页设计

    HibernateSessionFactory.configFile = configFile; sessionFactory = null; } /** * return hibernate configuration * */ public static Configuration getConfiguration() { return configuration; } ...

    Eclipse数据库hibernate反向生成数据库类

    最后,我们可以在Eclipse菜单栏中的工具栏的空白处右击鼠标,选择Customize Perspective,勾选Hibernate Code Generation,然后单击确定,完成反向生成数据库实体类的配置。 通过以上步骤,我们可以成功地使用...

    毕业设计项目源码:基于JavaEE的智能旧书交易平台设计与实现.zip

    或根据bookMarket-java/src/main/resources/config/hibernate.properties配置信息创建数据库 在bookMarket-java/src/main/resources/config/hibernate.properties中更新数据库配置 使用maven命令重新打包bookMarket-

    巴巴运动网Code

    【巴巴运动网Code】是一个大型的电子商务平台,其核心技术栈基于Java编程语言,结合了Struts框架和Spring框架。这个项目展示了如何运用这些技术来构建一个功能完善的网上购物系统。Struts是Apache软件基金会的一个...

    xmemcached 中文开发手册

    - 与Hibernate-memcached集成,使得memcached可以无缝作为Hibernate的二级缓存。 8. **客户端连接池**: - 即便在单个memcached节点上也能通过创建多个连接的方式构建连接池,从而提升客户端在高并发环境下的处理...

    code-generator:通用jdbc代码生成器,使用自定义免费标记模板为每个使用关系型数据库项目生成通用代码

    通用JDBC代码生成器通过xml配置文件和用户...除了利用代码生成,也可以写模板生成数据库说明文档快速开始修改src / test / resources / codeGenerator / config.xml中的jdbc信息运行代码生成器src / test / java / ...

    mybatis笔记------

    MyBatis 是一个流行的 Java 持久层框架,它源于 Apache 的 iBatis 项目,并在 2010 年转到 Google Code,随后在 2013 年迁移至 Github。MyBatis 提供了一个简单而强大的 SQL 映射框架,它支持定制化 SQL、存储过程...

    Eclipse 插件集合

    3.Code Folding 加入多种代码折叠功能(比eclipse自带的更多) 4.Easy Explorer 打开在eclipse中选定文件所在的目录。 5.Fat Jar 打包插件,可以方便的完成各种打包任务,可以包含外部的包等 6.RegEx Test ...

    Castle AR快速入门

    // Code that runs on application startup Castle.ActiveRecord.Framework.IConfigurationSource source = System.Configuration.ConfigurationManager.GetSection("activerecord") as Castle.ActiveRecord....

    jeecg3.4.3平台代码及文档

    - `config.properties`,也用于配置数据库连接,其中特别注意`hibernate.hbm2ddl.auto`参数的设置,根据是否需要自动创建表来选择`create`或`none`。 3. 创建数据库:建立一个UTF-8编码的数据库,数据库名称推荐为...

    Mybatis讲义

    - **起源**: MyBatis 最初源自 Apache 的一个开源项目 iBatis,后来该项目迁移至 Google Code 并更名为 MyBatis,最终在 2013 年 11 月迁移到 GitHub 上继续开发维护。 - **定义**: 官方定义 MyBatis 是一个优秀的...

    Mybatis课件(基于传智修改)

    2010年,该项目由Apache迁移到Google Code并更名为Mybatis,并在此基础上升级至3.X版本。随着项目的不断发展,自3.2版本开始,Mybatis迁移至GitHub进行维护,目前最新的稳定版本为3.2.8。 **特点**: - **轻量级...

    mybatis笔记mybatis笔记mybatis笔记

    MyBatis 是一个流行的Java持久层框架,它源自Apache的iBatis项目,后来在2010年迁移到Google Code,并在2013年进一步转移到GitHub。MyBatis的主要特点在于它允许开发者自定义SQL查询,支持存储过程,以及高级的映射...

    struts in action

    通常这样的资源会包含多个章节,每个章节详细阐述Struts的不同方面,如Action类、配置文件(struts-config.xml)、Form Beans、Tiles布局、国际化、异常处理、以及与Spring或Hibernate等其他框架的整合等。...

    尚硅谷 Java 技术之 MyBatis1

    MyBatis 是一个强大的持久层框架,起源于 Apache 的 iBatis 项目,后来转移到 Google Code,最终在 Github 上发展。它允许开发者编写定制化的 SQL、存储过程,并提供了对 Java POJOs 的高级映射,避免了手动处理 ...

    myeclipse.8.6.注册码

    MyEclipse支持多种开发框架,如Spring、Struts、Hibernate等,并且具备了诸如代码编辑、调试、构建、测试等一系列功能,极大地提高了开发效率。 ### 二、注册码的作用 在MyEclipse中,注册码(或称许可证密钥)是...

    IKAnalyzer中文分词器V3.1.1使用手册

    2. 将`IKAnalyzer.cfg.xml`配置文件放置在项目的根目录下(对于Web项目,通常是`WEB-INF/classes`目录,与hibernate、log4j等配置文件放置在一起)。 **2.3 Lucene用户快速入门** 为了帮助Lucene用户快速上手,...

    CentrSourceCode-ce source code

    文件夹结构可能是按照功能模块划分,如:`src/main`可能包含应用程序的主要代码,`src/test`用于存放单元测试代码,`docs`存储项目文档,而`config`则包含配置文件。`CentrSourceCode-master`可能指的是主分支或者...

Global site tag (gtag.js) - Google Analytics