zoukankan      html  css  js  c++  java
  • 10、基于annotation的配置 many2one

    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" />
  • 相关阅读:
    洛谷 [P1024]一元三次方程求解
    洛谷 [P1426] 通往奥格瑞玛的道路
    洛谷 [p1439] 最长公共子序列 (NlogN)
    洛谷 [P1182] 数列分段
    洛谷 [P1314] 聪明的质检员(NOIP2011 D2T2)
    洛谷 [P1280] 尼克的任务
    洛谷 [P1801] 黑匣子
    洛谷 [p1196] 银河英雄传说
    洛谷 [P2024] 食物链
    洛谷 [P1198] 最大数
  • 原文地址:https://www.cnblogs.com/zhangbaowei/p/4868408.html
Copyright © 2011-2022 走看看