0

我有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; 
    } 

和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在地址表的外鍵。

回答

1

只使用@ManyToOneCustomer的關係。因此,在Java代碼中,您將使用customerId而不是Customer對象,但在數據庫級別,Hibernate將使用外鍵與客戶進行對照。

相關問題