1 ANNOTATIONS
1.1 @PrimaryKeyJoinColumn and mappedBy
1.1.1 Note
@PrimaryKeyJoinColumn is an annotation, while mappedBy is only an attribute of the @OneToOne, @OneToMany, @ManyToOne and @ManyToMany
1.1.2 Reference URL
The following material reference from section “2.2.5.1 One-to-one” at
http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/entity.html
You can associate entities through a one-to-one relationship using @OneToOne. There are three cases for one-to-one associations: either the associated entities share the same primary keys values, a foreign key is held by one of the entities (note that this FK column in the database should be constrained unique to simulate one-to-one multiplicity), or a association table is used to store the link between the 2 entities (a unique constraint has to be defined on each fk to ensure the one to one multiplicity).
1.1.3 @PrimaryKeyJoinColumn
First, we map a real one-to-one association using shared primary keys:
@Entity
public class Body {
@Id
public Long getId() { return id; }
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
public Heart getHeart() {
return heart;
}
...
}
@Entity
public class Heart {
@Id
public Long getId() { ...}
}
The @PrimaryKeyJoinColumn annotation does say that the primary key of the entity is used as the foreign key value to the associated entity.
Comments: the primary key is in Heart, and the associated entity is Body who has a foreign key referencing the primary key of Heart.
1.1.4 mappedBy attribute
In the following example, the associated entities are linked through an explicit foreign key column:
@Entity
public class Customer implements Serializable {
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="passport_fk")
public Passport getPassport() {
...
}
@Entity
public class Passport implements Serializable {
@OneToOne(mappedBy = "passport")
public Customer getOwner() {
...
}
A Customer is linked to a Passport, with a foreign key column named passport_fk in the Customer table. The join column is declared with the @JoinColumn annotation which looks like the @Column annotation. It has one more parameters named referencedColumnName. This parameter declares the column in the targeted entity that will be used to the join. Note that when using referencedColumnName to a non primary key column, the associated class has to be Serializable. Also note that the referencedColumnName to a non primary key column has to be mapped to a property having a single column (other cases might not work).
The association may be bidirectional. In a bidirectional relationship, one of the sides (and only one) has to be the owner: the owner is responsible for the association column(s) update. To declare a side as not responsible for the relationship, the attribute mappedBy is used. mappedBy refers to the property name of the association on the owner side. In our case, this is passport. As you can see, you don't have to (must not) declare the join column since it has already been declared on the owner’s side.
Comments: the owner side is Customer (@JoinColumn is defined in Customer) who will be responsible for the association column update, and mappedBy will be used in non-owner side, the value of mappedBy is the property name of the association on the owner side. In the above example, the name of the association on the owner side is “passport” (Customer has a getter method: getPassport).
If no @JoinColumn is declared on the owner side, the defaults apply. A join column(s) will be created in the owner table and its name will be the concatenation of the name of the relationship in the owner side, _ (underscore), and the name of the primary key column(s) in the owned side. In this example passport_id because the property name is passport and the column id of Passport is id.
The third possibility (using an association table) is quite exotic.
@Entity
public class Customer implements Serializable {
@OneToOne(cascade = CascadeType.ALL)
@JoinTable(name = "CustomerPassports",
joinColumns = @JoinColumn(name="customer_fk"),
inverseJoinColumns = @JoinColumn(name="passport_fk")
)
public Passport getPassport() {
...
}
@Entity
public class Passport implements Serializable {
@OneToOne(mappedBy = "passport")
public Customer getOwner() {
...
}
A Customer is linked to a Passport through a association table named CustomerPassports ; this association table has a foreign key column named passport_fk pointing to the Passport table (materialized by the inverseJoinColumn, and a foreign key column named customer_fk pointing to the Customer table materialized by the joinColumns attribute.
You must declare the join table name and the join columns explicitly in such a mapping.
分享到:
相关推荐
这里,`mappedBy`属性指定了关联的另一方,而`@PrimaryKeyJoinColumn`表明两个实体共享相同的主键。 ### 3. 插入和更新策略 在一对一关系中,插入和更新策略需要考虑。例如,你可以选择先保存父实体,然后保存子...
在上面的代码中,`Person`类中的`profile`属性使用了`mappedBy`属性,表明`Profile`是被关联方,`Profile`类中的`person`属性则使用了`@OneToOne`和`@PrimaryKeyJoinColumn`注解,表示`Person`是关联方,且主键和...
@OneToOne(mappedBy = "user") private Profile profile; } @Entity public class Profile { @Id private Long id; @OneToOne @PrimaryKeyJoinColumn private User user; } ``` 2. **一对多关联关系*...
@OneToOne(mappedBy = "student") private IdCard idCard; } @Entity public class IdCard { @OneToOne @PrimaryKeyJoinColumn private Student student; } ``` 2. **一对多关联**: 一对多关联常出现...
这里的关键在于`@OneToOne`和`@PrimaryKeyJoinColumn`注解的使用,以及`mappedBy`属性的设置。`@OneToOne`注解用于声明一对一的关联关系;`mappedBy`属性指明了关联关系的主导方,本例中Person类是被动方,IdCard类...
`@JoinColumn(name = "user_id")`指定了外键所在的列名,`@OneToOne`和`mappedBy`注解用于定义一对一关系。 总结来说,Hibernate提供这两种方式来处理一对一关联,共享主键方式简单但可能违反数据库设计最佳实践,...
你可以指定`mappedBy`属性来指定子类中的反向关联字段,这通常是多端的`@ManyToOne`注解的引用字段。 2. **@JoinColumn** 或 **@JoinTable**:对于一端,我们通常需要使用`@JoinColumn`注解来指定外键所在的列,...
在Profile类中,我们同样需要声明对User的引用,但是不需要`mappedBy`属性,因为它是主键关联的一方: ```java @OneToOne @PrimaryKeyJoinColumn private User user; ``` 3. **@PrimaryKeyJoinColumn注解**...
@OneToOne(mappedBy = "user", cascade = CascadeType.ALL) @PrimaryKeyJoinColumn private Profile profile; } @Entity public class Profile { @Id private Long id; // 与User的id共享 @OneToOne @...
`mappedBy`属性用于指定关系的维护方,没有`mappedBy`的一方负责在保存时同步关联关系。 ### 5. "bionetoonetable"实践 资源"bionetoonetable"可能是某个实际的项目或教程,它可能包含了一个关于生物信息学领域的...
在上述代码中,`mappedBy`属性用于指示另一端的关联字段,而在`IdCard`类中,`@PrimaryKeyJoinColumn`注解表明两个实体的主键是相同的,这对应于数据库中的一对一外键约束。 接下来,我们需要配置Hibernate的映射...
在这里,`mappedBy`属性用于指定对方实体中的字段,`@PrimaryKeyJoinColumn`表明两个实体的主键是相同的。 2. **非共享主键关联**: 如果两个实体的主键不相同,但仍然存在一对一关系,可以使用外键来建立关联。...
双向一对一关系需要在两个实体类中都使用`@OneToOne`,一方使用`mappedBy`属性引用另一方的属性。例如: ```java // 在User类中 @OneToOne(mappedBy="user") private Profile profile; // 在Profile类中 @...
@OneToOne(mappedBy = "detail") private User user; @MapsId @PrimaryKeyJoinColumn private Long id; // ... } ``` 在这里,`@MapsId`表示UserDetail的id字段映射到User的id字段,而`@...
1. 在“一方”类上使用`@OneToMany`注解,指定`mappedBy`属性为“多方”类中的反向引用字段名。 2. 在“多方”类中添加一个`@ManyToOne`注解的字段,对应一方的主键。 ```java // "一方"类 public class Student { ...
@OneToOne(mappedBy = "user") private Account account; } @Entity public class Account { @Id private Long id; @OneToOne @JoinColumn(name = "user_id") private User user; } ``` **2. 一对多关联...
这种关联可以是共享主键(mappedBy)或者拥有独立主键。共享主键意味着两个实体共用同一个主键,而独立主键则意味着每个实体都有自己的主键。 二、配置与实现 1. 共享主键(Primary Key Join Column) 在这种情况下...
@OneToOne(mappedBy = "person") private IdentityCard identityCard; } @Entity public class IdentityCard { @Id private Long id; //与Person的id共享 @OneToOne @PrimaryKeyJoinColumn private Person ...