`

一对一双向外键联合主键(annotation方式)

 
阅读更多
主键类
package org.hibernate.tutorial.domain;


public class WifePK implements Serializable {
	private int id;
	private String name;
	
	public int getId() {
		return id;
	}
	public String getName() {
		return name;
	}
	public void setId(int id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}



设置对象主键

@Entity
@IdClass(WifePK.class)
public class Wife {
	private int id;
	private String name;
	private String age;
	
	
	@Id
	public int getId() {
		return id;
	}
	public String getName() {
		return name;
	}
	public void setId(int id) {
		this.id = id;
	}
	@Id
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	
}



设置关联
@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
	@JoinColumns(
			{
				@JoinColumn(name="wifeId",referencedColumnName="id"),
				@JoinColumn(name="wifeName",referencedColumnName="name")
			}
	)
	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;
	}
	
}




hibernate.hb.xml配置和测试
引用

<mapping class="org.hibernate.tutorial.domain.Husband"/>
<mapping class="org.hibernate.tutorial.domain.Wife"/>

public class OneToOneORMappingTest {
	public static void main(String[] args) {
		new SchemaExport(new AnnotationConfiguration().configure("/hibernate.cfg.xml")).create(true, false);
	}
}


结果
alter table Husband 
        drop 
        foreign key FK_sdshifqwgidsm93jdh5kfl0e4

    drop table if exists Husband

    drop table if exists Wife

    create table Husband (
        id integer not null auto_increment,
        name varchar(255),
        wifeId integer,
        wifeName varchar(255),
        primary key (id)
    )

    create table Wife (
        id integer not null,
        name varchar(255) not null,
        age varchar(255),
        primary key (id, name)
    )

    alter table Husband 
        add index FK_sdshifqwgidsm93jdh5kfl0e4 (wifeId, wifeName), 
        add constraint FK_sdshifqwgidsm93jdh5kfl0e4 
        foreign key (wifeId, wifeName) 
        references Wife (id, name)
分享到:
评论

相关推荐

    Hibernate一对一单向外键关联 (联合主键annotation)

    通过这种方式,你可以实现Hibernate中一对一单向外键关联及联合主键的配置。这使得两个实体间的关系更加明确,同时也方便了数据的管理和操作。在实际项目中,这样的设计可以提高数据的完整性和一致性。

    Hibernate_学习笔记.

    - **联合主键关联(Annotation方式)**:两个对象通过联合主键进行关联。 - **Component(组件)关联映射** - **Component关联映射**:将一个对象的某些属性作为一个单独的对象进行处理。 - **User实体类**:表示...

    Hibernate学习笔记

    - **联合主键**:当一个实体的主键由多个字段组成时,可以通过XML或Annotation方式指定联合主键。 #### Hibernate核心开发接口 - **Configuration/AnnotationConfiguration**:负责读取配置文件,初始化...

    Hibernate3的帮助文档

    8.5.2. 一对一(one to one) 8.5.3. 多对多(many to many) 9. 组件(Component)映射 9.1. 依赖对象(Dependent objects) 9.2. 在集合中出现的依赖对象 9.3. 组件作为Map的索引(Components as Map indices ...

    hibernate 框架详解

    一对一(one to one) 8.5. 使用连接表的双向关联(Bidirectional associations with join tables) 8.5.1. 一对多(one to many) /多对一( many to one) 8.5.2. 一对一(one to one) 8.5.3. 多对多...

    hibernate3.04中文文档.chm

    8.4.2. 一对一(one to one) 8.5. 使用连接表的双向关联(Bidirectional associations with join tables) 8.5.1. 一对多(one to many) /多对一( many to one) 8.5.2. 一对一(one to one) 8.5.3. 多对多...

    Hibernate教程

    8.3.3. 一对一(one to one) 8.3.4. 多对多(many to many) 8.4. 双向关联(Bidirectional associations) 8.4.1. 一对多(one to many) / 多对一(many to one) 8.4.2. 一对一(one to one) 8.5. 使用连接表...

    hibernate 体系结构与配置 参考文档(html)

    一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many ...

    Hibernate参考文档

    7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多...

    Hibernate 中文 html 帮助文档

    7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多...

    最全Hibernate 参考文档

    7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多...

    Hibernate3+中文参考文档

    7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. 双向关联(Bidirectional associations) 7.4.1. 一对多(one to many) / 多对一(many to one) 7.4.2. 一对一(one to one) 7.5. 使用连接表的...

Global site tag (gtag.js) - Google Analytics