zoukankan      html  css  js  c++  java
  • Hibernate- 表联系

    hibernate.cfg.xml 配置文件:连接数据库

    <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    
    <hibernate-configuration>
        <session-factory>
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_many2one</property>
            <property name="hibernate.connection.username">root</property>
            <property name="hibernate.connection.password">bjpowernode</property>
            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
            <property name="hibernate.show_sql">true</property>
            
           <!-- 数据库表-->
             <mapping resource="com/bjpowernode/hibernate/User.hbm.xml"/>
             <mapping resource="com/bjpowernode/hibernate/Group.hbm.xml"/>
    
        </session-factory>
    </hibernate-configuration>
    View Code

    many-t-one

    Group.hbm.xml

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
        <class name="com.bjpowernode.hibernate.Group" table="t_group">
            <id name="id">
                <generator class="native"/>
            </id>
            <property name="name"/>
        </class>
    </hibernate-mapping>
    View Code

    User.hbm.xml

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
        <class name="com.bjpowernode.hibernate.User" table="t_user">
            <id name="id">
                <generator class="native"/>
            </id>
            <property name="name"/>
            <many-to-one name="group" column="groupid" cascade="save-update"/>
        </class>
    </hibernate-mapping>
    View Code

    one-to-one

    IdCard.hbm.xml

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
        <class name="com.bjpowernode.hibernate.IdCard" table="t_idCard">
            <id name="id">
                <generator class="native"/>
            </id>
            <property name="cardNo"/>
        </class>
    </hibernate-mapping>
    View Code

    Person.hbm.xml

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
        <class name="com.bjpowernode.hibernate.Person" table="t_person">
            <id name="id">
                <!-- 采用foreign生成策略,foreign会取得关联对象的标示 -->
                <generator class="foreign">
                    <!-- property 指关联对象 -->
                    <param name="property">idCard</param>
                </generator>
            </id>
            <property name="name"/>
            <!--
             one-to-one 指示hibernate如何加载其关联对象,默认根据主键加载,
             也就是拿到关联字段值,根据对端的主键来加载关联对象
             
             constrained="true"表示,当前主键(person的主键)还是一个外键
            参照了对端的主键(IdCard的主键),也就是会生成外键约束语句
             -->
            <one-to-one name="idCard" constrained="true"/>
            
        </class>
    </hibernate-mapping>
    View Code
  • 相关阅读:
    .NET Core部署到linux(CentOS)最全解决方案,常规篇
    C#规则引擎RulesEngine
    Angular—都2019了,你还对双向数据绑定念念不忘
    HTTP Error 500.30
    ConsoleUDPServer与ConsoleUDP
    基于WMI获取USB设备信息(即获取插即用设备信息)System.Management.ManagementObjectSearcher--ManagementObjectCollection
    基于WMI获取USB设备信息(即获取插即用设备信息)----WMI使用的WIN32_类库名
    基于WMI获取USB设备信息(即获取插即用设备信息)System.Management.ManagementObjectSearcher--ManagementObjectCollection
    C#使用Emgu.CV.dll进行图像处理---使用EmguCV获取摄像头和读取视频
    Windows 10系统“家庭版”到“专业版”的转换
  • 原文地址:https://www.cnblogs.com/yinweitao/p/5937920.html
Copyright © 2011-2022 走看看