hibernate 休眠:在.fk_id = www.example.com上连接表b.id

kninwzqo  于 2022-11-24  发布在  其他
关注(0)|答案(2)|浏览(141)

我在一个小项目涉及游戏Re-Volt(1999),我试图使一个网页,你可以看到所有的汽车与他们的细节(图像,引擎类型,评级,速度等)。我有一个表cars,我前面提到的信息,它看起来像这样:

CREATE TABLE public.cars (
    id integer NOT NULL,
    name character varying(25),
    thumbnail_id integer,
    engine_id integer,
    rating_id integer,
    speed integer,
    acc numeric,
    mass numeric
);

我正在使用Hibernate和SpringBoot,PostgreSQL用于数据库,Thymeleaf用于在Web页面中显示数据。但现在我想将carscars.thumbnail_id = thumbnails.id上的表thumbnails连接起来,并显示表thumbnails中的列image,而不是thumbnails_id。下面是我的thumbnails表:

CREATE TABLE public.thumbnails (
    id integer NOT NULL,
    image character varying(50)
);

这些是我的实体:
第一次
我不知道如何正确地编写join。我目前的代码Hibernate在匹配行时不考虑thumbnail_id的值,它只是执行“car_1 = thumbnail_1”,即使“car_1”的“thumbnail_id”等于12,它仍然匹配第一个缩略图,而不是第12个。有人能帮我吗?
编辑:基本上,我试图通过Hibernate实现的是下面的SQL查询:

SELECT c.name, t.image, c.engine_id, c.rating_id, c.speed, c.acc, c.mass
FROM cars c
JOIN thumbnails t
ON c.thumbnails_id = t.id;
wvmv3b1j

wvmv3b1j1#

汽车实体

@Entity
@Table(name = "cars")
public class Car {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;
    @Column(name = "name")
    private String name;

    @JoinColumn(name = "thumbnail_id", referencedColumnName="id")
    @OneToOne(cascade = CascadeType.ALL)
    @Fetch(FetchMode.JOIN)
    private Thumbnail thumbnail;

    @Column(name = "engine_id")
    private Integer engine_id;

    @Column(name = "rating_id")
    private Integer rating_id;

    @Column(name = "speed")
    private Integer speed;
    @Column(name = "acc")
    private Double acc;
    @Column(name = "mass")
    private Double mass;

缩略图实体

@Entity
@Table(name = "thumbnails")
public class Thumbnail {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;
    @Column(name = "image")
    private String image;

由于您希望实现通过连接进行选择,并且在一对一关联N+1问题不允许您这样做的情况下,您需要按如下方式修改存储库以添加EntityGraph

import java.util.List;

import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;

import com.mahesh.tt.model.Car;

public interface TutorialRepository2 extends JpaRepository<Car, Long> {
    
     @Override
        @EntityGraph(attributePaths = {"thumbnail"})
        List<Car> findAll();
}

这将确保您的缩略图数据是通过join而不是通过多个select语句获取的。
1.您可以在控制台中看到生成的查询
休眠模式:

select
            car0_.id as id1_0_0_,
            thumbnail1_.id as id1_1_1_,
            car0_.acc as acc2_0_0_,
            car0_.engine_id as engine_i3_0_0_,
            car0_.mass as mass4_0_0_,
            car0_.name as name5_0_0_,
            car0_.rating_id as rating_i6_0_0_,
            car0_.speed as speed7_0_0_,
            car0_.thumbnail_id as thumbnai8_0_0_,
            thumbnail1_.image as image2_1_1_ 
        from
            cars car0_ 
        left outer join
            thumbnails thumbnail1_ 
                on car0_.thumbnail_id=thumbnail1_.id

1.您可以在API响应中看到结果。

1.您可以在数据库中看到示例数据
汽车-

缩图-

kx7yvsdv

kx7yvsdv2#

多亏了你的注解,我才能让它正常工作。我所做的就是在@JoinColumn注解中提到thumbnail_id(多亏了@blau注解),并添加@MapsId("id")(多亏了@Mahesh注解)。这就是我在Car.java类中所做的:

// Car.java
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "cars")
public class Car {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;
    @Column(name = "name")
    private String name;

    @OneToOne
    @JoinColumn(name = "thumbnail_id") // I changed this
    @MapsId("id") // and added this
    private Thumbnail thumbnail;

    @Column(name = "engine_id")
    private Integer engine;

    @Column(name = "rating_id")
    private Integer rating;

    @Column(name = "speed")
    private Integer speed;
    @Column(name = "acc")
    private Double acc;
    @Column(name = "mass")
    private Double mass;
}

相关问题