• hibernate 关系映射之 单向外键关联一对一


    这里的关系指的是对象与对象之间的关系

    注解方式单向关联一对一:

    //这个类描述的husband是一个对应一个wife的

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.OneToOne;

    @Entity
    public class Husband {
        private int id;
        private String name;
        private Wife wife;
        @Id
        @GeneratedValue
        public int getId() {
            return id;
        }
       
        public String getName() {
            return name;
        }
       @OneToOne
       @JoinColumn(name="wifeId")//指定生成的表该外键的字段名,不指定会自动生成系统默认的字段名wife_id
        public Wife getWife() {
            return wife;
        }
        public void setId(int id) {
            this.id = id;
        }
        public void setName(String name) {
            this.name = name;
        }
        public void setWife(Wife wife) {
            this.wife = wife;
        }
       
    }

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;

    @Entity
    public class Wife {
        private int id;
        private String name;
       
        @Id
        @GeneratedValue
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
       
    }

    测试类:

    package com.bjsxt.hibernate;

    import java.util.Date;

    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.AnnotationConfiguration;
    import org.hibernate.tool.hbm2ddl.SchemaExport;
    import org.junit.AfterClass;
    import org.junit.BeforeClass;
    import org.junit.Test;

    public class HibernateORMappingTest {
        private static SessionFactory sessionFactory;
       
        //@BeforeClass
        public static void beforeClass() {
                sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        }
        //@AfterClass
        public static void afterClass() {
            sessionFactory.close();
        }
       

        @Test
        public void testSchemaExport() {
            new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);//配置了log4j可以看到生成语句
        }
       
        public static void main(String[] args) {
            beforeClass();
        }
    }

    配置文件

    <hibernate-configuration>

    ……

            <mapping class="com.bjsxt.hibernate.Husband"/>
            <mapping class="com.bjsxt.hibernate.Wife"/>

        </session-factory>

    </hibernate-configuration>

        </session-factory>

    </hibernate-configuration>

    xml方式:

     StuIdCard.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.bjsxt.hibernate.StuIdCard">
            <id name="id">
                <generator class="native"></generator>
            </id>
           
            <property name="num"/>
            <many-to-one name="student" column="studentId" unique="true"></many-to-one><!-- unique唯一的,manytoone是多对一,加上unique就是一对一了,表示StuIdCard单向一对一对应 student,name设为本类的student属性(StuIdCard类中要定义private Student student属性),column表示用哪个字段作为外键关联字段 -->
        </class>
       
    </hibernate-mapping>

  • 相关阅读:
    CentOS7静默安装WebLogic 12c
    yocto:bitbake单独编译某个模块的方法
    git config 查看配置信息
    文件上传bypass jsp内容检测的一些方法
    开始
    阿里云在线扩容磁盘(踩坑)笔记
    ifort 编译报错
    C语言测试题
    【Linux】find笔记
    Add User Snippet to VS Code
  • 原文地址:https://www.cnblogs.com/flying607/p/3477107.html
Copyright © 2020-2023  润新知