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.
– Isaías de Lima Coelho
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.
– Leonardo Rosa
I checked in the database script and also does not present the relationship between the entities
– Leonardo Rosa
You can post the Date and Calendar entities to the Bank and the method in which you save the date entity ?
– Isaías de Lima Coelho
I edited the question by putting the method I used to send the dates to the bank
– Leonardo Rosa
Well, at some point in that stream, you set the calendar within date?
– Isaías de Lima Coelho
what is dates ? a list<Data> or a Date object ?
– Isaías de Lima Coelho
No If the calendar at any time, Dates is the class where the dates selected by the users are stored.
– Leonardo Rosa
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.
– Isaías de Lima Coelho
It makes it easier if you post as your entity in the bank
– Isaías de Lima Coelho
Declaring the foreign key, you should not automatically enter the ID ?
– Leonardo Rosa
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.
– Isaías de Lima Coelho
And how could I make that relationship?
– Leonardo Rosa
posted in reply, look if it helps you
– Isaías de Lima Coelho