【发布时间】:2022-12-21 07:55:58
【问题描述】:
我在 JoinColumns 中有以下用法
@Entity
public class EntityOne{
private String action;
private String type;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumns({
@JoinColumn(name = "action", referencedColumnName = "action_name", updatable = false, insertable = false),
@JoinColumn(name = "type", referencedColumnName = "type_name", updatable = false, insertable = false)
})
private Entitytwo entitytwo;
}
和
@Entity
public class EntityTwo {
@Id
@Column(name = "type_name")
private String typeName;
@Id
@Column(name = "action_name")
private String actionName;
}
此设置导致休眠错误
Referenced column '" + column.getName()
+ "' mapped by target property '" + property.getName()
+ "' occurs out of order in the list of '@JoinColumn's
如果我更改 @JoinColumns 中的顺序,它似乎可以工作,但可以在下次应用程序启动时停止工作。
相关代码开头的 hibernate cmets 状态:
// Now we need to line up the properties with the columns in the
// same order they were specified by the @JoinColumn annotations
// this is very tricky because a single property might span
// multiple columns.
// TODO: For now we only consider the first property that matched
// each column, but this means we will reject some mappings
// that could be made to work for a different choice of
// properties (it's also not very deterministic)
在相关代码本身:
// we have the first column of a new property
orderedProperties.add( property );
if ( property.getColumnSpan() > 1 ) {
if ( !property.getColumns().get(0).equals( column ) ) {
// the columns have to occur in the right order in the property
throw new AnnotationException("Referenced column '" + column.getName()
+ "' mapped by target property '" + property.getName()
+ "' occurs out of order in the list of '@JoinColumn's");
}
currentProperty = property;
lastPropertyColumnIndex = 1;
}
我应该如何设置 @JoinColumn 以使其始终如一地工作?
【问题讨论】:
-
这是一个非常奇怪的要求。如果没有错误,请提交错误,因为 JPA 对注释和 java 属性的排序没有要求。可能会显示堆栈和错误消息,因为它可能指示更多可能指向更好解决方法的上下文。
-
你有什么进步吗?我遇到了同样的问题。你有没有用hibernate提出这个问题。对我来说这似乎是一个错误
-
@Chris - 不,我的团队在更高优先级的任务到来时放弃了它。我会回来的。 No 没有提出这个问题,因为从上面的文档来看它似乎是故意的。
标签: spring-boot hibernate jpa spring-boot-3 hibernate-6.x