Answer the question
In order to leave comments, you need to log in
OneToMany in Hibernate?
Comrades, I ask you to help, I can’t catch up with something.
In general, there are two classes "Directory" and "Directory fields".
Both classes are written in HibernateSessionFactoryUtil:
configuration.addAnnotatedClass(Dictionary.class);
configuration.addAnnotatedClass(DictionaryContent.class);
@Entity
@Table(name = "sys_dictionaries")
public class Dictionary {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column
UUID guid = UUID.randomUUID();
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private Set<DictionaryContent> dictionaryContents = new HashSet<>();
.............................
}
@Entity
@Table(name = "sys_dictionaries_content")
public class DictionaryContent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column
UUID guid = UUID.randomUUID();
@Column
UUID dictionaryId;
........................
}
Answer the question
In order to leave comments, you need to log in
Hello!
I myself am just learning Java development (hibernate, spring etc.)
You need to use OneToMany & ManyToOne. From the Dictionary side, get a list of DictionaryContent entries (List or Set), and from the DictionaryContent side, get a Dictionary object.
@Entity
public class Dictionary {
@OneToMany(mappedBy = "dictionary")
private List<DictionaryContent> contentList;
}
@Entity
public class DictionaryContent {
@ManyToOne
@JoinColumn(name = "id")
private Dictionary dictionary;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question