码迷,mamicode.com
首页 > 其他好文 > 详细

10、基于annotation的配置 many2one

时间:2015-10-10 22:50:36      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:

1、基于annotation的配置 many2one

student.java

package org.zttc.itat.model;

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;

@Entity
@Table(name="t_student")
public class Student {
    private int id;
    private String name;
    private String no;
    private Classroom classroom;
    
    @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;
    }
    public String getNo() {
        return no;
    }
    public void setNo(String no) {
        this.no = no;
    }
    
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="cid")
    public Classroom getClassroom() {
        return classroom;
    }
    public void setClassroom(Classroom classroom) {
        this.classroom = classroom;
    }
}

classroom.java

package org.zttc.itat.model;

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;

@Entity
@Table(name="t_student")
public class Student {
    private int id;
    private String name;
    private String no;
    private Classroom classroom;
    
    @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;
    }
    public String getNo() {
        return no;
    }
    public void setNo(String no) {
        this.no = no;
    }
    
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="cid")
    public Classroom getClassroom() {
        return classroom;
    }
    public void setClassroom(Classroom classroom) {
        this.classroom = classroom;
    }
}

 在Hibernate.cfg.xml中需要以下的修改

<mapping class="com.baowei.entity.User" />

 

10、基于annotation的配置 many2one

标签:

原文地址:http://www.cnblogs.com/zhangbaowei/p/4868408.html

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