• hibernate 一对多关联


    package com.bjsxt.hibernate;

    import java.util.HashSet;
    import java.util.Set;

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.OneToMany;
    import javax.persistence.Table;

    @Entity
    @Table(name="t_group")
    public class Group {
        private int id;
        private String name;
        private Set<User> users = new HashSet<User>(); //用set可以排除重复
        @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;
        }
        @OneToMany
        @JoinColumn(name="groupId")//加上外键映射关系,不加上会做中间表
        public Set<User> getUsers() {
            return users;
        }
        public void setUsers(Set<User> users) {
            this.users = users;
        }
    }

    package com.bjsxt.hibernate;

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

    @Entity
    @Table(name="t_user")
    public class User {
        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;
        }
    }

    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.bjsxt.hibernate.Group" table="t_group">
            <id name="id">
                <generator class="native"></generator>
            </id>
           
            <property name="name"></property>
            <set name="users">
                <key column="groupId"></key> <!--  指定关联字段的名字叫什么 -->   
                <one-to-many class="com.bjsxt.hibernate.User"/>
            </set>
        </class>
       
    </hibernate-mapping>

    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.bjsxt.hibernate.User" table="t_user">
            <id name="id">
                <generator class="native"></generator>
            </id>
           
            <property name="name"></property>
           
        </class>
       
    </hibernate-mapping>

    测试类

    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);
        }
       
        public static void main(String[] args) {
            beforeClass();
        }
    }

  • 相关阅读:
    053(九)
    方法的重载
    方法的重写(override / overwrite)
    属性与局部变量的对比
    面向对象基础知识合集:对象的产生、对象的生命周期、内存解析
    使用二维数组打印一个 10 行杨辉三角
    数组中的常见异常: 1. 数组角标越界的异常:ArrayIndexOutOfBoundsExcetion 2. 空指针异常:NullPointerException
    快速排序
    * 数组的冒泡排序的实现
    * 二维数组的使用
  • 原文地址:https://www.cnblogs.com/flying607/p/3478266.html
Copyright © 2020-2023  润新知