@JoinColumn 升级到 spring-boot-3 (Hibernate 6 ) 时“发生故障”

问题描述 投票:0回答:3

我在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 注释指出:

    // 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才能使其始终有效?

spring-boot hibernate jpa hibernate-6.x
3个回答
0
投票

如果

action
type
EntityOne
属性是指
EntityTwo
的相应属性,那么它们是无用且具有误导性的。

属性

private Entitytwo entitytwo
足以设计
@ManytoOne
关系。

删除这两个属性,如果您需要获取链接到

action
type
entityTwo
entityOne
值,只需使用
entityOne.entitytwo.getAction()
(或
entityOne.entitytwo.getType()
)即可。


0
投票

我刚刚尝试了您在 Hibernate 6.1 中发布的代码,没有发现任何错误。即使排列了各种东西之后,仍然没有错误。因此,为了让事情变得更困难,我在 FK 中添加了第三列,并尝试对事物进行排列。仍然没有错误。

我现在有:

@Entity
public class EntityOne {

  @Id @GeneratedValue
  Long id;

  String action;
  String type;
  int count;

  @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
  @NotFound(action = NotFoundAction.IGNORE)
  @JoinColumns({
          @JoinColumn(name = "count", referencedColumnName = "count", updatable = false, insertable = false),
          @JoinColumn(name = "action", referencedColumnName = "action_name", updatable = false, insertable = false),
          @JoinColumn(name = "type", referencedColumnName = "type_name", updatable = false, insertable = false),
  })
  EntityTwo entitytwo;
}

@Entity
public class EntityTwo {

  @Id
  @Column(name = "type_name")
  String typeName;

  @Id
  @Column(name = "count")
  int count;

  @Id
  @Column(name = "action_name")
  String actionName;

}

和测试代码:

@DomainModel(annotatedClasses = {EntityOne.class, EntityTwo.class})
@SessionFactory
public class BugTest {
    @Test
    public void test(SessionFactoryScope scope) {
        scope.inTransaction( session -> {
            EntityOne entityOne = new EntityOne();
            entityOne.action = "go";
            entityOne.type = "thing";
            EntityTwo entityTwo = new EntityTwo();
            entityTwo.actionName = "go";
            entityTwo.typeName = "thing";
            entityOne.entitytwo = entityTwo;
            session.persist( entityOne );
        } );
    }
}

也许有些事你没有告诉我们?例如,与您原来发布的代码中缺少的

@Id
EntityOne
有关吗?

以防万一,也尝试了这个变体:

@Entity
public class EntityOne {

  @Id
  String action;
  @Id
  String type;
  @Id
  int count;

  @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
  @NotFound(action = NotFoundAction.IGNORE)
  @JoinColumns({
          @JoinColumn(name = "action", referencedColumnName = "action_name", updatable = false, insertable = false),
          @JoinColumn(name = "count", referencedColumnName = "count", updatable = false, insertable = false),
          @JoinColumn(name = "type", referencedColumnName = "type_name", updatable = false, insertable = false),
  })
  EntityTwo entitytwo;
}

但仍然没有错误。


0
投票

使用这个休眠版本

    <dependency>
        <groupId>org.hibernate.orm</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>6.1.0.Final</version>
    </dependency>
© www.soinside.com 2019 - 2024. All rights reserved.