zoukankan      html  css  js  c++  java
  • 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>

  • 相关阅读:
    Java 异常Exception e中e的getMessage()和toString()以及 e.printStackTrace();方法的区别
    js几秒以后倒计时跳转示例
    Java读取property配置文件
    js 设置下拉框的默认值
    JS的可枚举性
    Object的原型拷贝-create、assign、getPrototypeOf 方法的结合
    JS 事件循环机制
    vue nextTick深入理解-vue性能优化、DOM更新时机、事件循环机制
    vue 实战问题-watch 数组或者对象
    vue2.0读书笔记2-进阶
  • 原文地址:https://www.cnblogs.com/flying607/p/3477107.html
Copyright © 2011-2022 走看看