diff --git a/persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/pojo/OrderEntryIdClass.java b/persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/pojo/OrderEntryIdClass.java index 18926640af..707713fe57 100644 --- a/persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/pojo/OrderEntryIdClass.java +++ b/persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/pojo/OrderEntryIdClass.java @@ -3,6 +3,7 @@ package com.baeldung.hibernate.pojo; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.IdClass; +import javax.persistence.ManyToOne; @Entity @IdClass(OrderEntryPK.class) @@ -11,6 +12,8 @@ public class OrderEntryIdClass { private long orderId; @Id private long productId; + @ManyToOne + private User user; public long getOrderId() { return orderId; @@ -28,4 +31,12 @@ public class OrderEntryIdClass { this.productId = productId; } + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + } diff --git a/persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/pojo/OrderEntryPK.java b/persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/pojo/OrderEntryPK.java index 637d590629..41ceb6ab25 100644 --- a/persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/pojo/OrderEntryPK.java +++ b/persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/pojo/OrderEntryPK.java @@ -4,6 +4,7 @@ import java.io.Serializable; import java.util.Objects; import javax.persistence.Embeddable; +import javax.persistence.ManyToOne; @Embeddable public class OrderEntryPK implements Serializable { @@ -11,6 +12,9 @@ public class OrderEntryPK implements Serializable { private long orderId; private long productId; + @ManyToOne + private User user; + public long getOrderId() { return orderId; } @@ -27,6 +31,14 @@ public class OrderEntryPK implements Serializable { this.productId = productId; } + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + @Override public boolean equals(Object o) { if (this == o) { diff --git a/persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/IdentifiersIntegrationTest.java b/persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/IdentifiersIntegrationTest.java index 6b54dc80a8..29d1273b37 100644 --- a/persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/IdentifiersIntegrationTest.java +++ b/persistence-modules/hibernate5/src/test/java/com/baeldung/hibernate/IdentifiersIntegrationTest.java @@ -63,9 +63,12 @@ public class IdentifiersIntegrationTest { @Test public void whenSaveCompositeIdEntity_thenOk() { + User user = new User(); + OrderEntryPK entryPK = new OrderEntryPK(); entryPK.setOrderId(1L); entryPK.setProductId(30L); + entryPK.setUser(user); OrderEntry entry = new OrderEntry(); entry.setEntryId(entryPK); @@ -77,9 +80,12 @@ public class IdentifiersIntegrationTest { @Test public void whenSaveIdClassEntity_thenOk() { + User user = new User(); + OrderEntryIdClass entry = new OrderEntryIdClass(); entry.setOrderId(1L); entry.setProductId(30L); + entry.setUser(user); session.save(entry); assertThat(entry.getOrderId()).isEqualTo(1L);