问题描述
我认为hibernate只考虑用 @Column
注释的类变量。但奇怪的是,当我添加一个变量(未映射到任何列,只是我需要的类中的变量)时,它试图将该变量包含在select语句中作为列名并引发错误 -
My class -
@Entity
@Table(name =team)
public class Team extends BaseObject implements Serializable {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(长度= 50)
私人字符串名称;
@Column(长度= 10)
私有字符串代码;
@Column(name =agency_id)
private long agencyId;
private String agencyName; //注意:没有注释。
仅供参考...我在另一类中使用上述类(
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ =user_team,
joinColumns = {@JoinColumn(name =user_id)},
inverseJoinColumns = @JoinColumn(name =team_id)
)
public Set<团队及GT; getTeams(){
返回团队;
}
为什么会发生这种情况?!
@Transient :
@Transient
private String agencyName;
@Column
注释纯粹是可选的,并在那里让您覆盖自动生成的列名称。此外, @Column
的长度
属性仅用于自动生成表定义时,它对运行时。
I thought hibernate takes into consideration only class variables that are annotated with @Column
. But strangely today when I added a variable (that is not mapped to any column, just a variable i need in the class), it is trying to include that variable in the select statement as a column name and throws the error -
My class -
@Entity
@Table(name="team")
public class Team extends BaseObject implements Serializable {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@Column(length=50)
private String name;
@Column(length=10)
private String code;
@Column(name = "agency_id")
private Long agencyId;
private String agencyName; //note: not annotated.
}
FYI...I use the above class in another class with many to many mapping
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name="user_team",
joinColumns = { @JoinColumn( name="user_id") },
inverseJoinColumns = @JoinColumn( name="team_id")
)
public Set<Team> getTeams() {
return teams;
}
Why is this happening?!
解决方案 JPA will use all properties of the class, unless you specifically mark them with @Transient
:
@Transient
private String agencyName;
The @Column
annotation is purely optional, and is there to let you override the auto-generated column name. Furthermore, the length
attribute of @Column
is only used when auto-generating table definitions, it has no effect on the runtime.
这篇关于让hibernate忽略未映射的类变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!