I have a logic that saves some data and I use spring boot + spring data jpa.
Now, I have to save one object, and after moment, I have to save another objeect. those of object consists of three primary key properties. - partCode, setCode, itemCode.
let's say first object has a toString() returning below:
SetItem(partCode=10-001, setCode=04, itemCode=01-0021, qty=1.0, sortNo=2, item=null)
and the second object has a toString returning below:
SetItem(partCode=10-001, setCode=04, itemCode=01-0031, qty=1.0, sortNo=2, item=null)
there is a difference on itemCode value, and itemCode property is belonged to primary key, so the two objects are different each other.
but in my case, when I run the program, the webapp saves first object, and updates first object with second object value, not saving objects seperately.
(above image contains different values from this post question)
Here is my entity information:
/**
* The persistent class for the set_item database table.
*
*/
@Data
@DynamicInsert
@DynamicUpdate
@Entity
@ToString(includeFieldNames=true)
@Table(name="set_item")
@IdClass(SetGroupId.class)
public class SetItem extends BasicJpaModel<SetItemId> {
private static final long serialVersionUID = 1L;
@Id
@Column(name="PART_CODE")
private String partCode;
@Id
@Column(name="SET_CODE")
private String setCode;
@Id
@Column(name="ITEM_CODE")
private String itemCode;
private Double qty;
@Column(name="SORT_NO")
private int sortNo;
@Override
public SetItemId getId() {
if(BooleanUtils.ifNull(partCode, setCode, itemCode)){
return null;
}
return SetItemId.of(partCode, setCode, itemCode);
}
@ManyToMany(fetch=FetchType.LAZY)
@JoinColumns(value = {
@JoinColumn(name="PART_CODE", referencedColumnName="PART_CODE", insertable=false, updatable=false)
, @JoinColumn(name="ITEM_CODE", referencedColumnName="ITEM_CODE", insertable=false, updatable=false)
})
private List<Item> item;
}
So the question is, how do I save objects separately which the objects' composite primary keys are partially same amongst them.
EDIT: The entity extends below class:
@Setter
@Getter
@MappedSuperclass
@DynamicInsert
@DynamicUpdate
public abstract class BasicJpaModel<PK extends Serializable> implements Persistable<PK>, Serializable {
@Override
@JsonIgnore
public boolean isNew() {
return null == getId();
}
}
EDIT again: embeddable class. after soneone points out embeddable class, I noticed there are only just two properties, it should be three of it. thank you.
@Data
@NoArgsConstructor
@RequiredArgsConstructor(staticName="of")
@Embeddable
public class SetGroupId implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
@NonNull
private String partCode;
@NonNull
private String setCode;
}
Check howto use @EmbeddedId
& @Embeddable
(update you might need to use AttributeOverrides
in id field, not sure if Column
s in @Embeddable
works).
You could create class annotated @Embeddable
and add all those three ID fields there.
@Embeddable
public class MyId {
private String partCode;
private String setCode;
private String itemCode;
}
Add needed getters & setters.
Then set in class SetItem
this class to be the id like `@EmbeddedId´.
public class SetItem {
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name="partCode",
column=@Column(name="PART_CODE")),
@AttributeOverride(name="setCode",
column=@Column(name="SET_CODE"))
@AttributeOverride(name="itemCode",
column=@Column(name="ITEM_CODE"))
})
MyId id;
Check also Which annotation should I use: @IdClass or @EmbeddedId