Answer the question
In order to leave comments, you need to log in
How to push data into the model?
There are 3 entities:
1, Zone
2. Description of the Zone
3. Language The
description of the Zone contains the id "Zone" and the id "Language".
I need to get the Zone Description object from the Zone,
when I got the Zone by id, I can transfer the Language id to it.
How do I map objects if the Zone doesn't know about Languages?
Answer the question
In order to leave comments, you need to log in
Mapping is easy
@Entity
public class Zone {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToOne(mappedBy = "zone")
private ZoneDescription description;
// Геттеры и сеттеры не указаны для краткости
}
@Entity
public class ZoneDescription {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToOne
private Zone zone;
@ManyToOne(optional = false)
private ZoneLanguage language;
// Геттеры и сеттеры не указаны для краткости
}
@Entity
public class ZoneLanguage {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
/* Раскоментировать, если нужна двунаправленная связь
@OneToMany(mappedBy = "language")
private Set<ZoneDescription> descriptions = new HashSet<>();
*/
// Геттеры и сеттеры не указаны для краткости
}
Zone zone = zoneRepository.getById(zoneId);
ZoneDescription description = zone.getDescription();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question