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班"}

    数据库结果:

  • 相关阅读:
    dotnet core 获取 MacAddress 地址方法
    dotnet core 获取 MacAddress 地址方法
    dotnet core 发布只带必要的依赖文件
    dotnet core 发布只带必要的依赖文件
    Developing Universal Windows Apps 开发UWA应用 问答
    Developing Universal Windows Apps 开发UWA应用 问答
    cmd 如何跨驱动器移动文件夹
    cmd 如何跨驱动器移动文件夹
    C++ 驱动开发 error LNK2019
    C++ 驱动开发 error LNK2019
  • 原文地址:https://www.cnblogs.com/JAYIT/p/6986480.html
Copyright © 2011-2022 走看看