下载了别人网上商场的项目,想跑起来看看效果,没想到开始没有表,等建了表之后发现没有数据。实乃悲剧。
不过还好在 建表的时候 学习了很多 hibernate.hbm.xml 与oracle 的映射问题。 可以纪录下来,算是 自己的只是积累吧。
首先在hibernate 文件中 会有配置:
users.hbm.xml:
<hibernate-mapping>
<class name="com.test.Users" table="USERS" schema="hr"> <id name="userid" type="java.lang.Integer">
<column name="USERID" precision="22" scale="0" />
<generator class="sequence">
<param name="sequence">USERID_SQ</param>
</generator>
</id> <many-to-one name="userlevel" class="com.test.Userlevel" fetch="select"> <column name="LVLID" precision="22" scale="0" not-null="true"/> </many-to-one> <many-to-one name="usergroup" class="com.y7g.shop.bean.Usergroup" fetch="select">
<column name="GRPID" precision="22" scale="0" not-null="true" />
</many-to-one>
<property name="uaccount" type="java.lang.String">
<column name="UACCOUNT" length="20" not-null="true" unique="true" />
</property>
<property name="upass" type="java.lang.String">
<column name="UPASS" length="50" not-null="true" />
</property>
<property name="uname" type="java.lang.String">
<column name="UNAME" length="50" not-null="true" />
</property>
<property name="age" type="java.lang.Short">
<column name="AGE" precision="3" scale="0" />
</property>
<property name="gender" type="java.lang.String">
<column name="GENDER" length="3" />
</property>
<property name="headimg" type="java.lang.String">
<column name="HEADIMG" length="300" />
</property>
<property name="tel" type="java.lang.String">
<column name="TEL" length="25" />
</property>
<property name="msn" type="java.lang.String">
<column name="MSN" length="100" />
</property>
<property name="email" type="java.lang.String">
<column name="EMAIL" length="100" />
</property>
<property name="lastlog" type="java.lang.String">
<column name="LASTLOG" />
</property>
<property name="regtime" type="timestamp">
<column name="REGTIME" />
</property>
<property name="regip" type="java.lang.String">
<column name="REGIP" length="20" not-null="true" />
</property>
<property name="uaddress" type="java.lang.String">
<column name="UADDRESS" length="200" />
</property>
<set name="postses" inverse="true">
<key>
<column name="USERID" precision="22" scale="0" not-null="true" />
</key>
<one-to-many class="com.y7g.shop.bean.Posts" />
</set>
<set name="orderses" inverse="true">
<key>
<column name="USERID" precision="22" scale="0" not-null="true" />
</key>
<one-to-many class="com.y7g.shop.bean.Orders" />
</set>
<set name="carts" inverse="true">
<key>
<column name="USERID" precision="22" scale="0" />
</key>
<one-to-many class="com.y7g.shop.bean.Cart" />
</set>
</class>
</hibernate-mapping>
因为Oracle 不像MySql 那样带有主键自增功能 所以需要建立sequence和trigger
CREATE SEQUENCE USERID_SQ
INCREMENT BY 1
START WITH 1
NOMAXVALUE
NOCYCLE
NOCACHE;
CREATE OR REPLACE TRIGGER TRI_USERS
BEFORE INSERT ON USERS FOR EACH ROW WHEN(NEW.USERID IS NULL)
BEGIN
SELECT USERID_SQ.NEXTVAL INTO:NEW.USERID FROM DUAL;
END;
其次这个对象 里面有 多对一和 一对多的情况。
需要 根据多对一的情况 建立外键 关联上相对应 “一”的表。
ALTER TABLE USERS ADD CONSTRAINT FK_USERS_USERLEVEL FOREIGN KEY (LVLID)
REFERENCES USERLEVEL (LVLID);
ALTER TABLE USERS ADD CONSTRAINT FK_USERS_USERGROUP FOREIGN KEY (GRPID)
REFERENCES USERGROUP (GRPID);
至于一对多的情况 ,则需要在多的那个对象里 做外键关联。
对于多对一 和一对多 其实 我还有点迷糊,现在先做到会用。等慢慢的开发过程中再循序渐进的 体会其中的奥秘!
本文分享了使用Hibernate框架在Oracle数据库中进行表映射的实际经验,包括如何配置sequence和trigger实现主键生成,以及多对一和一对多关联的处理。

1143

被折叠的 条评论
为什么被折叠?



