• hibernate one2many


    package com.bjsxt.hibernate;

    import java.util.HashSet;
    import java.util.Set;

    import javax.persistence.Embedded;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.JoinColumns;
    import javax.persistence.OneToMany;
    import javax.persistence.OneToOne;
    import javax.persistence.PrimaryKeyJoinColumn;
    import javax.persistence.Table;

    @Entity
    @Table(name="t_group")
    public class Group {
    @Id
    @GeneratedValue                                 //这个  @Id 和  one2many  要么都加在getter上  要么 都加在 变量上
    private int id;

    private String name;


    @OneToMany
    @JoinColumn(name="groupId")                                // 一直以为 哪边加joinColumn 哪边加字段     经过这次试验  发现   字段只会加在多对一  一对多   多的一方;
    private Set<User> users = new HashSet<User>();

    public int getId() {
    return id;
    }

    public Set<User> getUsers() {
    return users;
    }
    public void setUsers(Set<User> users) {
    this.users = users;
    }
    public void setId(int id) {
    this.id = id;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    ·············································································

    package com.bjsxt.hibernate;

    import javax.persistence.Embedded;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.JoinColumns;
    import javax.persistence.ManyToOne;
    import javax.persistence.OneToOne;
    import javax.persistence.PrimaryKeyJoinColumn;
    import javax.persistence.Table;

    @Entity
    @Table(name="t_user")
    public class User {
    @Id
    @GeneratedValue
    private int id;
    private String name;

    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;
    }



    }



    }

  • 相关阅读:
    优秀大数据GitHub项目一览
    自定义组件-BreadcrumbTreeView 的使用
    IOS中的属性列表----Property List
    即时通讯之smack客户端配置
    Android studio 使用问题汇总
    触摸事件UITouch的应用
    Android界面设计之对话框——定制Toast、AlertDialog
    android_orm框架之greenDAO(一)
    火速提升Android仿真器的运行速度 ——仿真器Genymotion
    Android 中的缓存机制与实现
  • 原文地址:https://www.cnblogs.com/lize1215/p/7641734.html
Copyright © 2020-2023  润新知