微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Hibernate生成错误的主键

如何解决Hibernate生成错误的主键

我有一个休眠实体。

@AllArgsConstructor @NoArgsConstructor @Getter
@Entity
@Table(name = "app_category_link",schema = "mariott_application")
public class AppCategory {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "app_category_link_id")
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "app_id")
    private App app;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "category_id")
    private Category category;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id_who_added")
    private User userWhoAdded;

    @Column(name = "date_add")
    private zoneddatetime dateAdded;
}

我有一个@DataJpaTest生成了这样的DDL。令人惊讶的是,它产生了意外的主键。

create table mariott_application.app_category_link
(
    app_category_link_id int8      not null,date_add             timestamp,app_id               bigserial not null,category_id          int8      not null,user_id_who_added    int8,primary key (app_id,category_id)     -- wrong
)

为什么Hibernate生成错误的主键?

解决方法

当您多次使用表名时可能会发生这种情况同样在 keycloakService.findUserByEmailOrUsername( user.getKeycloakUsername() ) .ifPresent( userRepresentation -> { CredentialRepresentation credential = new CredentialRepresentation(); credential.setType( CredentialRepresentation.PASSWORD ); credential.setValue( userDTO.getPassword() ); credential.setTemporary( false ); userRepresentation.setCredentials( Collections.singletonList( credential ) ); } ); / @JoinTable中。始终将现有实体用作反向@CollectionTable,而不是定义@OneToMany关联,以避免出现此问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。