3

everyone! Sorry but I need some help. My db is looking this way, I try to do same throught the Hibernate in Java.

But I don`t understand how I need to annoted this relations with so many different tables. It`s a part of my Abiturient table.

@Entity
@Table (name = "abiturient",
            uniqueConstraints = {@UniqueConstraint(columnNames = {"id"})})

public class Abiturient {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false, unique = true, length = 11)
    @ManyToOne
    private Long id;

    @Column
    private Date data_narodjennya;

    @Column
    private Integer city_village;

It`s a part of my nomer_telefonu table

@Entity
@Table(name = "nomer_telefonu")
public class NomerTelefonu {
    @Id
    @Column(name = "nomer_tel_id", nullable = false, unique = true, length = 11)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long nomer_tel_id;

    @Column(name = "nomer")
    private String nomer;

    @Column(name = "id")
    @OneToMany(fetch = FetchType.LAZY)
    private Set<Abiturient> id;

I don`t think that all there is right, `cos every time I try to solve problem I get an error and need other type.

ziki
  • 519
  • 1
  • 4
  • 8
  • In `NomerTelefonu` the `@Column` is suspicious in conjunction with `@OneToMany`. There should be a `@JoinColumn`. Also the `@ManyToOne` in `Abiturient` seems wrong. I would try to use netbeans to creates entities from DB. – Robert Niestroj Dec 13 '17 at 11:10
  • @RobertNiestroj Thanks a lot! Will wait. What type of relations I should use in `Abiturient`? And how to write it without an error in code?) – ziki Dec 13 '17 at 11:29

1 Answers1

1

Use @OneToMany bidirectional relation.

@Entity
public class Abiturient {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @OneToMany(mappedBy = "abiturent")
    private List<NomerTelefonu> phones = Lists.newArrayList();

} 

@Entity
public class NomerTelefonu{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "nomer_tel_id")
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    private Abiturient abiturent;

} 

Lists.newArrayList() from guava. This is working mapping. Always use the simplest version to experiment.

what is @JoinColumn and how it is used in Hibernate

You can use this project to play with mappings in the unit tests:

https://github.com/v-ladynev/hibernate-experimental

v.ladynev
  • 19,275
  • 8
  • 46
  • 67
  • Thank you!!! Haha, read this link before your answer) But the id in `NomerTelefonu` is the`nomer_tel_id`, no an `id`. Why do I need to create other variable, it is depends on the id or not? how will it know how to associate them? Don\`t catch the thing about _Lists.newArrayList() from guava. This is working mapping._ Will be really happy, if you answer me. – ziki Dec 13 '17 at 12:20
  • @ziki That was just an example. I updated the id column name. "Why do I need to create other variable" Did you ask about an `id`? – v.ladynev Dec 13 '17 at 12:23
  • Thanks. No, in general my question is " Why do I need an other variable?" and How is it work? – ziki Dec 13 '17 at 12:27
  • I mean other variables as this `@OneToMany(mappedBy = "abiturent") private List phones = Lists.newArrayList();` and ` @ManyToOne(fetch = FetchType.LAZY) private Abiturient abiturent;` – ziki Dec 13 '17 at 12:39
  • @ziki Hibernate doesn't work with ids. It works with classes (objects) like `Abiturient`. You can use ids, but it will be much complex. For example, you can't use HQL join with ids using Hibernate version prior 5.10. Also Hibernate can generate a database schema, if classes are used. – v.ladynev Dec 13 '17 at 15:37