我需要将主表的主键转移到从属表中的外键字段
我有 2 个实体客户和地址,请查找下面的代码,为了简单起见,我省略了样板代码。
public class Customer implements java.io.Serializable {
private static final long serialVersionUID = 3116894694769321104L;
private short customerId;
private Address address;
private String firstName;
private String lastName;
private String email;
private boolean active;
private Date createDate;
private Date lastUpdate;
// Property accessors
@Id
@Column(name="customer_id", unique=true, nullable=false, insertable=true, updatable=true)
public short getCustomerId() {
return this.customerId;
}
public void setCustomerId(short customerId) {
this.customerId = customerId;
}
@ManyToOne(cascade={CascadeType.ALL},
fetch=FetchType.LAZY)
@JoinColumn(name="address_id", unique=false, nullable=false, insertable=true, updatable=true)
public Address getAddress() {
return this.address;
}
public void setAddress(Address address) {
this.address = address;
}
地址类是:
public class Address implements java.io.Serializable {
// Fields
private short addressId;
private short customerId;
private String address;
private String address2;
private String district;
private String postalCode;
private String phone;
private Date lastUpdate;
private Set<Customer> customers_1 = new HashSet<Customer>(0);
// Constructors
/** default constructor */
public Address() {
}
// Property accessors
@Id
@Column(name="address_id", unique=true, nullable=false, insertable=true, updatable=true)
public short getAddressId() {
return this.addressId;
}
public void setAddressId(short addressId) {
this.addressId = addressId;
}
/**
* ??????what goes here
*/
public short getCustomerId() {
return customerId;
}
/**
* @param customerId the customerId to set
*/
public void setCustomerId(short customerId) {
this.customerId = customerId;
}
我需要将客户 ID 作为外键保留在地址表中。
I have 2 entities Customer and address please find the code below, I have omitted boiler plate code for simplicity.
public class Customer implements java.io.Serializable {
private static final long serialVersionUID = 3116894694769321104L;
private short customerId;
private Address address;
private String firstName;
private String lastName;
private String email;
private boolean active;
private Date createDate;
private Date lastUpdate;
// Property accessors
@Id
@Column(name="customer_id", unique=true, nullable=false, insertable=true, updatable=true)
public short getCustomerId() {
return this.customerId;
}
public void setCustomerId(short customerId) {
this.customerId = customerId;
}
@ManyToOne(cascade={CascadeType.ALL},
fetch=FetchType.LAZY)
@JoinColumn(name="address_id", unique=false, nullable=false, insertable=true, updatable=true)
public Address getAddress() {
return this.address;
}
public void setAddress(Address address) {
this.address = address;
}
and Address class is :
public class Address implements java.io.Serializable {
// Fields
private short addressId;
private short customerId;
private String address;
private String address2;
private String district;
private String postalCode;
private String phone;
private Date lastUpdate;
private Set<Customer> customers_1 = new HashSet<Customer>(0);
// Constructors
/** default constructor */
public Address() {
}
// Property accessors
@Id
@Column(name="address_id", unique=true, nullable=false, insertable=true, updatable=true)
public short getAddressId() {
return this.addressId;
}
public void setAddressId(short addressId) {
this.addressId = addressId;
}
/**
* ??????what goes here
*/
public short getCustomerId() {
return customerId;
}
/**
* @param customerId the customerId to set
*/
public void setCustomerId(short customerId) {
this.customerId = customerId;
}
I need to persist the customer id as a foreign key in address table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需使用
@ManyToOne
与客户
的关系。因此,您将使用Customer
对象进行操作,而不是 Java 代码中的customerId
,但在数据库级别 Hibernate 将使用外键与 customer 表进行操作。Just use
@ManyToOne
relationship withCustomer
. So, instead ofcustomerId
in Java code you will be operates withCustomer
object, but at database level Hibernate will use foreign key to table with customer.