我需要将主表的主键转移到从属表中的外键字段

发布于 2024-12-05 22:22:49 字数 2175 浏览 1 评论 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;
    }

地址类是:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兰花执着 2024-12-12 22:22:49

只需使用 @ManyToOne客户的关系。因此,您将使用 Customer 对象进行操作,而不是 Java 代码中的 customerId ,但在数据库级别 Hibernate 将使用外键与 customer 表进行操作。

Just use @ManyToOne relationship with Customer. So, instead of customerId in Java code you will be operates with Customer object, but at database level Hibernate will use foreign key to table with customer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文