主表有两个字段映射到副表,可以取得唯一记录,但这条记录不定存在,这两个字段需要做条件过滤,就这个状况。
Hibernate Annotation 配置的注意点:
- 需要映射字段的@Column属性设为insertable = false, updatable = false,
- 不产生列重复的异常
- 在hql解析的时候,自动合并相同的列(参考)
- 类必须implements Serializable,不然会抛ClassCaseException(郁闷4个小时 -_-!)
HQL
select
s
from
ScopeProjection s
left
join fetch s.overridewhere s.hubsite=? ands.companyabbr=?
CODE
@Entity
@SequenceGenerator(name
=
"
SEQ_GEN
"
, sequenceName
=
"
SCOPEPROJECTIONSEQ
"
)
public
class
ScopeProjection
implements
Serializable {
private
Integer id;
private
String hubsite;
private
String companyabbr;
private
ScopeProjectionOverride override;
@Id
@Column(name
=
"
PROJECTIONID
"
)
@GeneratedValue(strategy
=
GenerationType.AUTO, generator
=
"
SEQ_GEN
"
)
public
Integer getId() {
return
id;
}
public
void
setId(Integer id) {
this
.id
=
id;
}
@Column(name
=
"
HUBSITENAME
"
, nullable
=
false
, insertable
=
false
, updatable
=
false
)
public
String getHubsite() {
return
hubsite;
}
public
void
setHubsite(String hubsite) {
this
.hubsite
=
hubsite;
}
@Column(name
=
"
COMPANYABBREVIATION
"
, nullable
=
false
, insertable
=
false
, updatable
=
false
)
public
String getCompanyabbr() {
return
companyabbr;
}
public
void
setCompanyabbr(String companyabbr) {
this
.companyabbr
=
companyabbr;
}
@OneToOne(cascade
=
CascadeType.PERSIST)
@JoinColumns ({
@JoinColumn(name
=
"
COMPANYABBREVIATION
"
, referencedColumnName
=
"
COMPANYABBREVIATION
"
),
@JoinColumn(name
=
"
HUBSITENAME
"
, referencedColumnName
=
"
HUBSITENAME
"
)
})
public
ScopeProjectionOverride getOverride() {
return
override;
}
public
void
setOverride(ScopeProjectionOverride override) {
this
.override
=
override;
}
}
@Entity
@SequenceGenerator(name
=
"
SEQ_GEN
"
, sequenceName
=
"
SCOPEOVERRIDESEQ
"
)
public
class
ScopeProjectionOverride
implements
Serializable {
private
Integer id;
private
String companyabbr;
private
String hubsitename;
@Id
@Column(name
=
"
OVERRIDEID
"
)
@GeneratedValue(strategy
=
GenerationType.AUTO, generator
=
"
SEQ_GEN
"
)
public
Integer getId() {
return
id;
}
public
void
setId(Integer id) {
this
.id
=
id;
}
@Column(name
=
"
COMPANYABBREVIATION
"
)
public
String getCompanyabbr() {
return
companyabbr;
}
public
void
setCompanyabbr(String companyabbr) {
this
.companyabbr
=
companyabbr;
}
public
String getHubsitename() {
return
hubsitename;
}
public
void
setHubsitename(String hubsitename) {
this
.hubsitename
=
hubsitename;
}
}
posted on 2007-02-01 23:55
阿姆斯壮 阅读(1405)
评论(0) 编辑 收藏 所属分类:
基础很重要