2

I have defined a very simple database as follow :

MySql Simple Database

I'm using the hibernate plug-in to generate the different classes, including the one-to-many relationship between customer and order. i made this relationship as an identifying relationship as an order could not exist if not linked to a customer.

I have the three following classes :

=> Class Customer

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

=> Class Order :

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

=> Class 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;
}

I then have my main class :

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);

}

Please note that my DAO objects are doing nothing fancy... Just the usual HibernateUtil implementation of getting the session and finally call the 'save' method. My Problem is the following. I have never dealt before with composite key... Excuse me for my stupid question in advance. I don't want to deal manually with the different ids and primary keys. For the Cutomer class, it is not a problem since I set up a strategy that will automatically create the id when saving in database. For the Order class, I don't want to deal with that as well. But since the key is composite, Hibernate generates an identifierGenerationExeption when trying to save this object in DB... What I would have liked is the following => an Order id generated automatically and since I setup the customer in the order Object, I would have expect hibernate to link the customer id to the one in the composite key... What is the correct way of doing this thing ?

Thanks

2 Answers 2

2

Why do you include the customer ID foreign key in the primary key of your Order entity? Its primary key should be idOrder. Customer_idCustomer should be a foreign key, and should not be part of the ID of Order.

Sign up to request clarification or add additional context in comments.

2 Comments

This is the way Hibernate generates automatically an identifying relationship in Java. Customer_idCustomer is indeed a foreign key, but theoretically an order cannot exist if not linked to a customer. I'm quite new in this, should I do differently ?
Yes, you should. Make idOrder the primary key, and make Customer_idCustomer a not null foreign key to Customer.idCustomer. I suspect Hibernate generated this code because you chose to make Customer_idCustomer part of the primary key of the order table. That's what's wrong.
2

Using composite keys is discouraged and should only be used when forced to do it (say: legacy database). I think that you just should make the Customer_id in the Order not null. There is a simple, regular id for the order (idOrder) and everything should be straight forward.

2 Comments

What do you mean by not null ? Should I consider Customer_id in Order as an non-identifying relationship ? If I allow Customer_id in the database to be possibly null, I can end up with Order not linked to a Customer ?
Yes, customer_id is just a relation and has nothing to do with the identity. not null is used to make sure that it can't be null in the database and therefore there is always a customer for every order.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.