简体   繁体   English

Hibernate + JPA 找不到 @ElementCollection 表的列

[英]Hibernate + JPA does not find the column for an @ElementCollection table

I am trying to add an @ElementCollection but the column is not found after the setup, so I constantly receive an error.我正在尝试添加一个@ElementCollection ,但在设置后找不到该列,因此我经常收到错误消息。 I use Spring + flyway for the set up.我使用 Spring + flyway 进行设置。 Everything happens in the public schema一切都发生在公共模式中

So here is my big object:所以这是我的大 object:

@Entity
@Table(name = "my_big_table")
MyBigObject{

   @Id
   @Column(name=COL_ID)
   @GeneratedValue(generator="gen_name")
   @GenericGenerator(
            name = "gen_name",
            strategy = "seq_name"
        )
   @AttributeAccessor(CommonConstants.HIBERNATE_ACCESS_PROPERTY)
   private long id;
    ...
    ...
        @ElementCollection(fetch = FetchType.EAGER)
        @CollectionTable(
            name = "my_small_table",
            joinColumns = @JoinColumn(name = "big_object_id")
        )
    private List<MySmallObject> mySmallObjects;
}

Here is my embedded object:这是我的嵌入式 object:

@Embeddable
public class MySmallObject {

    @Column(name = "small_object_type")
    private String smallObjectType;
}

Then besides the existing my_big_table table I add my_small_table using flyway然后除了现有的my_big_table表之外,我使用 flyway 添加my_small_table

create table if not exists my_small_table
(
    big_object_id      bigint not null,
    small_object_type     varchar(64) not null
);

alter table my_small_table
      add constraint FK_my_small_table
      foreign key (big_object_id)
      references my_big_table (id);

After this the my_small_table is successfully created but any instance of MyBigObject cannot be found because it looks for a column in the my_small_table that does not exist.在此之后,my_small_table 成功创建,但无法找到 MyBigObject 的任何实例,因为它在my_small_table中查找不存在的列。 As you can see it does not understand that the column name should use an underscore.如您所见,它不理解列名应该使用下划线。

Big error trace ands with the following message:
Caused by: org.postgresql.util.PSQLException: ERROR: column mysmalltab0_.smallobjecttype does 
not exist
09:17:24.994 INFO  - STDOUT:   Hint: Perhaps you meant to reference the column "mysmalltab0_.smallobjecttype".

Do you know what I could forget?你知道我会忘记什么吗? Could lombock annotations that I also use for both classes spoil the picture?我也用于这两个类的 lombock 注释会破坏图片吗?

As it's stated in the documentation :正如文档中所述:

By default, the placement of the @Id annotation gives the default access strategy.默认情况下, @Id注释的位置提供了默认访问策略。 When placed on a field, Hibernate will assume field-based access.放置在字段上时,Hibernate 将采用基于字段的访问。 When placed on the identifier getter, Hibernate will use property-based access.当放置在标识符 getter 上时,Hibernate 将使用基于属性的访问。

But the usage of the @AttributeAccessor leads to the changing access strategy for the field that hold @Id and as result your @Column(name = "small_object_type") annotation just was ignored.但是@AttributeAccessor的使用会导致更改包含@Id的字段的访问策略,因此您的@Column(name = "small_object_type")注释被忽略了。 You can try to put it on the appropriate getter and it should work.您可以尝试将其放在适当的吸气剂上,它应该可以工作。 But it's considered a good practiсe not to mix up access strategies for the entity fields.但不混淆实体字段的访问策略被认为是一种很好的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM