删除JoinTable中joinColumns的非空约束

huangapple go评论66阅读模式
英文:

Drop not null constraint for joinColumns in JoinTable

问题

我有一个实体,依靠它,Hibernate可以为OneToMany关系生成联接表。

@Entity
public class RequestType extends EntityObject {
   @OneToMany(cascade = CascadeType.ALL)
   @JoinTable(name = "MType_MType", joinColumns = @JoinColumn(name = "mtype_id"), 
   inverseJoinColumns = @JoinColumn(name = "inner_request_types_id"))
   private List<LogicalInnerType> innerRequestTypes;
 }

@Entity
public class EntityObject {
  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "EntityID")
  private Integer id;
 }

但是当我运行应用程序时,我收到以下异常:

PSQLException: 错误:列"inner_request_types_id"中的空值违反了非空约束条件

据我所知,任何JoinColumn的默认nullable参数为true。

我应该如何删除非空约束?

英文:

I have an entity, leaning on which hibernate able to generate join table for OneToMany-relations.

@Entity
public class RequestType extends EntityObject {
   @OneToMany(cascade = CascadeType.ALL)
   @JoinTable(name = &quot;MType_MType&quot;, joinColumns = @JoinColumn(name = &quot;mtype_id&quot;), 
   inverseJoinColumns = @JoinColumn(name = &quot;inner_request_types_id&quot;))
   private List&lt;LogicalInnerType&gt; innerRequestTypes;
 }

@Entity
public class EntityObject {
  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = &quot;EntityID&quot;)
  private Integer id;
 }

But when I run application, I receive the following exception:

>PSQLException: EROOR: null value in column "inner_request_types_id" violate constraint NOT NULL

As I know, any JoinColumn has a default true of nullable-parameter.

How could I otherwise drop notnull-constraint?

答案1

得分: 1

是的,JoinColumn.nullable() 的默认值是 true

/** (可选)外键列是否可为 null。 */
boolean nullable() default true;

但是在这里这不相关,因为在 @OneToMany 中不会检查 @JoinColumnnullable()。Hibernate 总是会为两个列添加 not null 约束,因为它期望始终存在一个拥有的实体,并且该列表永远不包含 null 值。

如果您想要首先在列表中添加 null 值(并且这不是错误),您应该考虑一种替代方案。例如,您可以为 RequestType 添加一个布尔属性,指示您希望通过 null 值实现的状态。

顺便提一下,您的模型中还有另一件事情,显然是不正确的。您使用了一个 List,但是您没有添加 @OrderColumn。因此,Hibernate 无法确保任何顺序。如果您确实希望存储特定的顺序,您应该添加 @OrderColumn 注解。或者您可以改用 Set

英文:

Yes, the default value of JoinColumn.nullable() is true:

/** (Optional) Whether the foreign key column is nullable. */
boolean nullable() default true;

But this is not relevant here, because nullable() is not inspected for @JoinColumn in a @OneToMany. Hibernate will always add a not null constraint to both columns, because it expects that there is always an owning entity and that the list never contains null values.

If you wanted to add null values to your list in the first place (and this is not a mistake), you should think about an alternative. For example you could add a boolean attribute to RequestType that indicates the state that you wanted to achieve with the null value.

By the way there is another thing, that is apparently not correct in your model. You are using a List, but you add no @OrderColumn. As a result Hibernate has no possibility to ensure any order. If you really want to store a specific order, you should add an @OrderColumn annotation. Or you use a Set instead.

huangapple
  • 本文由 发表于 2020年9月1日 23:34:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63690907.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定