7.8--SSH学习之注解关联映射

之前记过配置文件方式的关联映射
可以参考:http://blog.csdn.net/su1573/article/details/74615709
现在记一下,用注解方式写的关联映射

单向多对一

只在多端配置;

多端:

@Entity(name="TABLE_STU")    //Entity标识本类为一个POJO类,name属性代表由这个类映射生成的表名//@TABLE(name="TABLE_STU"),还有其他属性,用table
public class Student {
    private Integer stuId;
    private String stuName;
    private String stuGender;
    private Integer stuAge;
    //单向多对一
    private Classes classes;

    @Id      //该属性为主键
    @GeneratedValue(strategy=GenerationType.AUTO)   //设置主键生成规则,AUTO为数据库默认生成主键

//  @GeneratedValue(strategy=GenerationType.IDENTITY)  //设置主键生成规则,IDENTITY使用数据库自增列来生成主键,适用于MySQL,SqlServer,DB2等

//  @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="stuSeq")   //SEQUENCE使用序列作为主键生成规则,适用于Oracle,DB2等
//  @SequenceGenerator(name="stuSeq",sequenceName="STU_SEQ",initialValue=1,allocationSize=1)    //产生一个名为STU_SEQ的新序列,主键引用这个新序列
                                                        //initialValue从1开始,allocationSize每次加一

//  @GeneratedValue(generator="currGen")    //引用名为currGen的主键为生成规则,
//  @GenericGenerator(name="currGen",strategy="uuid")  //strategy="uuid"为uuid方式产生主键,strategy="assigned"手动插入产生规则

    @Column(name="STU_ID")            //括号中可以指定类型,columnDefinition="timestap",length,(insertable=false,增加数据时,这个列不出现在新增语句中)
    public Integer getStuId() {        //该属性对应的列名
        return stuId;                   
    }
    public void setStuId(Integer stuId) {
        this.stuId = stuId;
    }

    @Column(name="STU_NAME",length=200)
    public String getStuName() {
        return stuName;
    }
    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    @Column(name="STU_GENDER" ,length=50)
    public String getStuGender() {
        return stuGender;
    }
    public void setStuGender(String stuGender) {
        this.stuGender = stuGender;
    }

    @Column(name="STU_AGE")
    public Integer getStuAge() {
        return stuAge;
    }
    public void setStuAge(Integer stuAge) {
        this.stuAge = stuAge;
    }
    //单向多对一
    @ManyToOne(targetEntity=Classes.class,fetch=FetchType.EAGER,cascade=CascadeType.ALL)
    @JoinColumn(name="CLASSES_ID")                //cascade={CascadeType.PERSIST,CascadeType.MERGE}不能级联增加,但是all 可以
    //多对一
    public Classes getClasses() {
        return classes;
    }
    public void setClasses(Classes classes) {
        this.classes = classes;
    }
}


一端:

@Entity(name="TABLE_CLASSES")
public class Classes {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="CLASSES_ID")
    private Integer classesId;

    @Column(name="CLASSES_NAME")
    private String classesName;
    //一对多
//  @OneToMany(targetEntity=Student.class,fetch=FetchType.EAGER,cascade=CascadeType.ALL)   //,mappedBy="classes"双向的时候写
//  @JoinColumn(name="CLASSES_ID")
//  private Set<Student> stuSet = new HashSet<Student>();
    //一对多
    public Integer getClassesId() {
        return classesId;
    }
    public void setClassesId(Integer classesId) {
        this.classesId = classesId;
    }
    public String getClassesName() {
        return classesName;
    }
    public void setClassesName(String classesName) {
        this.classesName = classesName;
    }
//  public Set<Student> getStuSet() {
//      return stuSet;
//  }
//  public void setStuSet(Set<Student> stuSet) {
//      this.stuSet = stuSet;
//  }
}



单向一对多

只在一段配置;

一端:

@Entity(name="TABLE_CLASSES")
public class Classes {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="CLASSES_ID")
    private Integer classesId;

    @Column(name="CLASSES_NAME")
    private String classesName;
    //一对多
    @OneToMany(targetEntity=Student.class,fetch=FetchType.EAGER,cascade=CascadeType.ALL)   //,mappedBy="classes"双向的时候写
    @JoinColumn(name="CLASSES_ID")
    private Set<Student> stuSet = new HashSet<Student>();
    //一对多
    public Integer getClassesId() {
        return classesId;
    }
    public void setClassesId(Integer classesId) {
        this.classesId = classesId;
    }
    public String getClassesName() {
        return classesName;
    }
    public void setClassesName(String classesName) {
        this.classesName = classesName;
    }
    public Set<Student> getStuSet() {
        return stuSet;
    }
    public void setStuSet(Set<Student> stuSet) {
        this.stuSet = stuSet;
    }
}


多端:

@Entity(name="TABLE_STU")    //Entity标识本类为一个POJO类,name属性代表由这个类映射生成的表名//@TABLE(name="TABLE_STU"),还有其他属性,用table
public class Student {
    private Integer stuId;
    private String stuName;
    private String stuGender;
    private Integer stuAge;
    //单向多对一
//  private Classes classes;

    @Id      //该属性为主键
    @GeneratedValue(strategy=GenerationType.AUTO)   //设置主键生成规则,AUTO为数据库默认生成主键

//  @GeneratedValue(strategy=GenerationType.IDENTITY)  //设置主键生成规则,IDENTITY使用数据库自增列来生成主键,适用于MySQL,SqlServer,DB2等

//  @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="stuSeq")   //SEQUENCE使用序列作为主键生成规则,适用于Oracle,DB2等
//  @SequenceGenerator(name="stuSeq",sequenceName="STU_SEQ",initialValue=1,allocationSize=1)    //产生一个名为STU_SEQ的新序列,主键引用这个新序列
                                                        //initialValue从1开始,allocationSize每次加一

//  @GeneratedValue(generator="currGen")    //引用名为currGen的主键为生成规则,
//  @GenericGenerator(name="currGen",strategy="uuid")  //strategy="uuid"为uuid方式产生主键,strategy="assigned"手动插入产生规则

    @Column(name="STU_ID")            //括号中可以指定类型,columnDefinition="timestap",length,(insertable=false,增加数据时,这个列不出现在新增语句中)
    public Integer getStuId() {        //该属性对应的列名
        return stuId;                   
    }
    public void setStuId(Integer stuId) {
        this.stuId = stuId;
    }

    @Column(name="STU_NAME",length=200)
    public String getStuName() {
        return stuName;
    }
    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    @Column(name="STU_GENDER" ,length=50)
    public String getStuGender() {
        return stuGender;
    }
    public void setStuGender(String stuGender) {
        this.stuGender = stuGender;
    }

    @Column(name="STU_AGE")
    public Integer getStuAge() {
        return stuAge;
    }
    public void setStuAge(Integer stuAge) {
        this.stuAge = stuAge;
    }
    //单向多对一
//  @ManyToOne(targetEntity=Classes.class,fetch=FetchType.EAGER,cascade=CascadeType.ALL)
//  @JoinColumn(name="CLASSES_ID")                //cascade={CascadeType.PERSIST,CascadeType.MERGE}不能级联增加,但是all 可以
    //多对一
//  public Classes getClasses() {
//      return classes;
//  }
//  public void setClasses(Classes classes) {
//      this.classes = classes;
//  }
}



双向多对一

在多端配置:
@ManyToOne()
@JoinColumn()
在一端配置:
@OneToMany(….,mappedBy=”classes”)
mappedBy属性如果设置则表明这一段放弃控制权

多端:

@Entity(name="TABLE_STU")    //Entity标识本类为一个POJO类,name属性代表由这个类映射生成的表名//@TABLE(name="TABLE_STU"),还有其他属性,用table
public class Student {
    private Integer stuId;
    private String stuName;
    private String stuGender;
    private Integer stuAge;
    //单向多对一
    private Classes classes;

    @Id      //该属性为主键
    @GeneratedValue(strategy=GenerationType.AUTO)   //设置主键生成规则,AUTO为数据库默认生成主键

//  @GeneratedValue(strategy=GenerationType.IDENTITY)  //设置主键生成规则,IDENTITY使用数据库自增列来生成主键,适用于MySQL,SqlServer,DB2等

//  @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="stuSeq")   //SEQUENCE使用序列作为主键生成规则,适用于Oracle,DB2等
//  @SequenceGenerator(name="stuSeq",sequenceName="STU_SEQ",initialValue=1,allocationSize=1)    //产生一个名为STU_SEQ的新序列,主键引用这个新序列
                                                        //initialValue从1开始,allocationSize每次加一

//  @GeneratedValue(generator="currGen")    //引用名为currGen的主键为生成规则,
//  @GenericGenerator(name="currGen",strategy="uuid")  //strategy="uuid"为uuid方式产生主键,strategy="assigned"手动插入产生规则

    @Column(name="STU_ID")            //括号中可以指定类型,columnDefinition="timestap",length,(insertable=false,增加数据时,这个列不出现在新增语句中)
    public Integer getStuId() {        //该属性对应的列名
        return stuId;                   
    }
    public void setStuId(Integer stuId) {
        this.stuId = stuId;
    }

    @Column(name="STU_NAME",length=200)
    public String getStuName() {
        return stuName;
    }
    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    @Column(name="STU_GENDER" ,length=50)
    public String getStuGender() {
        return stuGender;
    }
    public void setStuGender(String stuGender) {
        this.stuGender = stuGender;
    }

    @Column(name="STU_AGE")
    public Integer getStuAge() {
        return stuAge;
    }
    public void setStuAge(Integer stuAge) {
        this.stuAge = stuAge;
    }
    //单向多对一
    @ManyToOne(targetEntity=Classes.class,fetch=FetchType.EAGER,cascade=CascadeType.ALL)
    @JoinColumn(name="CLASSES_ID")                //cascade={CascadeType.PERSIST,CascadeType.MERGE}不能级联增加,但是all 可以
    //多对一
    public Classes getClasses() {
        return classes;
    }
    public void setClasses(Classes classes) {
        this.classes = classes;
    }
}


一端:

@Entity(name="TABLE_CLASSES")
public class Classes {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="CLASSES_ID")
    private Integer classesId;

    @Column(name="CLASSES_NAME")
    private String classesName;
    //一对多
    @OneToMany(targetEntity=Student.class,fetch=FetchType.EAGER,cascade=CascadeType.ALL)   //,mappedBy="classes"双向的时候写
//  @JoinColumn(name="CLASSES_ID")
    private Set<Student> stuSet = new HashSet<Student>();
    //一对多
    public Integer getClassesId() {
        return classesId;
    }
    public void setClassesId(Integer classesId) {
        this.classesId = classesId;
    }
    public String getClassesName() {
        return classesName;
    }
    public void setClassesName(String classesName) {
        this.classesName = classesName;
    }
    public Set<Student> getStuSet() {
        return stuSet;
    }
    public void setStuSet(Set<Student> stuSet) {
        this.stuSet = stuSet;
    }
}



多对多

两端都要写配置;

学生端:

@Entity(name="TABLE_TWOSTU")    //Entity标识本类为一个POJO类,name属性代表由这个类映射生成的表名//@TABLE(name="TABLE_STU"),还有其他属性,用table
public class TwoStudent {
    private Integer stuId;
    private String stuName;
    private String stuGender;
    private Integer stuAge;

    private Set<Teacher> stuTea = new HashSet<Teacher>();

    @Id      //该属性为主键
    @GeneratedValue(strategy=GenerationType.AUTO)   //设置主键生成规则,AUTO为数据库默认生成主键
    @Column(name="STU_ID")            //括号中可以指定类型,columnDefinition="timestap",length,(insertable=false,增加数据时,这个列不出现在新增语句中)
    public Integer getStuId() {        //该属性对应的列名
        return stuId;                   
    }
    public void setStuId(Integer stuId) {
        this.stuId = stuId;
    }

    @Column(name="STU_NAME",length=200)
    public String getStuName() {
        return stuName;
    }
    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    @Column(name="STU_GENDER" ,length=50)
    public String getStuGender() {
        return stuGender;
    }
    public void setStuGender(String stuGender) {
        this.stuGender = stuGender;
    }

    @Column(name="STU_AGE")
    public Integer getStuAge() {
        return stuAge;
    }
    public void setStuAge(Integer stuAge) {
        this.stuAge = stuAge;
    }

    @ManyToMany(targetEntity=Teacher.class,fetch=FetchType.EAGER,cascade=CascadeType.ALL)
    @JoinTable(name="TABLE_TEA_STU",joinColumns={@JoinColumn(name="STU_ID")},inverseJoinColumns={@JoinColumn(name="TEA_ID")})
    public Set<Teacher> getStuTea() {
        return stuTea;
    }
    public void setStuTea(Set<Teacher> stuTea) {
        this.stuTea = stuTea;
    }
}


教师端:

@Entity
public class Teacher {
    private Integer teaId;
    private String teaName;
    private String teaPwd;
    private Set<TwoStudent> stuSet = new HashSet<TwoStudent>();


    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="TEA_ID")
    public Integer getTeaId() {
        return teaId;
    }
    public void setTeaId(Integer teaId) {
        this.teaId = teaId;
    }

    @Column(name="TEA_NAME", length=200,nullable=false)
    public String getTeaName() {
        return teaName;
    }
    public void setTeaName(String teaName) {
        this.teaName = teaName;
    }
    @Column(name="TEA_PWD", length=200,nullable=false)
    public String getTeaPwd() {
        return teaPwd;
    }
    public void setTeaPwd(String teaPwd) {
        this.teaPwd = teaPwd;
    }

    @ManyToMany(targetEntity=TwoStudent.class,cascade=CascadeType.ALL,fetch=FetchType.EAGER)
    @JoinTable(name="TABLE_TEA_STU",joinColumns={@JoinColumn(name="TEA_ID")},inverseJoinColumns={@JoinColumn(name="STU_ID")})
    public Set<TwoStudent> getStuSet() {
        return stuSet;
    }
    public void setStuSet(Set<TwoStudent> stuSet) {
        this.stuSet = stuSet;
    }
}



一对一

两端也都要配置;

学生端:

@Entity(name="TABLE_THREESTU")
public class ThreeStudent {
    private Integer stuId;
    private String stuName;
    private String stuGender;
    private Integer stuAge;
    private StudentCard studentCard;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="STU_ID")
    public Integer getStuId() {
        return stuId;
    }
    public void setStuId(Integer stuId) {
        this.stuId = stuId;
    }

    @Column(name="STU_NAME",length=200)
    public String getStuName() {
        return stuName;
    }
    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    @Column(name="STU_GENDER",length=50)
    public String getStuGender() {
        return stuGender;
    }
    public void setStuGender(String stuGender) {
        this.stuGender = stuGender;
    }

    @Column(name="STU_AGE")
    public Integer getStuAge() {
        return stuAge;
    }
    public void setStuAge(Integer stuAge) {
        this.stuAge = stuAge;
    }

    @OneToOne(targetEntity=StudentCard.class,cascade=CascadeType.ALL,fetch=FetchType.EAGER)
    @PrimaryKeyJoinColumn
    public StudentCard getStudentCard() {
        return studentCard;
    }
    public void setStudentCard(StudentCard studentCard) {
        this.studentCard = studentCard;
    }
}


学生卡端:

@Entity(name="TAB_CARD")
public class StudentCard {
    private Integer cardId;
    private String cardNum;
    private ThreeStudent threeStudent;

    @Id
    @GeneratedValue(generator="cardStuKey")
    @GenericGenerator(name="cardStuKey",strategy="foreign",parameters=@Parameter(name="property", value="threeStudent"))
    @Column(name="CARD_ID")
    public Integer getCardId() {
        return cardId;
    }
    public void setCardId(Integer cardId) {
        this.cardId = cardId;
    }

    @Column(name="CARD_NUM",length=200)
    public String getCardNum() {
        return cardNum;
    }
    public void setCardNum(String cardNum) {
        this.cardNum = cardNum;
    }

    @OneToOne(targetEntity=ThreeStudent.class,cascade=CascadeType.ALL,fetch=FetchType.EAGER,mappedBy="studentCard")
    @PrimaryKeyJoinColumn
    public ThreeStudent getThreeStudent() {
        return threeStudent;
    }
    public void setThreeStudent(ThreeStudent threeStudent) {
        this.threeStudent = threeStudent;
    }
}




配置文件中需要写:

         <mapping class="com.su.domain.Student"/>
        <mapping class="com.su.domain.Classes"/>
         <mapping class="com.su.domain.Teacher"/>
        <mapping class="com.su.domain.TwoStudent"/>
        <mapping class="com.su.domain.ThreeStudent"/>
        <mapping class="com.su.domain.StudentCard"/>



然后运读取配置文件的代码,即可在数据库自动建表:

public class HibernateTest {

    public static void main(String[] args) {

        // 默认读取hibernate.cfg.xml文件
        Configuration cfg = new Configuration().configure();

        // 生成并输出sql到文件(当前目录)和数据库 
        SchemaExport se = new SchemaExport(cfg);

        // 创建表结构,第一个true 表示在控制台打印sql语句,第二个true 表示导入sql语句到数据库
        se.create(true, true);      
    }

}



控制台显示成功信息如下:

INFO: HHH000046: Connection properties: {user=ssss, password=****}
七月 09, 2017 10:45:45 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
七月 09, 2017 10:45:45 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)

    drop table TABLE_TEA_STU cascade constraints

    drop table TABLE_TWOSTU cascade constraints

    drop table Teacher cascade constraints

    drop sequence hibernate_sequence

    create table TABLE_TEA_STU (
        STU_ID number(10,0) not null,
        TEA_ID number(10,0) not null,
        primary key (TEA_ID, STU_ID)
    )

    create table TABLE_TWOSTU (
        STU_ID number(10,0) not null,
        STU_AGE number(10,0),
        STU_GENDER varchar2(50 char),
        STU_NAME varchar2(200 char),
        primary key (STU_ID)
    )

    create table Teacher (
        TEA_ID number(10,0) not null,
        TEA_NAME varchar2(200 char) not null,
        TEA_PWD varchar2(200 char) not null,
        primary key (TEA_ID)
    )

    alter table TABLE_TEA_STU 
        add constraint FK_dnnscrifuvtxpg4t3c9oo1aaq 
        foreign key (TEA_ID) 
        references Teacher

    alter table TABLE_TEA_STU 
        add constraint FK_feeoifq2kgnybn2rvw4muoq8p 
        foreign key (STU_ID) 
        references TABLE_TWOSTU

    create sequence hibernate_sequence
七月 09, 2017 10:45:46 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH000030: Cleaning up connection pool [jdbc:oracle:thin:@localhost:1521:xe]
七月 09, 2017 10:45:46 下午 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete

完结!!!


Author:su1573

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ssy03092919

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值