Nested relationship with Composite IDs that have Entity Ids

Hello everyone and thank you for any help or advice.

I’ve been stuck for a few days trying to figure out how to make a new relation between an Entity with a Composite ID which references another Entity class as part of the Composite ID.

Originally I had the following code, which works fine:

@Entity
public class Order {

	@Id
	private Integer id;

	@OneToMany(mappedBy = "id.order")
	private List<OrderProduct> comandaProductes;

...
}
@Data
@Embeddable
public class OrderProductID implements Serializable {

	private String productId;

	@ManyToOne
	private Order order;
...
}
@Entity
public class OrderProduct implements Serializable {

	@EmbeddedId
	private OrderProductID id;
	
	...
}

But now I want to add another one-to-many relation from OrderProduct to a new class OrderProductExtras:

@Entity
public class OrderProduct implements Serializable {

	@EmbeddedId
	private OrderProductID id;
	
	@OneToMany(mappedBy = "orderProduct", cascade = CascadeType.ALL, orphanRemoval = true)
	private List<OrderProductExtras> orderProductExtras;	

    ...
}
@Embeddable
public class OrderProductExtrasPK implements Serializable {

    private Integer extrasId;

    @ManyToOne
    private OrderProduct orderProduct; 
    ...
}

@Entity
@Data
public class OrderProductExtras {

    @EmbeddedId
    private OrderProductExtrasPK id;

    @ManyToOne
    @JoinColumns({
            @JoinColumn(name = "product_fk", referencedColumnName = "productId"),
            @JoinColumn(name = "order_fk", referencedColumnName = "orderId")
    })
    private OrderProduct orderProduct;
    ...
}

It does create the tables on the database but it fails to generate the FK constraint:

ERROR: foreign key constraint "fkg37ti7qy3rk7ywie9sxwh2fg1" cannot be implemented
  Detail: Key columns "order_id" and "product_id" are of incompatible types: numeric and character varying.

I’ve tried many things but I can’t figure out how to properly reference the OrderId from OrderProductExtras.

There’s nothing similar on ORM User Guide and haven’t found anything online.

Any help would be appreciated.

What are the types of the foreign key columns in the OrderProductExtras table? And the primary key of OrderProduct? If the tables are created it means that Hibernate is able to interpret your mappings, that look correct to me, but from the foreign key creation error I suspect there might be a type mismatch.

If you believe this to be a bug in Hibernate, please create an issue in the issue tracker with a test case that reproduces the issue.

Hello mbladel

Thank you for your response. I did more digging into the issue, and the problem from what I can see is the generated SQL.

SQL generated by hibernate:

alter table order_product_extras 
       add constraint FKmlvgvxc3htmf7056x7riw16vf 
       foreign key (order_id, product_id) 
       references order_product

If I run this SQL code directly on the DB but specifying the reference columns it works fine:

order_product (order_id, product_id)

alter table order_product_extras 
       add constraint FKmlvgvxc3htmf7056x7riw16vf 
       foreign key (order_id, product_id) 
       references order_product (order_id, product_id)

Now that the foreign key is created I get no errors at the start and the applications works fine, I can insert, read and delete data without problem from Java.

I’m not knowledgeable enough of Hibernate to know if this is really an issue or I’m just doing something wrong on the relation definition…

I will try to do more digging but I would be surprised if this was a Hibernate issue as this isn’t any special kind of relationship and many people should have encountered this issue before I did.

If the constraint generated by Hibernate is wrong, but your mapping is correctly interpreted and you can still use it, there might be a bug regarding DDL generation. Your use case is not that common, a to-one with a composite foreign key that references an embedded identifier which as part of its key has another to-one.

It would be nice if you could come up with a simple test case that reproduces the error you encountered, and if you can please open a new issue in the issue tracker and we will look into it.