簡體   English   中英

復合主鍵休眠和Java實現

[英]Composite Primary Key Hibernate and Java Implementation

我定義了一個非常簡單的數據庫,如下所示:

MySql簡單數據庫

我正在使用休眠插件生成不同的類,包括客戶和訂單之間的一對多關系。 我將這種關系作為一種識別關系,因為如果不鏈接到客戶,則訂單將不存在。

我有以下三個課程:

=>類客戶

private int idCustomer;
private String name;
private Set<Order> orders = new HashSet<Order>(0);

public Customer() {
}

public Customer(int idCustomer) {
    this.idCustomer = idCustomer;
}

public Customer(int idCustomer, String name, Set<Order> orders) {
    this.idCustomer = idCustomer;
    this.name = name;
    this.orders = orders;
}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idCustomer", unique = true, nullable = false)
public int getIdCustomer() {
    return this.idCustomer;
}

public void setIdCustomer(int idCustomer) {
    this.idCustomer = idCustomer;
}

@Column(name = "name", length = 45)
public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "customer")
public Set<Order> getOrders() {
    return this.orders;
}

public void setOrders(Set<Order> orders) {
    this.orders = orders;
}

=>班級順序:

private OrderId id;
private Customer customer;
private String quantity;
private Float price;

public Order() {
}

public Order(OrderId id, Customer customer) {
    this.id = id;
    this.customer = customer;
}

public Order(OrderId id, Customer customer, String quantity, Float price) {
    this.id = id;
    this.customer = customer;
    this.quantity = quantity;
    this.price = price;
}

@EmbeddedId
@AttributeOverrides({
        @AttributeOverride(name = "idOrder", column = @Column(name = "idOrder", nullable = false)),
        @AttributeOverride(name = "customerIdCustomer", column = @Column(name = "Customer_idCustomer", nullable = false)) })
public OrderId getId() {
    return this.id;
}

public void setId(OrderId id) {
    this.id = id;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "Customer_idCustomer", nullable = false, insertable = false, updatable = false)
public Customer getCustomer() {
    return this.customer;
}

public void setCustomer(Customer customer) {
    this.customer = customer;
}

@Column(name = "quantity", length = 45)
public String getQuantity() {
    return this.quantity;
}

public void setQuantity(String quantity) {
    this.quantity = quantity;
}

@Column(name = "price", precision = 12, scale = 0)
public Float getPrice() {
    return this.price;
}

public void setPrice(Float price) {
    this.price = price;
}

=>類OrderId

private int idOrder;
private int customerIdCustomer;

public OrderId() {
}

public OrderId(int idOrder, int customerIdCustomer) {
    this.idOrder = idOrder;
    this.customerIdCustomer = customerIdCustomer;
}

@Column(name = "idOrder", nullable = false)
public int getIdOrder() {
    return this.idOrder;
}

public void setIdOrder(int idOrder) {
    this.idOrder = idOrder;
}

@Column(name = "Customer_idCustomer", nullable = false)
public int getCustomerIdCustomer() {
    return this.customerIdCustomer;
}

public void setCustomerIdCustomer(int customerIdCustomer) {
    this.customerIdCustomer = customerIdCustomer;
}

public boolean equals(Object other) {
    if ((this == other))
        return true;
    if ((other == null))
        return false;
    if (!(other instanceof OrderId))
        return false;
    OrderId castOther = (OrderId) other;

    return (this.getIdOrder() == castOther.getIdOrder())
            && (this.getCustomerIdCustomer() == castOther
                    .getCustomerIdCustomer());
}

public int hashCode() {
    int result = 17;

    result = 37 * result + this.getIdOrder();
    result = 37 * result + this.getCustomerIdCustomer();
    return result;
}

然后,我上了主課:

public static void main(String[] args) {
    Customer customer = new Customer();
    customer.setName("John Doe");
    CustomerDAO.save(customer);

    Order order = new Order();
    order.setQuantity(10);
    order.setPrice(100);
    order.setCustomer(customer);

    OrderDAO.save(order);

}

請注意,我的DAO對象沒有做任何花哨的事情……只是獲取會話並最終調用'save'方法的常規HibernateUtil實現。 我的問題如下。 我從來沒有處理過復合鍵...事先請我提出我的愚蠢問題。 我不想手動處理不同的ID和主鍵。 對於Cutomer類,這不是問題,因為我設置了一個策略,該策略在保存到數據庫時將自動創建id。 對於Order類,我也不想處理。 但是由於鍵是復合鍵,因此Hibernate在嘗試將該對象保存到DB中時會生成一個identifierGenerationExeption ...我想要的是以下內容=>自動生成的訂單ID,並且由於我在訂單對象中設置了客戶,所以我會期望休眠將客戶ID鏈接到組合鍵中的ID。做這件事的正確方法是什么?

謝謝

不鼓勵使用組合鍵,僅應在強制使用組合鍵時使用(例如:遺留數據庫)。 我認為您應該使Order中的Customer_id not null 該訂單有一個簡單的常規ID( idOrder ),所有內容都應簡單明了。

為什么將客戶ID外鍵包含在Order實體的主鍵中? 它的主鍵應該是idOrder Customer_idCustomer應該是外鍵,並且不應該是Order ID的一部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM