Hibernate generiert seltsame DDL für die Konfiguration mit serieller Spalte und FK. Beispiel (Book.author * -1 Authors.id):Hibernate-Fremdschlüssel für serielle Spalte
@Entity
@Table(name = "books")
public class Book {
private Integer id;
private Author author;
@Id
@Column(name = "id", columnDefinition = "serial")
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@JoinColumn(name = "author", nullable = true)
@ManyToOne(optional = true, fetch = FetchType.LAZY)
public Author getAuthor() {
return author;
}
public void setAuthor(Author author) {
this.author = author;
}
}
@Entity
@Table(name = "authors")
public class Author {
private Integer id;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
DDL Ergebnis für Spalte Autor in Büchern Tabelle:
ALTER TABLE books ADD COLUMN author integer;
ALTER TABLE books ALTER COLUMN author SET NOT NULL;
ALTER TABLE books ALTER COLUMN author SET DEFAULT nextval('authors_seq'::regclass);
Es hat seltsamen Standardwert und auch ich kann es nicht machen NULL festlegbare . Ist es möglich, es zu beheben, ohne columnDefinition für FK zu schreiben?
Ich denke, Sie haben @JoinColumn (Name = "Autor", NULL-Wert = True) hier Fehler bitte versuchen Sie dies * @ JoinColumn (name = "id", nullable = false) * –
@RaviKavaiya Name ist nur ein Name der Spalte, es kann alles sein. – therg
Alte Frage zu diesem Problem http://stackoverflow.com/questions/15511899/cannot-make-manytoone-relationship-nullable – therg