JAVA-16567 Potential issue in An Overview of Identifiers in Hibernate/JPA article (#13248)

This commit is contained in:
anuragkumawat 2023-01-10 01:27:11 +05:30 committed by GitHub
parent b6075009b0
commit 1f360ea65d
3 changed files with 29 additions and 0 deletions

View File

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

View File

@ -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) {

View File

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