Last active
December 18, 2023 11:45
-
-
Save gtiwari333/8db9d5e4acf3dd17ea9bd65b22735eec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
public class Parent implements Serializable { | |
@Id | |
@GeneratedValue(strategy = GenerationType.AUTO) | |
private Long id; | |
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true) | |
private Set<Child> children = new HashSet<>(); | |
... | |
} | |
@Entity | |
public class Child implements Serializable { | |
@Id | |
@GeneratedValue(strategy = GenerationType.AUTO) | |
private Long id; | |
@ManyToOne | |
@JoinColumn(name = "parent_id") | |
private Parent parent; | |
... | |
} | |
void processChildDto(Parent parent, Set<ChildDto> childDtos) { | |
Set<Child> children = new HashSet<>(); | |
for (ChildDto dto : childDtos) { | |
Child child; | |
if (dto.getId() == null) { | |
//CREATE MODE: create new child | |
child = new Child(); | |
child.setParent(parent); //associate parent | |
} else { | |
//UPDATE MODE : fetch by id | |
child = childRepository.getOne(dto.getId()); | |
} | |
BeanUtils.copyProperties(dto, child);//copy properties from dto | |
children.add(child); | |
} | |
parent.getChildren().clear(); | |
parent.getChildren().addAll(children); | |
//finally save the parent ( and children) | |
parentRepository.save(parent); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your code efficiently processes
ChildDto
objects for aParent
, handling creation and updates. Consider usingfindById
instead ofgetOne
for safer fetching, and exploresaveAll
for batch operations. This enhances readability and ensures robustness in entity retrieval and persistence gamers! I just wanted to let you know about the amazing casino bonuses Canada has to offer. As a first-time player, I was blown away by the list of casinos that are offered at https://bestcasinoplay.com/online-slots/bonuses/ deposit match, free cash, and even those amazing free spins. It’s a great way to start your gaming journey right after you sign up. And what’s more – the goodness doesn’t end there! For existing players, you can enjoy reload bonuses as well as cash-back benefits.