我正在寻找一种方法来设置Hibernate应用于Java对象中的变量名称的“默认”映射,以便根据数据库进行查询。目前我们使用内联javax.persistence标记来手动设置列名,但由于我们的数据库有一个严格的命名策略,所以能够跳过手动命名并让Hibernate进行映射会很不错。但是,目前除了本地非主要关键字段之外,这对于任何事情都不起作用。
目前,Hibernate似乎设置为将非外键映射到它们的名称(参见下面的示例类中的“foo”),将外键映射到“variableName_ReferencedTable_Id”(参见下面的“bar”)示例类)。我们希望非外键保持原样,除了标记为@id的变量,我们希望将其映射到“TableName_Id”,并且我们希望将外键映射到“variableName_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;
}
}
答案 0 :(得分:1)
(从评论中复制)
Hibernate确实有NamingStrategy的概念,但它对于对象是PK还是普通列并不敏感,所以它不会有任何用处。