Hibernate JPA方式配置

如果使用JPA的目的只是利用Annotation的特性取代XML映射配置文件,并且你也不想使用JPA的编程接口或查询语言,想使用hibernate和HQL,只需在hibernate.cfg.xml中的<mapping class=""/> 注意这里的属性是class,不是resource(引用类路径上的资源配置文件xml)。同时用AnnotationConfiguration替代原来的Configuration来buildSessionFactory。

也可以使用原生的JPA编程接口,hibernate EntityManager提供了实现。

JPA基本配置:src/META-INF/persistence.xml

     <persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
             version="1.0">
    <persistence-unit name="persistUnitName" transaction-type="RESOURCE_LOCAL">
        <class>org.hibernate.ejb.HibernatePersistence</class>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value="××××"/>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost/jpadb"/>
            <property name="hibernate.hbm2ddl.auto" value="create"/>
            <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
        	<property name="hibernate.show_sql" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

一个HSQL的配置

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
	xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
	<persistence-unit name="jpatest">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
		<class>com.lephix.entity.Car</class>
		<properties>
			<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
			<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
			<property name="hibernate.connection.username" value="sa" />
			<property name="hibernate.connection.password" value="" />
			<property name="hibernate.connection.url" value="jdbc:hsqldb:hsql://localhost" />
			<property name="hibernate.max_fetch_depth" value="1" />
			<property name="hibernate.hbm2ddl.auto" value="update" />
			<!-- 调整JDBC抓取数量的大小: Statement.setFetchSize() -->
			<property name="hibernate.jdbc.fetch_size" value="18" />
			<!-- 调整JDBC批量更新数量 -->
			<property name="hibernate.jdbc.batch_size" value="10" />
			<!-- 显示最终执行的SQL -->
			<property name="hibernate.show_sql" value="true" />
			<!-- 格式化显示的SQL -->
			<property name="hibernate.format_sql" value="true" />
		</properties>
	</persistence-unit>
</persistence>

 或则引用hibernate.cfg.xml

<persistence-unit name="persistUnitName">    
    <properties>    
        <property name="hibernate.ejb.cfgfile" value="hibernate.cfg.xml"/>    
    </properties>    
   </persistence-unit>  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值