Answer the question
In order to leave comments, you need to log in
How to attach a list of other entities to an entity and then work with them and with the list?
Hey!
There is a certificate (Certificate), it can have many tags (Tag).
Actually, I would like to understand how I can now work with its tags through the class of a specific Certificate? Get all his tags, change one/several? Add/remove new, etc.
Methods for this kind of started, in fact, before jpa there were lists so that RestController at least issued something and it was possible to write / read something at run-time.
How to stick the tag repository there - I'll never know.
repository here
....
@MappedSuperclass
public abstract class CommonFields implements Serializable {
private static final long serialVersionUID = 5618898539406697796L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonProperty("id")
@Column(name = "id", nullable = false)
private long id;
@JsonProperty("name")
@Column(name = "name", nullable = true)
private String name;
@JsonProperty("dateCreated")
@Column(name = "dateCreated", nullable = false)
private ZonedDateTime dateCreated;
@JsonProperty("dateUpdated")
@Column(name = "dateUpdated", nullable = false)
private ZonedDateTime dateUpdated;
}
...
package by.alex.certws.domain;
@JsonSerialize
@Entity
@Table(name = "certificates")
public class Certificate extends CommonFields {
private static final long serialVersionUID = -3444580092047125843L;
@JsonProperty("content")
private String content;
@JsonProperty("tags")
@OneToMany(targetEntity=Tag.class, mappedBy="id",cascade=CascadeType.ALL, fetch = FetchType.LAZY)
private List<Tag> tags = new ArrayList<>();
public Certificate() {
super();
}
public Certificate(long certId, String certName) {
super(certId, certName);
}
public Certificate(long certId, String certName, String content) {
super(certId, certName);
this.setContent(content);
}
public Certificate(long id, String name, String content, String[] tags) {
super(id, name);
this.setContent(content);
for (String tag : tags) {
addTag(tag);
}
}
public String getContent() {
return this.content;
}
public void setContent(String target) {
this.content = target;
this.UpdateNotify();
}
public List<Tag> getTags() {
return this.tags;
}
public Optional<Tag> findTagByName(String name) {
return this.tags.stream().filter(Objects::nonNull)
.filter(t -> (Objects.nonNull(t.getName()) && t.getName().compareTo(name) == 0)).findAny();
}
public long addTag(String tagName) {
Optional<Tag> match = this.findTagByName(tagName);
if (match.isPresent()) {
return match.get().getId();
// throw new ETagExistsException();
} else {
return 0;
}
}
public Boolean removeTag(String target) {
Optional<Tag> match = this.findTagByName(target);
if (match.isPresent()) {
return this.tags.remove(match.get());
} else {
// TODO: MUST throw exception
return false;
}
}
}
package by.alex.certws.domain;
....
@Entity
@Table(name = "tags")
public class Tag extends CommonFields {
@JsonProperty("references")
@OneToMany(targetEntity=Certificate.class, mappedBy="id",cascade=CascadeType.ALL, fetch = FetchType.LAZY)
private List<Certificate> refs = new ArrayList<>();
private static final long serialVersionUID = -2535451222904563266L;
public Tag() {
super();
}
public Tag(long id, String name) {
super(id, name);
}
List<Certificate> getReferences() {
return this.refs;
}
}
public interface CertificatesRepository extends PagingAndSortingRepository<Certificate, Long> {}
...
public interface TagsRepository extends PagingAndSortingRepository<Tag, Long> {}
Answer the question
In order to leave comments, you need to log in
Good afternoon!
First, based on your words:
There is a certificate (Certificate), it can have many tags (Tag).
It is assumed that in the tags table there may be a bunch of duplicates by name, i.e. 3 certificates can have the same "Java" tag or something like that.
certificate_id | tag_id
where you can store data. And correspondingly,How to attach a list of other entities to an entity and then work with them and with the list?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question