• hibernate外键一对一关联


    两个实体类

    package vo;

    public class message {
    private int mid;
    private String mname;
    private int age;
    private String mail;
    private String address;
    private String tel;

    private User user;
    public int getMid() {
    return mid;
    }
    public void setMid(int mid) {
    this.mid = mid;
    }
    public String getMname() {
    return mname;
    }
    public void setMname(String mname) {
    this.mname = mname;
    }
    @Override
    public String toString() {
    return "message [mid=" + mid + ", mname=" + mname + ", age=" + age
    + ", mail=" + mail + ", address=" + address + ", tel=" + tel
    + ", user=" + user + "]";
    }
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    public String getMail() {
    return mail;
    }
    public void setMail(String mail) {
    this.mail = mail;
    }
    public String getAddress() {
    return address;
    }
    public void setAddress(String address) {
    this.address = address;
    }
    public String getTel() {
    return tel;
    }
    public void setTel(String tel) {
    this.tel = tel;
    }

    public User getUser() {
    return user;
    }
    public void setUser(User user) {
    this.user = user;
    }


    }

    第二个user实体类

    package vo;

    public class User {
    private Integer id;
    private message m;
    public message getM() {
    return m;
    }
    public void setM(message m) {
    this.m = m;
    }
    public Integer getId() {
    return id;
    }
    public void setId(Integer id) {
    this.id = id;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public String getPwd() {
    return pwd;
    }
    public void setPwd(String pwd) {
    this.pwd = pwd;
    }
    private String name;
    private String pwd;
    }

    hibernate配置

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>

    <class name="vo.message" table="message">//name:实体的路径,table:对应表的名字
    <id name="mid" column="mid">//id为主键  name为实体中的属性 column为表中的列名
    <generator class="native"></generator>
    </id>
    <property name="mname" column="mname"></property>//非主键
    <property name="age" column="age"></property>
    <property name="mail" column="mail"></property>
    <property name="address" column="address"></property>
    <property name="tel" column="tel"></property>
    <one-to-one name="user" constrained="true" cascade="all"></one-to-one>//表示一对一对应 cascade为级联关系

    </class>

    </hibernate-mapping>

    以下为user表对应的数据配置:

    将项目部署在tomcat服务器上就可以自动生成表。


  • 相关阅读:
    CentOS iftop和nethogs安装配置
    sfcapd服务新增nfdump安装
    CentOS 7.0手工解压安装Java JDK1.8环境
    华为USG防火墙CPU超高因NGE引擎占用率超过60%解决方法
    centos7快速安装smokeping
    华为USG防火墙常用SNMP OID
    Oracle物理结构和逻辑结构是怎样的?
    解决Windows Server 2008磁盘脱机状态
    Zabbix监控华为交换机CPU、温度并显示到拓扑图
    Object.defineProperty的理解
  • 原文地址:https://www.cnblogs.com/longlinxie/p/5628185.html
Copyright © 2020-2023  润新知