zoukankan      html  css  js  c++  java
  • hibernate---一对一单向主键关联(不重要)

    比如, husband的id参考wife的id

    husband.java:

    package com.bjsxt.hibernate;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.OneToOne;
    import javax.persistence.PrimaryKeyJoinColumn;
    
    @Entity
    public class Husband {
    	private int id;
    	private String name;
    	private Wife wife;
    	@Id
    	@GeneratedValue
    	public int getId() {
    		return id;
    	}
    	
    	public String getName() {
    		return name;
    	}
    	@OneToOne
    	@PrimaryKeyJoinColumn
    	public Wife getWife() {
    		return wife;
    	}
    	public void setId(int id) {
    		this.id = id;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public void setWife(Wife wife) {
    		this.wife = wife;
    	}
    	
    }
    

    wife.java:

    package com.bjsxt.hibernate;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    
    @Entity
    public class Wife {
    	private int id;
    	private String name;
    	
    	@Id
    	@GeneratedValue
    	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;
    	}
    	
    }
    

    有bug, 无法实现,

    xml方式:

    主导方 StuIdCard.java里设置one-to-one就可以

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping>
    	<class name="com.bjsxt.hibernate.StuIdCard">
    		<id name="id">
    			<generator class="foreign">
    				<param name="property">student</param>
    			</generator>
    		</id>
    		
    		<property name="num"/>
    		<one-to-one name="student" constrained="true"></one-to-one>
        </class>
    	
    </hibernate-mapping>
    

      

      

  • 相关阅读:
    liunx挂载Ios镜像文件
    liunx下删除多个目录下的相同文件
    tomcat启动报错:Unable to complete the scan for annotations for web application [] due to a StackOverflow
    lr中controller 中scenario-> rendezvous显示灰色不可用
    liunx 下在指定文件夹下搜索字符
    python运行时提示:dot not in the path
    1.4 python 类型转换函数
    js字符串拼接
    自己写的时间轴空控件
    ios禁止页面下拉
  • 原文地址:https://www.cnblogs.com/wujixing/p/5420529.html
Copyright © 2011-2022 走看看