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

posted @ 2013-12-17 13:23  剑握在手  阅读(149)  评论(0编辑  收藏  举报
返回顶部↑