V
V
Valera Programmer2017-11-30 23:49:41
Java
Valera Programmer, 2017-11-30 23:49:41

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

1 answer(s)
S
Sergey Gornostaev, 2017-12-01
@noddux

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<>();
*/

    // Геттеры и сеттеры не указаны для краткости
}

And then just as easily in the application code
Zone zone = zoneRepository.getById(zoneId);
ZoneDescription description = zone.getDescription();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question