Hibernate Annotation,如何使用@Joincolumn引用外键?

发布于 2024-12-19 11:18:13 字数 2166 浏览 0 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过期情话 2024-12-26 11:18:13

这是因为您将注释放在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文