使休眠忽略未映射的类变量 [英] Make hibernate ignore class variables that are not mapped
问题描述
我认为 hibernate 只考虑用 @Column
注释的类变量.但奇怪的是,今天当我添加一个变量(它没有映射到任何列,只是我在类中需要的一个变量)时,它试图将该变量作为列名包含在 select 语句中并抛出错误 -
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 -
字段列表"中的未知列team1_.agencyName"
Unknown column 'team1_.agencyName' in 'field list'
我的班级 -
@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;
}
为什么会这样?!
推荐答案
JPA 将使用该类的所有 属性,除非您专门用 @Transient
标记它们:
JPA will use all properties of the class, unless you specifically mark them with @Transient
:
@Transient
private String agencyName;
@Column
注释纯粹是可选的,它可以让您覆盖自动生成的列名.此外,@Column
的length
属性只在自动生成表定义时使用,对运行时没有影响.
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.
这篇关于使休眠忽略未映射的类变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!