• 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();
        }
    }

  • 相关阅读:
    C# 获取文件的修改时间、访问时间、创建时间
    Nhibernate Or多条件查询
    C# 将GridView当前页数据导成Execl
    C# 清空文件夹
    TreeView默认收缩
    JS控制控件的隐藏显示
    div置顶,不随滚动条滚动而滚动
    js 父窗体与子窗体的调用
    树形菜单的绑定以及链接
    2010.10.16 OA项目组一周报告 CQ
  • 原文地址:https://www.cnblogs.com/flying607/p/3478266.html
Copyright © 2020-2023  润新知