Spring Hibernate MySQL Unidirectional ManyToOne

2019-08-22 11:56发布

I have two (very simple) entities: Parent and Child.

@Entity
class Child {

@Id
@Column(name = "id", nullable = false, updatable = false, columnDefinition = "BINARY(16)")
private UUID id;

@Column(columnDefinition="varchar(4000)")
private String obs;

@NotNull
@ManyToOne(optional = false)
@JoinColumn(nullable = false, updatable = false)
private Parent parent;

(getters and setters)
}

The original obs field had no annotation, so Hibernate created it as a varchar(255), but I needed it to be able to handle a larger text. After the changes (as shown above), the method getParent() always returns null.

Does anyone have any idea why this is happening?

1条回答
欢心
2楼-- · 2019-08-22 12:21

For your Column annotation, rather than using columnDefinition set the length element. Here is the Javadoc of length:

(Optional) The column length. (Applies only if a string-valued column is used.)

Default:

255

You can also see why it defaulted to VARCHAR(255) in your schema: the default is 255. Why your columnDefinition doesn't work, however, I don't know (I've no experience with it).

查看更多
登录 后发表回答