Hibernate Does Not Create Schema Tables

Asked

Viewed 27 times

0

What I realized is that Hibernate doesn’t seem to create tables the way they should be presented in the database. I’m using Mysql and realized the following: Example of the structure: Table Course and another Teacher, where the table courses has a Foreing Key with the teacher code.

If you create the tables directly by the database administrator, there is a FOREING KEY guide that shows the foreign keys created. And for final test if I do the inclusion of a course and use a teacher who does not exist the administrator of the database refuses.

Using Hibernate doesn’t happen anymore. Nor does the foreign key guide display the foreign key, which makes it possible to include a course and use the code of a non-existent teacher.

@Entity
@Table(name = "CURSO")
public class Curso implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "cur_codigo")
    private Long codigo;
    @Column(name = "cur_nome")
    private String nome;
    @Column(name = "cur_diashorario")
    private String diashorario;

    @ManyToOne
    @JoinColumn(name = "cur_profcodigo", referencedColumnName = "prof_codigo", foreignKey = @ForeignKey(name = "CUR_PRO_FK"))
    private Professor professor;

Is there something missing conceptual that I don’t understand ?

  • I tested on my machine using postgres (I don’t have access to mysql at work), he usually created Foreign key with the correct names. I deduced that in class Professor the id is mapped this way: @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 @Column(name = "prof_codigo")
 private Long codigo; . If you can post more details of the Teacher class.

  • @ Entity @Table(name = "TEACHER") public class Professor Implements Serializable { private Static final long serialVersionUID = 1L; @ Id // indicates primary key @ Generatedvalue(Strategy = Generationtype.IDENTITY) // generates autimetic code @ Column(name = "prof_code") Private long code; @ Column(name = "prof_name", length = 50, nullable = false) private String name;

1 answer

1

In the Teacher class, create a Course list, and use the annotation

@OneToMany(mappedBy="professor")
private List<Curso> lsCurso;

Browser other questions tagged

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