使用数据库:Oracle,平台Eclipse 4.14 jdk 1.8
指定表格所属的schema(数据库根据schema进行管理)
1.xxx.hbm.xml (hibernate mapping xml)
< hibernate-mapping package=“hibernate>
< class name=“Bean类” table=“table名” schema=”指定的schema”>
😗 ************ 😗 < !-- 数据库表格对应实体的字段-- >
< /class>
< /hibernate-mapping >
2.hibernate.cfg.xml(configure xml)
< property name=“hibernate.default-schema”>< /property>
3.@Table(name=“table”,schema=“指定的schema”)
hibernate反向创建表
xxxxx.cfg.xml
< property name=“hbm2ddl.auto">XXXX< /property>
XXXX{create , update}
create:覆盖新表
update:若无,创建。若有,对旧表进行更新
< property name=“hibernate.show_sql”>true< /property>
于日志中显示执行的h/s ql语句
cfg.xml的< mapping resource=”xxxx“/ >
指定用于映射数据库表格的文件
-
hbm.xml
< mapping resource=“xxx.hbm.xml”>
< hibernate-mapping package=“hibernate>
< class name=“Bean类” table=“table名” schema=”指定的schema”>< id name="id" type=”int“> < column name="编号"> < generator class="sequence"> < param class="sequence_name_in_use">AUTOID< /param> < !-- hibernate5.x版本及以后 --> < /generator> < /column> < /id> < property name="name" type="java.lang.String"> < column name="姓名"> < /property > :* ************ :* < !-- 数据库表格对应实体的字段-- >
< /class>
< /hibernate-mapping > -
PO (Persistence Object)
< mapping class=“bean类”/>
JPA提供的四种标准用法为TABLE,SEQUENCE,IDENTITY,AUTO.
TABLE:使用一个特定的数据库表格来保存主键。
SEQUENCE:根据底层数据库的序列来生成主键,条件是数据库支持序列。 Oracle
IDENTITY:主键由数据库自动生成(主要是自动增长型) MySQL
AUTO:主键由程序控制。
@Entity
@Table(name="***",schema=“xxx”)
public class Bean{
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE)
//members
//constructers
//setter getter
}
SessionFactory的初始化
configures settings from hibernate.cfg.xml
-
Hibernate 4.2/4.3
(configuration→buildSessionFactory)// A SessionFactory is set up once for an application
sessionFactory = new Configuration()
.configure()
.buildSessionFactory(); -
Hibernate 5.0/5.1/5.2 (StandardServiceRegistryBuilder→MetadataSources→SessionFactory)
支持上述方法,hibernate向后兼容
// A SessionFactory is set up once for an application!
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure()
.build();
try {
sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
}
catch (Exception e) {
//destroy it manually.
StandardServiceRegistryBuilder.destroy( registry );
}
- SSH框架合并 springContext.xml(可自定义命名)
c3p0+LocalSessionFactoryBean
demo as the following
<beans
****************>
<context:component-scan base-package="org.sxf.com.rj174211">
</context:component-scan>
<context:property-placeholder location="classpath:db.properties"/>
<bean id ="dataSource" class="com.mchange.v2.c3p0.
ComboPooledDataSource">
<property name="user" value="${jdbc.user}">
</property>
<property name="password" value="${jdbc.password}">
</property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}">
</property>
<property name="driverClass" value="${jdbc.driverClass}">
</property>
<property name="initialPoolSize" value="${jdbc.initialPoolSize}">
</property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}">
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.
LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource">
</property>
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
</beans>
创建DAO需要使用的Session的方法
save(Object)
update(Object)
delete(Object)
get(class,id)
createQuery(“FROM Table”).list(); //可以额外添加select等子句
createQuery(“FROM Table”).uniqueResult();