如何使用Hibernate / JPA从另一个表中从另一个表中检索一列

发布于 2025-01-21 17:17:24 字数 1149 浏览 0 评论 0原文

我想使用外键“ model_id”仅检索一个列“ model_name”从tt_cars表中, 我尝试了以下代码,但它返回了整个汽车对象。

@JoinColumn(name = "MODEL_ID", referencedColumnName = "ID")
@ManyToOne(fetch = FetchType.EAGER)
private CARS cars;

另外,我尝试了下面的代码,它也无法正常工作

@SecondaryTable(name = "TT_CARS", pkJoinColumns = @PrimaryKeyJoinColumn(name = "ID", referencedColumnName="MODEL_ID"))

,还有其他方法可以使用Hibernate和JPA保留列(model_name)?

备注:模型名称应为选项类的一部分。

我的代码

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@Entity
@Table(name = "TT_OPTIONS")
public class Options  {

    @Id
    @Column(name = "ID")
    private String id;

    @NotNull
    @Column(name = "DESCRIPTION", nullable = false)
    private String description;

    @Column(name = "MODEL_ID") // Foreign key
    private Long modelId;

    @Column(name = "MODEL_NAME", table = "TT_CARS") // this is the column name I would like to retrieve from the TT_CARS table
    private String modelName;

    // getters and setters
}

I would like to use the Foreign key "MODEL_ID" to retrieve just one column "MODEL_NAME" from the TT_CARS table,
I tried the following code, that works but it returns the whole CARS object.

@JoinColumn(name = "MODEL_ID", referencedColumnName = "ID")
@ManyToOne(fetch = FetchType.EAGER)
private CARS cars;

Also I tried the code below, its also not working

@SecondaryTable(name = "TT_CARS", pkJoinColumns = @PrimaryKeyJoinColumn(name = "ID", referencedColumnName="MODEL_ID"))

Is there other way to retieve just the column (MODEL_NAME) using hibernate and JPA??

remarks: The modelName should be part of the Options class.

my code

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@Entity
@Table(name = "TT_OPTIONS")
public class Options  {

    @Id
    @Column(name = "ID")
    private String id;

    @NotNull
    @Column(name = "DESCRIPTION", nullable = false)
    private String description;

    @Column(name = "MODEL_ID") // Foreign key
    private Long modelId;

    @Column(name = "MODEL_NAME", table = "TT_CARS") // this is the column name I would like to retrieve from the TT_CARS table
    private String modelName;

    // getters and setters
}

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

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

发布评论

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

评论(1

单调的奢华 2025-01-28 17:17:24

您可以使用@noreflow noreferrer“>@formula 。它是仅读取的列,可以通过自定义子查询检索。它不存在于目标表中。

定义一个公式(派生值),该公式是一个SQL片段,充当
在大多数情况下,@column替代方案。代表只读状态。

示例:

@Entity
@Table(name = "TT_OPTIONS")
public class Options  {

    @Id
    @Column(name = "ID")
    private Long id;

    @Column(name = "DESCRIPTION", nullable = false)
    private String description;

    @Column(name = "MODEL_ID") 
    private Long modelId;

    @Formula("(select TT_CARS.MODEL_NAME from TT_CARS where TT_CARS.ID = MODEL_ID)")
    private String modelNameFormula;
}

@Entity
@Table(name = "TT_CARS")
public class Cars {
    @Id
    @Column(name = "ID")
    private Long id;

    @Column(name = "MODEL_NAME")
    private String modelName;
}

Hibernate生成的本机查询:

    select
        options0_.id as id1_4_0_,
        options0_.description as descript2_4_0_,
        options0_.model_id as model_id3_4_0_,
        (select
            TT_CARS.MODEL_NAME 
        from
            TT_CARS 
        where
            TT_CARS.ID = options0_.MODEL_ID) as formula1_0_ 
    from
        tt_options options0_ 
    where
        options0_.id=?

You can use @Formula. It is read-only calculated column that can be retrieved by the custom subquery. It does not present in the target table.

Defines a formula (derived value) which is a SQL fragment that acts as
a @Column alternative in most cases. Represents read-only state.

Example:

@Entity
@Table(name = "TT_OPTIONS")
public class Options  {

    @Id
    @Column(name = "ID")
    private Long id;

    @Column(name = "DESCRIPTION", nullable = false)
    private String description;

    @Column(name = "MODEL_ID") 
    private Long modelId;

    @Formula("(select TT_CARS.MODEL_NAME from TT_CARS where TT_CARS.ID = MODEL_ID)")
    private String modelNameFormula;
}

@Entity
@Table(name = "TT_CARS")
public class Cars {
    @Id
    @Column(name = "ID")
    private Long id;

    @Column(name = "MODEL_NAME")
    private String modelName;
}

Hibernate generated native query:

    select
        options0_.id as id1_4_0_,
        options0_.description as descript2_4_0_,
        options0_.model_id as model_id3_4_0_,
        (select
            TT_CARS.MODEL_NAME 
        from
            TT_CARS 
        where
            TT_CARS.ID = options0_.MODEL_ID) as formula1_0_ 
    from
        tt_options options0_ 
    where
        options0_.id=?

@SecondaryTable designed for @OneToOne relationship to map multiple tables to the same entity. It will not work for the @ManyToOne relationship.

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