`
ponlya
  • 浏览: 159585 次
  • 性别: 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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics