主键类
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中一对一单向外键关联及联合主键的配置。这使得两个实体间的关系更加明确,同时也方便了数据的管理和操作。在实际项目中,这样的设计可以提高数据的完整性和一致性。
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 ...
一对一(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. 多对多...
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. 多对多...
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. 使用连接表...
一对一(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 ...
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. 多对多...
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. 多对多...
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. 多对多...
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. 使用连接表的...