【问题标题】:Explicitly changing the name of a column with Hibernate使用 Hibernate 显式更改列的名称
【发布时间】:2012-12-28 11:57:17
【问题描述】:

如何重命名生成 Hibernate 的表中的字段名称? 创建时:

@ManyToMany(targetEntity = GroupRightEntity.class)
or
@ManyToMany(targetEntity = UserRightEntity.class)

现在通过显式更改数据库中的列名来实现。

alter table security_mapping_user rename column sec_mapping_id to secmappingentity_id;

感谢您的帮助和理解。


Hibernate 为其他表生成链接。因此,我了解生成的字段名称。我需要使用注释或其他东西,这些列名会改变。我希望我写的一切都正确。


我们有。

@Entity
@Table(name = "ROLE")
public class RoleEntity implements Role, Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator = "SEQ_ROLE")
    @SequenceGenerator(name = "SEQ_ROLE", sequenceName="SEQ_ROLE", allocationSize = 1)
    private Long id;
    private String name;
    private String description;

@Entity
@Table(name = "URL")
public class UrlEntity implements Url, Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator = "SEQ_URL")
    @SequenceGenerator(name="SEQ_URL", sequenceName="SEQ_URL", allocationSize = 1)
    private Long id;
    private String url;
    @OneToMany(targetEntity = RoleEntity.class, fetch = FetchType.EAGER, mappedBy="url_id")
    private Set<Role> roles;

稍后我们得到第三张桌子。 “URL_ROLE”

在此表中,字段已命名。 “URL_ID”和“ROLE_ID”

“URL_ID”需要重命名“urlentity_id”中的字段。

现在看来我构建的一切都是正确的。

【问题讨论】:

    标签: java hibernate jpa hibernate-mapping


    【解决方案1】:

    在定义 ManyToMany 时,您还应该定义您的关系映射到哪一列。这就是您应该更改名称的地方。

    【讨论】:

    • Annotations @Column 看不到它没有。你举个例子?但是可以理解的是,需要更改Hibernate自动生成的表中某列的名称。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多