How to create table with unidirectional relation in Hibernate?

i have the empty database in mysql, and two java entites. One of those have unidirectional relation. When hibernate tryes to create tables, i got the error:

Error executing DDL "alter table entry add constraint FK6ov2k83sx3crs9v3q8nvjuf1j foreign key (category_name) references category (name)" via JDBC Statement

There are my entites:

public class Entry {
    @GeneratedValue( strategy = GenerationType.IDENTITY)
    private int id;

    private String myfio;

    private String descr;

    @OneToOne(cascade = CascadeType.ALL)
    private Category category;

And the second:

public class Category {
    private String name;

How to create tables without errors?

>Solution :

OneToOne relationship shares the same id. So it should be the same type, but the first one is int (actually it should be Integer to allow null value for the transient (not stored) entities) and the second one is String. It seems you simply missed a line. Also, it worths to mention Vlad Mihalchea’s article

Leave a Reply