是否可以设置 Hibernate 的“默认”变量名映射?

发布于 2024-09-08 14:53:53 字数 1872 浏览 5 评论 0 原文

我正在寻找一种方法来设置 Hibernate 应用于 Java 对象中的变量名称的“默认”映射,以根据数据库对其进行查询。目前,我们正在使用内联 javax.persistence 标记来手动设置列名称,但由于我们对数据库有严格的命名策略,因此如果能够跳过手动命名并让 Hibernate 进行映射,那就太好了。然而,目前这对于除本地非主键字段之外的任何内容都不起作用。

目前,Hibernate 似乎设置为将非外键映射到其名称(请参阅下面示例类中的“foo”),并将外键映射到“variableName_ReferencedTable_Id”(请参阅​​下面示例类中的“bar”) 。我们希望非外键保持原样,除了标记为 @id 的变量,我们希望将其映射到“TableName_Id”,并且我们希望将外键映射到“变量名_Id”。这是否可能,或者我们只能忍受手动映射?

package testPackage.test

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Table1 {

    private int id;
    private int localVariable;
    private int foreignKeyVariable;

    // Constructor here somewhere

    // Without a @Column( name="Table1_Id" ), this comes out as "id".
    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO)
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    // This works fine, being a local field.

    public int getLocalVariable() {
        return localVariable;
    }

    public void setLocalVariable(int LocalVariable) {
        this.localVariable = localVariable;
    }

    // Withou a @JoinColumn( name="foreignKeyVariable_Id" ) , this comes out as "foreignKeyVariable_Table2_Id".

    @ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
    @JoinColumn() // Not sure if this would even be necessary at all. Happy to leave it out if possible.
    public int getForeignKeyVariable() {
        return foreignKeyVariable;
    }

    public void setForeignKeyVariable(int foreignKeyVariable) {
        this.foreignKeyVariable = foreignKeyVariable;
    }

}

I'm looking for a way to set the "default" mapping that Hibernate applies to a variable name in a Java object to query it against the database. At the moment we are using the inline javax.persistence markup to manually set column names, but since we have a rigid naming policy for our database it would be nice to be able to just skip on the manual naming and let Hibernate do the mapping. However, at the moment this doesnt work nice at all with anything save for local, non-primary key fields.

At the moment, Hibernate seems to be set to map non-foreign keys to just their name (see "foo" in the below example class), and foreign-keys to "variableName_ReferencedTable_Id" (see "bar" in the below example class). We would like non-foreign keys to stay as they are, except for the variable marked @id, which we would like to be mapped to "TableName_Id", and we would like foreign keys to be mapped to "variableName_Id". Is this even possible, or do we just have to put up with manual mapping?

package testPackage.test

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Table1 {

    private int id;
    private int localVariable;
    private int foreignKeyVariable;

    // Constructor here somewhere

    // Without a @Column( name="Table1_Id" ), this comes out as "id".
    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO)
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    // This works fine, being a local field.

    public int getLocalVariable() {
        return localVariable;
    }

    public void setLocalVariable(int LocalVariable) {
        this.localVariable = localVariable;
    }

    // Withou a @JoinColumn( name="foreignKeyVariable_Id" ) , this comes out as "foreignKeyVariable_Table2_Id".

    @ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
    @JoinColumn() // Not sure if this would even be necessary at all. Happy to leave it out if possible.
    public int getForeignKeyVariable() {
        return foreignKeyVariable;
    }

    public void setForeignKeyVariable(int foreignKeyVariable) {
        this.foreignKeyVariable = foreignKeyVariable;
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哆啦不做梦 2024-09-15 14:53:53

(从评论复制)

Hibernate确实有NamingStrategy的概念,但它对对象是PK还是普通列不敏感,所以这没有任何用处。

(copied from comment)

Hibernate does have the concept of NamingStrategy, but it's not sensitive to whether than object is a PK or a normal column, so that's not going to be of any use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文