I have these entities
NormalizedChannelStock.java
@Entity
@Table(name = "stocks")
public class NormalizedChannelStock {
@EmbeddedId
private NormalizedChannelStockId id;
@Column(name = "qty")
private int qty;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "channel_id", insertable = false, updatable = false)
private Channel channel;
@Column(name = "created_at", updatable = false)
private Timestamp createdAt;
@Column(name = "updated_at", updatable = false)
private Timestamp updatedAt;
public NormalizedChannelStockId getId() {
return id;
}
public void setId(NormalizedChannelStockId id) {
this.id = id;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
public Channel getChannel() {
return channel;
}
public void setChannel(Channel channel) {
this.channel = channel;
}
public Timestamp getCreatedAt() {
return createdAt;
}
public Timestamp getUpdatedAt() {
return updatedAt;
}
}
NormalizedChannelStockId.java
@Embeddable
public class NormalizedChannelStockId implements Serializable {
@Column(name = "channel_id")
private Integer channelId;
@Column(name = "sku")
private String sku;
public NormalizedChannelStockId() {
}
public NormalizedChannelStockId(Integer channelId, String sku) {
this.channelId = channelId;
this.sku = sku;
}
public Integer getChannelId() {
return channelId;
}
public void setChannelId(Integer channelId) {
this.channelId = channelId;
}
public String getSku() {
return sku;
}
public void setSku(String sku) {
this.sku = sku;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
NormalizedChannelStockId that = (NormalizedChannelStockId) o;
return channelId.equals(that.channelId) &&
sku.equals(that.sku);
}
@Override
public int hashCode() {
return Objects.hash(channelId, sku);
}
}
Channel.java
@Entity
@Table(name = "channels")
public class Channel {
@Id
@Column(name = "channel_id")
private int channelId;
@Column(name = "channel_name")
private String channelName;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "store_id", insertable = false, updatable = false)
private Store store;
public int getChannelId() {
return channelId;
}
public void setChannelId(int channelId) {
this.channelId = channelId;
}
public String getChannelName() {
return channelName;
}
public void setChannelName(String channelName) {
this.channelName = channelName;
}
public Store getStore() {
return store;
}
public void setStore(Store store) {
this.store = store;
}
}
The problem I'm facing is when I call
List<NormalizedChannelStock> entitiesToSave = ...
List<NormalizedChannelStock> savedEntities = normalizedChannelStockService.saveAll(entitiesToSave);
The returned entities in savedEntities
have their Channel
inner objects set to null
, as well as their created_at and updated_at as shown
Is this normal behaviour? When I run a findAllById
on the Repository, the Channel
s inside the Entities are loaded lazily properly, so I believe the entities are properly mapped in code. The problem is after I save them.
Does JPA not reload the entity after saving it?