hibernate与DB的映射问题

 下载了别人网上商场的项目,想跑起来看看效果,没想到开始没有表,等建了表之后发现没有数据。实乃悲剧。

不过还好在 建表的时候 学习了很多 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);

至于一对多的情况 ,则需要在多的那个对象里 做外键关联。

对于多对一 和一对多 其实 我还有点迷糊,现在先做到会用。等慢慢的开发过程中再循序渐进的 体会其中的奥秘!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值