Hibernate映射文件创建和删除触发器,存储过程等数据库对象

创建表,触发器,和存储过程等数据库对象,这里一定要用create

在hibernate.cfg.xml文件中

在Hibernate的映射文件中

同时还可以指定对什么数据库有效

News.hbm.xml

 1 <?xml version='1.0' encoding='utf-8'?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5 <!-- hibernate-mapping 是映射文件的根元素-->
 6 <hibernate-mapping package="hibernate">
 7     <!--每个class元素对应一个持久化对象-->
 8     <class name="News" table="news_table">
 9         <id name="id">
10             <!-- 指定主键生成策略-->
11             <generator class="identity"/>
12         </id>
13         <!-- property元素定义常规属性-->
14         <property name="title"/>
15         <property name="content"/>
16         <!--通过formula指定该属性值没有对应的实际数据列
17             该属性值将由系统根据表达式来生成-->
18         <!--<property name="fullContent"
19                   formula="(select concat(nt.title,nt.content)
20                   from news_table nt where nt.id = id)"/>-->
21         <property name="fullContent" column="full_content" type="string" generated="insert"/>
22     </class>
23     <database-object>
24         <!--定义创建数据库对象的语句-->
25         <create>
26             CREATE TRIGGER t_full_content_gen BEFORE INSERT ON news_table
27                 FOR EACH ROW BEGIN set new.full_content = CONCAT(new.title,new.content);
28             END;
29         </create>
30         <!--让drop元素为空,不删除任何对象-->
31         <drop></drop>
32         <!--指定仅对MySQL数据库有效-->
33         <dialect-scope name="org.hibernate.dialect.MySQLDialect"></dialect-scope>
34         <dialect-scope name="org.hibernate.dialect.MySQLInnoDBDialect"></dialect-scope>
35         <dialect-scope name="org.hibernate.dialect.MySQL5InnoDBDialect"></dialect-scope>
36     </database-object>
37 </hibernate-mapping>

 

posted @ 2016-03-18 11:21  路途寻码人  阅读(792)  评论(0编辑  收藏  举报