create table USERS( PK_USER_ID INT IDENTITY NOT NULL, USER_ID VARCHAR(50) NOT NULL, FK_GROUP_ID INT NOT NULL ) create table USER_GROUP( PK_GROUP_ID INT IDENTITY NOT NULL, GROUP_NAME VARCHAR(50) NOT NULL, LAST_UPDATED_BY VARCHAR(50) NOT NULL, LAST_UPDATED_DATE DATETIME NOT NULL )
@Entity @Table(name = "USERS") public class User { private int pkUserId; private String userId; private Group group; public User(){} @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "PK_USER_ID", unique = true, nullable = false) public int getPkUserId() { return pkUserId; } public void setPkUserId(int pkUserId) { this.pkUserId = pkUserId; } @Column(name = "USER_ID", unique = true, nullable = false) public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } @OneToOne(fetch = FetchType.EAGER) @JoinColumn(name = "FK_GROUP_ID") public Group getGroup() { return group; } }
@Entity @Table(name = "USER_GROUP") public class Group { private int groupId; private String groupName; private String lastUpdatedBy; private Date lastUpdatedDate; public Group() { } @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "PK_GROUP_ID", unique = true, nullable = false) public int getGroupId() { return groupId; } public void setGroupId(int groupId) { this.groupId = groupId; } @Column(name = "GROUP_NAME", nullable = true) public String getGroupName() { return groupName; } public void setGroupName(String groupName) { this.groupName = groupName; } @Column(name = "LAST_UPDATED_BY", nullable = false) public String getLastUpdatedBy() { return lastUpdatedBy; } public void setLastUpdatedBy(String lastUpdatedBy) { this.lastUpdatedBy = lastUpdatedBy; } @Column(name = "LAST_UPDATED_DATE", nullable = false) public Date getLastUpdatedDate() { return lastUpdatedDate; } public void setLastUpdatedDate(Date lastUpdatedDate) { this.lastUpdatedDate = lastUpdatedDate; } public String toString() { return this.groupId + "\t" + this.groupName + "\t\t" + this.lastUpdatedBy + "\t\t" + this.lastUpdatedDate; } }
2. 外键双向(bidirectional with foreign key )
为Group 增加 User 变量
public class Group { ........... private User user; ................ @OneToOne(mappedBy = "group") public User getUser() { return user; } public void setUser(User user) { this.user = user; } .................... }
group 是User类中的成员变量名。
mappedBy 作用如下:
引用
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.
参考: http://docs.jboss.org/ejb3/app-server/HibernateAnnotations/reference/en/html_single/index.html#d0e1517
3. 主键双向(bidirectional with primary key)
create table BODY( PK_BODY_ID INT IDENTITY NOT NULL, LAST_UPDATED_BY VARCHAR(50) NOT NULL, LAST_UPDATED_DATE DATETIME NOT NULL ) create table HEART( PK_HEART_ID INT NOT NULL, HEART_TYPE VARCHAR(50) NOT NULL, LAST_UPDATED_BY VARCHAR(50) NOT NULL, LAST_UPDATED_DATE DATETIME NOT NULL )
@Entity @Table(name = "BODY") public class Body { private Long id; private String lastUpdatedBy; private Date lastUpdatedDate; private Heart heart; public Body(){ } @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "PK_BODY_ID", unique = true, nullable = false) public Long getId() { return id; } public void setId(Long id) { this.id = id; } @OneToOne(cascade = CascadeType.ALL) @PrimaryKeyJoinColumn public Heart getHeart() { return heart; } public void setHeart(Heart heart) { this.heart = heart; } @Column(name = "LAST_UPDATED_BY", nullable = false) public String getLastUpdatedBy() { return lastUpdatedBy; } public void setLastUpdatedBy(String lastUpdatedBy) { this.lastUpdatedBy = lastUpdatedBy; } @Column(name = "LAST_UPDATED_DATE", nullable = false) public Date getLastUpdatedDate() { return lastUpdatedDate; } public void setLastUpdatedDate(Date lastUpdatedDate) { this.lastUpdatedDate = lastUpdatedDate; } public String toString() { return this.getId() + "\t" + this.getLastUpdatedBy() + "\t" + this.getLastUpdatedDate() + "\t" + this.getHeart().getId(); } }
@Entity @Table(name = "HEART") public class Heart { private Long id; private String type; private String lastUpdatedBy; private Date lastUpdatedDate; private Body body; public Heart() { } @Id @GenericGenerator( name = "generator", strategy = "foreign", parameters = @Parameter( name = "property", value = "body")) @GeneratedValue(generator="generator") @Column(name = "PK_HEART_ID", unique = true, nullable = false) public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(name = "HEART_TYPE", nullable = false) public String getType() { return type; } public void setType(String type) { this.type = type; } @Column(name = "LAST_UPDATED_BY", nullable = false) public String getLastUpdatedBy() { return lastUpdatedBy; } public void setLastUpdatedBy(String lastUpdatedBy) { this.lastUpdatedBy = lastUpdatedBy; } @Column(name = "LAST_UPDATED_DATE", nullable = false) public Date getLastUpdatedDate() { return lastUpdatedDate; } public void setLastUpdatedDate(Date lastUpdatedDate) { this.lastUpdatedDate = lastUpdatedDate; } @OneToOne(cascade = CascadeType.ALL, mappedBy="heart") public Body getBody() { return body; } public void setBody(Body body) { this.body = body; } @Override public String toString() { return this.getId() + "\t" + this.getType(); } }
save时, 须调用
b.setHeart(h); h.setBody(b);
否则会报错
Exception in thread "main" [size=xx-small]org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property: body[/size]
4. 主键单向没成功