Spring Hibernate MySQL单向ManyToOne [英] Spring Hibernate MySQL Unidirectional ManyToOne
问题描述
我有两个(非常简单的)实体:父级和子级.
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)
}
原始的obs字段没有注释,因此Hibernate将其创建为varchar(255),但我需要它能够处理更大的文本.更改之后(如上所示),方法getParent()始终返回null.
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?
推荐答案
对于您的 Column
注释,而不是使用 columnDefinition
来设置
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.)
默认值:
255
您还可以看到为什么它在架构中默认为 VARCHAR(255)
:默认值为255.为什么您的 columnDefinition
不起作用,但是,我不知道不知道(我没有经验).
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).
这篇关于Spring Hibernate MySQL单向ManyToOne的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!