码迷,mamicode.com
首页 > 数据库 > 详细

1、Hibernate注解实现对象关联映射并自动生成数据库表

时间:2015-08-01 14:04:27      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

---恢复内容开始---

前言:本例单独使用Hibernate框架通过Hibernate注解实现年级表和学生表的一对多关联并完成其级联操作

1、年级表

package pojo;

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

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
//注解实体类,指定数据库表grade,数据库名school
@Entity
@Table(name="grade",catalog="school")
public class Grade {
    private int grade_id;
    private String grade_name;
    private Set<Student> students=new HashSet();
    //定义主键生成方式,注解主键,设置id的主键生成方式,数据库表字段关联
    @GenericGenerator(name="generator",strategy="assigned")
    @Id
    @GeneratedValue(generator="generator")
    @Column(name="grade_id",unique=true,nullable=false)
    public int getGrade_id() {
        return grade_id;
    }
    public void setGrade_id(int grade_id) {
        this.grade_id = grade_id;
    }
    @Column(name="grade_name",length=50)
    public String getGrade_name() {
        return grade_name;
    }
    public void setGrade_name(String grade_name) {
        this.grade_name = grade_name;
    }
    //设置一对多映射,允许全部级联操作,延迟加载,指定关联表的关联属性
    @OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY,mappedBy="grade")
    public Set<Student> getStudents() {
        return students;
    }
    public void setStudents(Set<Student> students) {
        this.students = students;
    }
}

2、学生表

package pojo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name="student",catalog="school")
public class Student {
    private int student_id;
    private String student_name;
    private Grade grade;
    @GenericGenerator(name="generator",strategy="increment")
    @Id
    @GeneratedValue(generator="generator")
    @Column(name="id",unique=true,nullable=false)
    public int getStudent_id() {
        return student_id;
    }
    public void setStudent_id(int student_id) {
        this.student_id = student_id;
    }
    @Column(name="student_name",length=50)
    public String getStudent_name() {
        return student_name;
    }
    public void setStudent_name(String student_name) {
        this.student_name = student_name;
    }
    //延迟加载,设置学生表的外键
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="grade_id")
    public Grade getGrade() {
        return grade;
    }
    public void setGrade(Grade grade) {
        this.grade = grade;
    }
}

3、Hibernate配置文件hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
    <!--数据库连接相关-->
    <property name="connection.driver_class">
        com.mysql.jdbc.Driver
    </property>
    <property name="connection.url">
        jdbc:mysql://localhost:3306/school?characterEncoding=utf8
    </property>
    <property name="connection.username">root</property>
    <property name="connection.password">123456</property>

    <!-- 辅助参数 --><!--数据库方言,提示HIbernate使用恰当的sql语句-->
    <property name="dialect">
        org.hibernate.dialect.MySQLDialect
    </property>
    <!-- 将当前会话绑定到线程 -->
    <property name="current_session_context_class">thread</property>
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    
    <!-- 添加使用Hibernate注解的实体Bean,而不是映射文件 -->
    <mapping class="pojo.Grade" />
    <mapping class="pojo.Student"/>
</session-factory>
</hibernate-configuration>

 

---恢复内容结束---

1、Hibernate注解实现对象关联映射并自动生成数据库表

标签:

原文地址:http://www.cnblogs.com/6da6da/p/4693947.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!