如何添加针对旧数据库的 JPA 关系

发布于 2024-11-27 06:48:34 字数 1433 浏览 1 评论 0原文

我有 C# 实体框架背景,正在研究 Java 项目中的 JPA,因此我希望我面临的只是一个概念问题。

我有一个旧数据库,无法更改其架构,并且需要编写 DAL。

我生成了(针对示例进行了简化)以下实体...

@Entity
@Table(name = "crag", catalog = "rad_dbo")
public class CragEntity {
    private int id;

    @Column(name = "id")
    @Id
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    private int fkSubRegionId;

    @Column(name = "fk_subRegionId")
    @Basic
    public int getFkSubRegionId() {
        return fkSubRegionId;
    }

    public void setFkSubRegionId(int fkSubRegionId) {
        this.fkSubRegionId = fkSubRegionId;
    }
}

并且

@Table(name = "subRegion", catalog = "rad_dbo")
@Entity
public class SubRegionEntity {
    private int id;

    @Column(name = "id")
    @Id
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

我尝试添加与 CragEntity 的关系,以便我可以访问其子区域,

@ManyToOne
@JoinColumn(name="fk_SubRegionId",nullable=false)
private SubRegionEntity subRegion;

但是当我尝试运行时

select c from CragEntity c where c.subRegion.region = :area

出现异常

java.lang.RuntimeException:org.hibernate.QueryException:可以

无法解析属性:子区域:uk.co.bmc.rad.dal.CragEntity

希望这是可能的,我很慢...

提前非常感谢您的帮助!

I'm coming from a C# entity framework background and looking at JPA in a Java project so I'm hoping that what I'm facing is just a conceptual problem.

I've got a legacy database that I can't alter the schema of and I need to write a DAL.

I've generated (simplified for the example) the following entities...

@Entity
@Table(name = "crag", catalog = "rad_dbo")
public class CragEntity {
    private int id;

    @Column(name = "id")
    @Id
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    private int fkSubRegionId;

    @Column(name = "fk_subRegionId")
    @Basic
    public int getFkSubRegionId() {
        return fkSubRegionId;
    }

    public void setFkSubRegionId(int fkSubRegionId) {
        this.fkSubRegionId = fkSubRegionId;
    }
}

and

@Table(name = "subRegion", catalog = "rad_dbo")
@Entity
public class SubRegionEntity {
    private int id;

    @Column(name = "id")
    @Id
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

I've tried adding a relationship to CragEntity so that I can access its subRegion

@ManyToOne
@JoinColumn(name="fk_SubRegionId",nullable=false)
private SubRegionEntity subRegion;

but when I try to run

select c from CragEntity c where c.subRegion.region = :area

I get an exception

java.lang.RuntimeException: org.hibernate.QueryException: could

not resolve property: subRegion of: uk.co.bmc.rad.dal.CragEntity

Hopefully this is possible and I'm being slow...

Many thanks in advance for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

余厌 2024-12-04 06:48:34

在您的查询中,您正在搜索属性“subRegion”,尽管在实体定义中您的名称为“fkSubRegionId”,因此您必须更改变量名称或查询。 ;)

编辑:抱歉我误读了关系。

您可以使用代码内的关系访问该属性(无需进行 HQL 查询)吗?

除非您只想在查询中选择某些字段,否则我会推荐如下查询:

from CragEntity c where c.subRegion.region='theRegion'

In your query you are searching for the property "subRegion" though in your entity definition you have the name "fkSubRegionId", so you must change the var name or the query. ;)

EDIT: Sorry i misreaded the relation.

Can you access the property (without making an HQL query) with the relationship inside the code?

Unless, you want to pick only certain fields in your query I would recommend a query like:

from CragEntity c where c.subRegion.region='theRegion'
原谅我要高飞 2024-12-04 06:48:34

事实证明,存在几个问题 - 一个是概念性的,一个是 IntelliJ 如何生成我正在复制的关系,另一个是椅子和键盘之间的关系......

IntelliJ 在“错误”的一端选择了与所有者的区域到子区域的关系 -可能是架构问题而不是 IntelliJ 的错误。一旦我意识到这一点并找出解决办法,我可以将其复制到 CragEntity 和 SubRegionEntity

在 CragEntity 中我添加了:

private SubRegionEntity subRegion;

        @ManyToOne
        @JoinColumn(name="fk_SubRegionId",nullable=false)
        public SubRegionEntity getSubRegion() {
            return subRegion;
        }

        public void setSubRegion(SubRegionEntity subRegion) {
            this.subRegion = subRegion;
        }

然后在 SubRegionEntity 中我添加了:

private List<CragEntity> crags;

    @OneToMany(mappedBy = "subRegion")
    List<CragEntity> getCrags() {
        return crags;
    }

    public void setCrags(List<CragEntity> crags) {
        this.crags = crags;
    }

另外,似乎任何将成为关系一端的实体类都必须实现可序列化(我猜实体被序列化为所有者。因此需要添加到 SubRegionEntity 和 RegionEntity 上。

我的愚蠢之处当然是查询应该是c.subRegion.region.name 否则我将 RegionEntity 类型的对象与字符串进行比较... doh - 我

是 TDD 新手,但一如既往 。我为我认为应该在现有代码中发生的情况编写了测试,我走过了我的错误(并通过异常和错误给出了谷歌关键字:-))

It turns out there were several issues - one conceptual, one with how IntelliJ had generated a relationship I was copying and one between the chair and keyboard...

IntelliJ had picked the region to subregion relationship with the owner at the "wrong" end - probably a schema issue rather than IntelliJ's fault. Once I realised that and figured out the fix I could copy that to CragEntity and SubRegionEntity

In CragEntity I added:

private SubRegionEntity subRegion;

        @ManyToOne
        @JoinColumn(name="fk_SubRegionId",nullable=false)
        public SubRegionEntity getSubRegion() {
            return subRegion;
        }

        public void setSubRegion(SubRegionEntity subRegion) {
            this.subRegion = subRegion;
        }

and then in SubRegionEntity I added:

private List<CragEntity> crags;

    @OneToMany(mappedBy = "subRegion")
    List<CragEntity> getCrags() {
        return crags;
    }

    public void setCrags(List<CragEntity> crags) {
        this.crags = crags;
    }

Also, it seem that any entity class that is going to be one end of a relationship has to implement serializable (I guess the entities get serialized into the owner. So that needed adding onto SubRegionEntity and RegionEntity

The silliness on my part was of course that the query should have been c.subRegion.region.name otherwise I was comparing an object of type RegionEntity with a string... doh - very stupid mistake on my part.

I'm new to TDD but as always as soon as I wrote tests for what I thought should be happening with the existing code I was walked through my errors (and given google keywords by the exceptions and errors :-))

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