hibernate映射文件问题

hibernate映射文件问题,第1张

1,针对第一个问题,如果你不指定 table属性的话,生成的数据库表名就和类名一致。也就是你可以通过该属性设置生成的表的名称。

2,关于第二个问题,type是用于指定属性的数据类型的,如果不指定该属性则由hibernate自行判断该属性的数据类型,通常建议设置该属性,这会保证程序有更好的性能。

是通过映射文件来映射的。举个简单的例子,例如我有个Teacher实体类(某教学管理系统老师用户):

private Integer id //编号

private String name //姓名

private String password //密码

private Date createDate //创建日期

同时,我创建了数据表TEACHER:

create table teacher( id number(9) primary key, --编号 name varchar2(20), --姓名 password varchar2(20), --密码 create_date date --创建日期 )

这时,怎么关联它们呢?通过加载hibernate框架,我们就可以使用映射文件来关联实体类和数据库了

文件名:Teacher.hbm.xml(必须与实体类名字一致)

内容:

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" " http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!-- Mapping file autogenerated by MyEclipse Persistence Tools-->

<hibernate-mapping>

<class name="com.yss.Teacher" table="TEACHER" schema="WZM">

<id name="id" type="java.lang.Integer">

<column name="ID" precision="9" />

</id>

<property name="name" type="java.lang.String">

<column name="NAME" length="20" />

</property>

<property name="password" type="java.lang.String">

<column name="PASSWORD" length="20" />

</property>

<property name="createDate" type="java.util.Date">

<column name="CREATE_DATE" length="7" />

</property>

</class>

</hibernate-mapping>

当然,这只是比较简单的映射,还有什么多对多,一对多等映射,并且映射时如何设置以达到需要的效果,这些你可以在网上搜索相关资料自行查看。

你看下我的配置 尤其是最后几行就明白了

<bean id="sessionFactory"

class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

<property name="dataSource" ref="dataSource" />

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">

org.hibernate.dialect.Oracle10gDialect

</prop>

<prop key="hibernate.show_sql">true</prop>

<!-- 配置二级缓存 -->

<prop key="hibernate.cache.use_second_level_cache">true</prop>

<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>

<prop key="hibernate.cache.use_query_cache">true</prop>

</props>

</property>

<!-- 读取 com.eshore.order 下的所有的 hbm.xml 文件 -->

<property name="mappingLocations"

value="classpath*:com/eshore/order/**/*.hbm.xml" />

</bean>


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/11681002.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存