Java 冬眠+;JPA找不到@ElementCollection表的列
我试图添加一个Java 冬眠+;JPA找不到@ElementCollection表的列,java,hibernate,embeddable,Java,Hibernate,Embeddable,我试图添加一个@ElementCollection,但在安装后找不到该列,因此我经常收到一个错误。我使用Spring+flyway进行设置。一切都发生在公共模式中 这是我的大目标: @Entity @Table(name = "my_big_table") MyBigObject{ @Id @Column(name=COL_ID) @GeneratedValue(generator="gen_name") @GenericGene
@ElementCollection
,但在安装后找不到该列,因此我经常收到一个错误。我使用Spring+flyway进行设置。一切都发生在公共模式中
这是我的大目标:
@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;
}
然后除了现有的my_big_表之外,我还使用flyway添加my_small_表
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);
在此之后,my_small_表成功创建,但找不到MyBigObject的任何实例,因为它在my_small_表中查找不存在的列。正如您所看到的,它不理解列名应该使用下划线
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".
你知道我会忘记什么吗?我在这两个类中使用的lombock注释会破坏图片吗?如以下所述:
默认情况下,@Id
注释的位置提供默认的访问策略。当放置在字段上时,Hibernate将采用基于字段的访问。当放置在标识符getter上时,Hibernate将使用基于属性的访问
但是使用
@AttributeAccessor
会导致持有@Id
的字段的访问策略发生变化,因此您的@列(name=“small\u object\u type”)
注释被忽略。你可以试着把它放在合适的getter上,它应该会起作用。但是,不要混淆实体字段的访问策略被认为是一种很好的做法。请注意,mysmalltab0\uuu
通常不是Hibernate将访问的表名,而只是一个别名。你能发布实际的查询吗?(您可能需要启用查询日志记录)还可以尝试使用不带下划线的表名。我记得读过一些关于默认情况下转换下划线的问题,mysmalltab0
表明Hibernate实际上从注释中提取了mysmall\u表
(至少这是您发布的代码唯一提到它的地方)。请在MyBigObject
上显示您将@Id
注释放在何处。您能同时显示您的hibernate配置吗?@SternK我添加了idannotation@Thomas我们项目中的所有表格都带有下划线,它们一直都写得很好。似乎删除该表会导致另一个错误,因此我假设它只能找到coumn@user2957954尝试删除@AttributeAccessor(CommonConstants.HIBERNATE\u ACCESS\u属性)
注释。
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".