Problems declaring a foreign key using Ibernate

Asked

Viewed 49 times

0

Hello.I need help declaring a foreign key using Hibernate in a project with JSF, I consulted several websites and handouts and could not find a solution. Where I have the classes already mapped calendar and dates, where a calendar contains several dates and dates contain a calendar.

Below is the code used:

Calendar class

@Id
@Column(name = "calendario_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private int calendario_id;

@Column(name = "ano")
private int ano;

@OneToMany(mappedBy = "calendarioDatas")
@Cascade(org.hibernate.annotations.CascadeType.ALL)
private List<Datas> datasCalendario = new ArrayList<>();

Class dates

@Id
@Column(name = "data_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private int data_id;

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

@Column(name = "data")
private Date data;

@ManyToOne
@JoinColumn(name="calendarioDatas", nullable = false)
@org.hibernate.annotations.ForeignKey(name = "calendarioDatas_fk")
private Calendario calendarioDatas = new Calendario();

Class where you have the method to register the dates after being selected

public void commitBancoDeDados(){
   hibernateUtil.cadastrar(calendario); 
   hibernateUtil.atualizar(datas);
}

HibernateUtil.register makes the persistence of the calendar class, whereas hibernateUtil.update merges the dates class.

  • In the context of databases, the concept of foreign key refers to the type of relationship between the tables, this you have mapped with '@Manytoone' and '@Onetomany'. I don’t understand your question.

  • Right. But when registering the calendar does not present the relationship between the two entities. Because register the dates and each registered date should receive the calendar id.

  • I checked in the database script and also does not present the relationship between the entities

  • You can post the Date and Calendar entities to the Bank and the method in which you save the date entity ?

  • I edited the question by putting the method I used to send the dates to the bank

  • Well, at some point in that stream, you set the calendar within date?

  • what is dates ? a list<Data> or a Date object ?

  • No If the calendar at any time, Dates is the class where the dates selected by the users are stored.

  • So, imagine that I have an id calendar = 1 saved in the database, when I save a new date I need to inform which calendar it relates, so before saving or updating the data entity I need to do : data.setCalendarioData (calendar); thus Hibernate will put the calendar ID in the FK you created.

  • It makes it easier if you post as your entity in the bank

  • Declaring the foreign key, you should not automatically enter the ID ?

  • I don’t understand what you mean by Declaring, but java doesn’t automatically associate that the date X will relate to the Y calendar, it doesn’t happen unless you explicitly say that one object is related to the other.

  • And how could I make that relationship?

  • posted in reply, look if it helps you

Show 9 more comments

1 answer

0


I did this example showing how you should do to associate the entities in question.


public class Teste {

    public static void main(String[] args) {
        Calendario calendario = new Calendario();
        calendario.setId(1L);

        Datas datas = new Datas();
        datas.setId(1L);

        // método que salva o calendario
        // hibernateUtil.cadastrar(calendario);

        // depois do calendario esta salvo , você relaciona a data a ele.
        datas.setCalendario(calendario);

         // método que salva a data.
        // hibernateUtil.cadastrar(data);

    }

}

class Calendario {
    private Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}

class Datas {
    private Long id;

    private Calendario calendario;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Calendario getCalendario() {
        return calendario;
    }

    public void setCalendario(Calendario calendario) {
        this.calendario = calendario;
    }
}

  • 1

    Thanks for your help, so I modify my code and do the tests, but thank you for your help again.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.