JAVA-16567 Potential issue in An Overview of Identifiers in Hibernate/JPA article (#13248)
This commit is contained in:
parent
b6075009b0
commit
1f360ea65d
@ -3,6 +3,7 @@ package com.baeldung.hibernate.pojo;
|
|||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
import javax.persistence.IdClass;
|
import javax.persistence.IdClass;
|
||||||
|
import javax.persistence.ManyToOne;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@IdClass(OrderEntryPK.class)
|
@IdClass(OrderEntryPK.class)
|
||||||
@ -11,6 +12,8 @@ public class OrderEntryIdClass {
|
|||||||
private long orderId;
|
private long orderId;
|
||||||
@Id
|
@Id
|
||||||
private long productId;
|
private long productId;
|
||||||
|
@ManyToOne
|
||||||
|
private User user;
|
||||||
|
|
||||||
public long getOrderId() {
|
public long getOrderId() {
|
||||||
return orderId;
|
return orderId;
|
||||||
@ -28,4 +31,12 @@ public class OrderEntryIdClass {
|
|||||||
this.productId = productId;
|
this.productId = productId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public User getUser() {
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUser(User user) {
|
||||||
|
this.user = user;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import java.io.Serializable;
|
|||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
import javax.persistence.Embeddable;
|
import javax.persistence.Embeddable;
|
||||||
|
import javax.persistence.ManyToOne;
|
||||||
|
|
||||||
@Embeddable
|
@Embeddable
|
||||||
public class OrderEntryPK implements Serializable {
|
public class OrderEntryPK implements Serializable {
|
||||||
@ -11,6 +12,9 @@ public class OrderEntryPK implements Serializable {
|
|||||||
private long orderId;
|
private long orderId;
|
||||||
private long productId;
|
private long productId;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
private User user;
|
||||||
|
|
||||||
public long getOrderId() {
|
public long getOrderId() {
|
||||||
return orderId;
|
return orderId;
|
||||||
}
|
}
|
||||||
@ -27,6 +31,14 @@ public class OrderEntryPK implements Serializable {
|
|||||||
this.productId = productId;
|
this.productId = productId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public User getUser() {
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUser(User user) {
|
||||||
|
this.user = user;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) {
|
if (this == o) {
|
||||||
|
@ -63,9 +63,12 @@ public class IdentifiersIntegrationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSaveCompositeIdEntity_thenOk() {
|
public void whenSaveCompositeIdEntity_thenOk() {
|
||||||
|
User user = new User();
|
||||||
|
|
||||||
OrderEntryPK entryPK = new OrderEntryPK();
|
OrderEntryPK entryPK = new OrderEntryPK();
|
||||||
entryPK.setOrderId(1L);
|
entryPK.setOrderId(1L);
|
||||||
entryPK.setProductId(30L);
|
entryPK.setProductId(30L);
|
||||||
|
entryPK.setUser(user);
|
||||||
|
|
||||||
OrderEntry entry = new OrderEntry();
|
OrderEntry entry = new OrderEntry();
|
||||||
entry.setEntryId(entryPK);
|
entry.setEntryId(entryPK);
|
||||||
@ -77,9 +80,12 @@ public class IdentifiersIntegrationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSaveIdClassEntity_thenOk() {
|
public void whenSaveIdClassEntity_thenOk() {
|
||||||
|
User user = new User();
|
||||||
|
|
||||||
OrderEntryIdClass entry = new OrderEntryIdClass();
|
OrderEntryIdClass entry = new OrderEntryIdClass();
|
||||||
entry.setOrderId(1L);
|
entry.setOrderId(1L);
|
||||||
entry.setProductId(30L);
|
entry.setProductId(30L);
|
||||||
|
entry.setUser(user);
|
||||||
session.save(entry);
|
session.save(entry);
|
||||||
|
|
||||||
assertThat(entry.getOrderId()).isEqualTo(1L);
|
assertThat(entry.getOrderId()).isEqualTo(1L);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user