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

    数据库结果:

  • 相关阅读:
    Hook钩子程序
    KMeans笔记 K值以及初始类簇中心点的选取
    自己用C#写的一个俄罗斯方块的小程序(附源代码)。
    那些帮助你成为优秀前端工程师的讲座——《JavaScript篇》
    Mac技巧合集第二期
    WCF增加UDP绑定(应用篇)
    第一个MVC4 Web应用程序
    jQuery的页面加载事件
    通过网页进行 iOS 应用内部分发
    sql count效率
  • 原文地址:https://www.cnblogs.com/JAYIT/p/6986480.html
Copyright © 2020-2023  润新知