欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

hibernate一对一关系(组件)映射

程序员文章站 2022-05-13 21:21:47
...
1.一对一单向外键关联:
package yingjun.model;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Husband {
	private int id;
	private String name;
	private Date birthday;
    private Wife wife;
    
    @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;
	}
	@Temporal(TemporalType.DATE) //改变为yyyy-mm-dd(默认为yyyy-mm-dd hh:mm:ss)
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	@OneToOne         //设定对应关系
	@JoinColumn(name="wifeid")  //外键
	public Wife getWife() {
		return wife;
	}
	public void setWife(Wife wife) {
		this.wife = wife;
	}
	
}
 
package yingjun.model;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Wife {
	private int id;
	private String name;
	private Date birthday;
	@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;
	}
	@Temporal(TemporalType.DATE) 
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	
}
create table Husband (
        id integer not null auto_increment,
        birthday date,
        name varchar(255),
        wifeid integer,
        primary key (id)
    )

    create table Wife (
        id integer not null auto_increment,
        birthday date,
        name varchar(255),
        primary key (id)
    )

    alter table Husband 
        add index FKAEEA401BF57AD616 (wifeid), 
        add constraint FKAEEA401BF57AD616 
        foreign key (wifeid) 
        references Wife (id)
 2.一对一双向外键关联:
package yingjun.model;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Husband {
	private int id;
	private String name;
	private Date birthday;
    private Wife wife;
    
    @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;
	}
	@Temporal(TemporalType.DATE) //改变为yyyy-mm-dd(默认为yyyy-mm-dd hh:mm:ss)
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	@OneToOne         //设定对应关系
	@JoinColumn(name="wifeid")  //外键
	public Wife getWife() {
		return wife;
	}
	public void setWife(Wife wife) {
		this.wife = wife;
	}
	
}
 
package yingjun.model;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Wife {
	private int id;
	private String name;
	private Date birthday;
	private Husband hudband;
	
	@OneToOne(mappedBy="wife")//告诉已经建立关联,不用建立冗余外键
	public Husband getHudband() {
		return hudband;
	}
	public void setHudband(Husband hudband) {
		this.hudband = hudband;
	}
	@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;
	}
	@Temporal(TemporalType.DATE) 
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	
}

 生成的建表语句和一对一单项外键关联相同即在数据库的表现一样,区别在java程序里面的husband和wife可以互相联系
  3.联合主键关联:
package yingjun.model;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity

@IdClass(HusbandPK.class) //指定联合主键
public class Husband {
	private int id;
	private String name;
	private Date birthday;
   
    
    @Id
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	@Id
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Temporal(TemporalType.DATE) //改变为yyyy-mm-dd(默认为yyyy-mm-dd hh:mm:ss)
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	
	
}
 
package yingjun.model;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Wife {
	private int id;
	private String name;
	private Date birthday;
	private Husband hudband;
	
	@OneToOne
	@JoinColumns({
		                 @JoinColumn(name="husbandid",referencedColumnName="id"),
		                 @JoinColumn(name="husbandname",referencedColumnName="name") 
		          })
	public Husband getHudband() {
		return hudband;
	}
	public void setHudband(Husband hudband) {
		this.hudband = hudband;
	}
	@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;
	}
	@Temporal(TemporalType.DATE) 
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	
}
 
package yingjun.model;

public class HusbandPK {
	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;
	}
}
 
 create table Husband (
        id integer not null,
        name varchar(255) not null,
        birthday date,
        primary key (id, name)
    )

    create table Wife (
        id integer not null auto_increment,
        birthday date,
        name varchar(255),
        husbandid integer,
        husbandname varchar(255),
        primary key (id)
    )

    alter table Wife 
        add index FK292331E40F5A5E (husbandid, husbandname), 
        add constraint FK292331E40F5A5E 
        foreign key (husbandid, husbandname) 
        references Husband (id, name)
 3.组件映射
package yingjun.model;

import java.util.Date;

import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Husband {
	private int id;
	private String name;
	private Date birthday;
    private Wife wife;
    @Id
	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;
	}
	@Temporal(TemporalType.DATE) //改变为yyyy-mm-dd(默认为yyyy-mm-dd hh:mm:ss)
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	@Embedded  //将wife的内容嵌入进来作为自己的一部分
	public Wife getWife() {
		return wife;
	}
	public void setWife(Wife wife) {
		this.wife = wife;
	}
	
	
}
 
package yingjun.model;

import java.util.Date;
public class Wife {
	
	private String wifename;
	private Date wifebirthday;


	
	public void setWifebirthday(Date wifebirthday) {
		this.wifebirthday = wifebirthday;
	}
	public Date getWifebirthday() {
		return wifebirthday;
	}
	public void setWifename(String wifename) {
		this.wifename = wifename;
	}
	public String getWifename() {
		return wifename;
	}
	
}
 
create table Husband (
        id integer not null,
        birthday date,
        name varchar(255),
        wifebirthday datetime,
        wifename varchar(255),
        primary key (id)
    )