lizable {
...
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="ADDR_TID")
private Address address;
...
}
注释 F. 多对多关系
// User (owner/child) entity
@Entity
@Table(name="T_USER")
public class User implements Serializable {
@Column(name="USER_TID")
@Id private Long userId;
@ManyToMany
@JoinTable(
name="T_USER_GROUP",
joinColumns=@JoinColumn(name="USER_TID"),
inverseJoinColumns=@JoinColumn(name="GROUP_TID"))
private Set<Group> groups = new HashSet<Group>();
...
}
// Group (non-owner/parent) entity
@Entity
@Table(name="T_GROUP")
public class Group implements Serializable {
@Column(name="GROUP_TID")
@Id private Long groupId;
@ManyToMany(mapppedBy="groups")
private Set<User> users = new HashSet<User>();
...
}
将遗留Hibernate应用程序迁移到OpenJPA和EJB 3.0(二)(17)
时间:2011-09-18 Donald Vines
注释 G. 延迟初始化
// Address (parent) entity
@Entity
@Table(name="T_ADDRESS")
public class Address implements Serializable {
...
@OneToMany(mappedBy="address", fetch=FetchType.EAGER)
private Set<Phone> phones = new HashSet<Phone>();;
...
@Column(name="ADDR_TID")
@Id private Long addressId;
}
// Phone (child) entity
@Entity
@Table(name="T_PHONE")
public class Phone implements Serializable {
...
@ManyToOne
@JoinColumn(name="ADDR_TID")
private Address address;
...
}
注释 H. 对象标识
// Address entity
@Entity
@Table(name="T_ADDRESS")
public class Address implements Serializable {
...
@SequenceGenerator(name="AddressSeq", sequenceName="T_ADDRESS_SEQ")
@GeneratedValue(
strategy=GenerationType.SEQUENCE,
generator="AddressSeq")
@Column(name="ADDR_TID")
@Id private Long addressId;
...
}
注释 I. 乐观锁定
@Entity
@Table(name="T_ADDRESS")
public class Address {
...
@Column(name="VERSION")
@Version private long version;
...
}
|