Hibernate Annotation,如何使用@Joincolumn引用外键?
我使用 Hibernate 3.6.8。这是我的表:
CREATE TABLE UTILIZATION (
ID BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY
...
FK_WORKCATEGORY SMALLINT,
CONSTRAINT PK_UTILIZATION PRIMARY KEY ( ID));
CREATE TABLE DB2ADMIN.WORKCATEGORY(
ID SMALLINT NOT NULL,
DESCRIPTION VARCHAR(50),
CONSTRAINT PK_WORKCATEGORY PRIMARY KEY(ID));
ALTER TABLE UTILIZATION ADD FOREIGN KEY (FK_WORKCATEGORY) REFERENCES WORKCATEGORY(ID);
我的 Pojos:
@Entity
@Proxy(proxyClass=IWorkCategory.class)
@Table(name="WORKCATEGORY")
public class WorkCategory extends BoBase implements Serializable, IWorkCategory{
@Id
private Integer Id;
private String description;
@Override
public Serializable getId() {
return Id;
}
@Override
public void setId(Serializable id) {
Id = (Integer)id;
}
@Override
public String getDescription() {
return description;
}
@Override
public void setDescription(String description) {
this.description = description;
}
}
@Entity
@Proxy(proxyClass=IUtilization.class)
@Table(name="UTILIZATION")
public class Utilization extends BoBase implements Serializable, IUtilization{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long Id;
private WorkCategory workCategory;
@ManyToOne(fetch=FetchType.LAZY, targetEntity=WorkCategory.class)
@JoinColumn(name="FK_WORKCATEGORY", updatable=false, insertable=false)
public WorkCategory getWorkCategory() {
return workCategory;
}
public void setWorkCategory(WorkCategory workCategory) {
this.workCategory = workCategory;
}
}
如您所见,表 Utilization 通过外键 FK_WORKCATEGORY
引用表 WorkCategory
,但在 Utilization
pojo 中,我使用名称 workCategory
声明此属性。 当我对 Utilization bo = UtilizationDAO.get(new Long(123))
运行单元测试时,会使用 UTILIZATIO0_.WORKCATEGORY 列生成 SQL,而不是 FK_WORKCATEGORY
。 我已将 @JoinColumn(name="FK_WORKCATEGORY") 放入 Utilization
Pojo 中,但没有帮助。 请帮我修复这个错误。提前致谢。
I use Hibernate 3.6.8. Here is my tables:
CREATE TABLE UTILIZATION (
ID BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY
...
FK_WORKCATEGORY SMALLINT,
CONSTRAINT PK_UTILIZATION PRIMARY KEY ( ID));
CREATE TABLE DB2ADMIN.WORKCATEGORY(
ID SMALLINT NOT NULL,
DESCRIPTION VARCHAR(50),
CONSTRAINT PK_WORKCATEGORY PRIMARY KEY(ID));
ALTER TABLE UTILIZATION ADD FOREIGN KEY (FK_WORKCATEGORY) REFERENCES WORKCATEGORY(ID);
my Pojos:
@Entity
@Proxy(proxyClass=IWorkCategory.class)
@Table(name="WORKCATEGORY")
public class WorkCategory extends BoBase implements Serializable, IWorkCategory{
@Id
private Integer Id;
private String description;
@Override
public Serializable getId() {
return Id;
}
@Override
public void setId(Serializable id) {
Id = (Integer)id;
}
@Override
public String getDescription() {
return description;
}
@Override
public void setDescription(String description) {
this.description = description;
}
}
@Entity
@Proxy(proxyClass=IUtilization.class)
@Table(name="UTILIZATION")
public class Utilization extends BoBase implements Serializable, IUtilization{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long Id;
private WorkCategory workCategory;
@ManyToOne(fetch=FetchType.LAZY, targetEntity=WorkCategory.class)
@JoinColumn(name="FK_WORKCATEGORY", updatable=false, insertable=false)
public WorkCategory getWorkCategory() {
return workCategory;
}
public void setWorkCategory(WorkCategory workCategory) {
this.workCategory = workCategory;
}
}
As you can see the table Utilization refer to table WorkCategory
by foreign key FK_WORKCATEGORY
, but in Utilization
pojo, i declare this property with the name workCategory
.
When I run unit test for Utilization bo = UtilizationDAO.get(new Long(123))
, the SQL is generated with the column UTILIZATIO0_.WORKCATEGORY, instead of FK_WORKCATEGORY
.
I have put the @JoinColumn(name="FK_WORKCATEGORY", in the Utilization
Pojo, but it doesn't help.
Please help me to fix this error. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您将注释放在 getter 上,而不是放在字段上。由于
@Id
注释被放置在字段上,Hibernate期望所有注释都在字段上。That's because you put your annotation on the getter rather than putting in on the field. Since the
@Id
annotation is put on a field, Hibernate expects all the annotations to be on fields.