zoukankan      html  css  js  c++  java
  • jpa单向多对一关联映射

    表结构

    student

    class

    Class

    package auth.model;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    @Entity
    @Table(name="class")
    public class Room {
    	private int id;
    	private String name;
    	@Id
    	@GeneratedValue(strategy= GenerationType.AUTO)
    	@Column(name="id")
    	public int getId() {
    		return id;
    	}
    	public void setId(int id) {
    		this.id = id;
    	}
    	@Column(name="name")
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    }

    Student

    package auth.model;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
    import javax.persistence.Table;
    
    @Entity
    @Table(name="student")
    public class Student {
    	private int id;
    	public int room_id;
    	private String name;
    	private Room room;
    	@GeneratedValue(strategy= GenerationType.AUTO)
    	@Id
    	@Column(name="id")
    	public int getId() {
    		return id;
    	}
    	public void setId(int id) {
    		this.id = id;
    	}
    	@Column(name="name")
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	@Column(name="room_id")
    	public int getRoom_id() {
    		return room_id;
    	}
    	public void setRoom_id(int room_id) {
    		this.room_id = room_id;
    	}
    	@ManyToOne
        @JoinColumn(name = "room_id",insertable=false, updatable=false)
    	public Room getRoom() {
    		return room;
    	}
    	public void setRoom(Room room) {
    		this.room = room;
    	}
    	
    	
    }

    测试

    package auth.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.alibaba.fastjson.JSONObject;
    
    import auth.dao.ClassDao;
    import auth.dao.StudentDao;
    import auth.model.Room;
    import auth.model.Student;
    
    @Service
    public class StudentController {
    	@Autowired
    	private ClassDao dao;
    	@Autowired
    	private StudentDao stuDao;
    	public void add(){
    		Room room=new Room();
    		room.setName("10班");
    		Student s=new Student();
    		dao.save(room);
    		s.setName("小李5");
    		s.setRoom(room);
    		stuDao.save(s);
    	}
    	public void query(){
    		Student s=stuDao.findOne(2);
    		System.out.println(JSONObject.toJSONString(s));
    		
    	}
    	public void query1(){
    		Room s=dao.findOne(2);
    		System.out.println(JSONObject.toJSONString(s));
    		
    	}
    
    }

    过程:

    insert 
        into
            class
            (name) 
        values
            (?)
            
            auth.model.Room{name=11班, id=20}
            
            insert 
        into
            student
            (name, room_id) 
        values
            (?, ?)
    2017-06-11 23:39:52,763 DEBUG [org.hibernate.id.IdentifierGeneratorHelper:94] - Natively generated identity: 24
    2017-06-11 23:39:52,766 DEBUG [org.hibernate.engine.transaction.spi.AbstractTransactionImpl:175] - committing
    2017-06-11 23:39:52,767 DEBUG [org.hibernate.event.internal.AbstractFlushingEventListener:149] - Processing flush-time cascades
    2017-06-11 23:39:52,767 DEBUG [org.hibernate.event.internal.AbstractFlushingEventListener:189] - Dirty checking collections
    2017-06-11 23:39:52,768 DEBUG [org.hibernate.event.internal.AbstractFlushingEventListener:123] - Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
    2017-06-11 23:39:52,768 DEBUG [org.hibernate.event.internal.AbstractFlushingEventListener:130] - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
    2017-06-11 23:39:52,772 DEBUG [org.hibernate.internal.util.EntityPrinter:114] - Listing entities:
    2017-06-11 23:39:52,772 DEBUG [org.hibernate.internal.util.EntityPrinter:121] - auth.model.Student{room_id=0, name=小李6, id=24, room=auth.model.Room#20}
    {"id":2,"name":"李明","room":{"id":1,"name":"12班"},"room_id":1}
    {"id":2,"name":"24班"}

    数据库结果:

  • 相关阅读:
    MFC中char*,string和CString之间的转换
    图像分割之(四)OpenCV的GrabCut函数使用和源码解读
    自然图像抠图/视频抠像技术发展情况梳理
    OpenCV混合高斯模型函数注释说明
    Effective STL 为包含指针的关联容器指定比较类型
    vs2010修改状态栏的CStatusBar指针的的SetPaneText()方法时死活不对问题
    windows c/c++ 代码运行时间,毫秒级
    【Math】根据置信度、样本数相关推导过程
    宝宝加减法出题小程序
    混淆矩阵
  • 原文地址:https://www.cnblogs.com/JAYIT/p/6986480.html
Copyright © 2011-2022 走看看