簡體   English   中英

Spring-JPA。多對多額外列

[英]Many-To-Many extra columns Spring JPA

已經過了一個星期,我正在努力解決問題,似乎我無法找到任何答案。

我有這個結構: 數據庫結構

Album模型:

@Entity
@Table(name = DatabaseConstants.ALBUM_TABLE_NAME)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Album {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false, length = 100)
    private String name;

    @Column(nullable = false)
    private int imageVersion = 1;

    @Column(length = 255)
    private String description;

    @Column(nullable = false)
    private boolean single = false;

    @Column(nullable = false)
    private Long createdAt;

    @Column(nullable = true)
    private Long deletedAt;

    // Relations

    @OneToMany(fetch = FetchType.LAZY)
    private List<AlbumView> albumViews;

    // Getters and Setters
}

AlbumView模型:

@Entity
@Table(name = DatabaseConstants.ALBUM_VIEW_RELATION_TABLE_NAME)
public class AlbumView {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private boolean bigger;

    @Column(nullable = false)
    private int position;

    @Column(nullable = false)
    private Long createdAt;

    @Column(nullable = true)
    private Long deletedAt;

    // Relations

    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "album_id")
    private Album album;

    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "view_id")
    private View view;

    // Getters and Setters    
}

View型號:

@Entity
@Table(name = DatabaseConstants.VIEW_TABLE_NAME)
public class View {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private String name;

    @Column(nullable = false)
    private Long createdAt;

    @Column(nullable = true)
    private Long deletedAt;

    // Relations

    @OneToMany(fetch = FetchType.LAZY)
    private List<AlbumView> albumViewList;

    // Getters and Setters
}

我需要通過視圖搜索專輯列表。 我需要的查詢是這樣的(使用Spring JPA的@Query注釋):

SELECT a, av.bigger, av.position FROM Album a, AlbumView av WHERE av.view.id = ?    

但我無法映射這兩個值(更大,位置),因為它們不在Album模型上。 我需要建立一個響應,如:

[
    {
        id: 1,
        name: 'Test Album',
        imageVersion: 1,
        description: 'Description one',
        single: true,
        bigger: true,
        position: 1
    },
    {
        id: 2,
        name: 'Test Album 2',
        imageVersion: 1,
        description: 'Description two',
        single: true,
        bigger: false,
        position: 2
    }
]

據我所知,我不能在這里使用@Transient來幫助我(因為顯然JPA忽略它並且@Query不填充這些屬性)並且我不知道其他方法。 謝謝。

編輯1

我在Spring JPA的Repository類中使用以下代碼嘗試了@bonifacio建議:

@Query("SELECT av.album.id, av.album.name, av.album.imageVersion, av.bigger, av.position FROM AlbumView av WHERE av.view.id = ?1")
List<AlbumView> findByViewId(Long id);

但我收到了以下回復:

[
    [
        1,
        "Test Best Hits",
        1,
        true,
        1
    ]
]

值正是如此,但它正在考慮這是一個數組,而不是像它所假設的對象..

一種方法是更改​​查詢以選擇AlbumView實體,該實體包含AlbumView和Album實體中的所有所需字段,還可以在查詢的WHERE子句中使用View實體。

在這種情況下,可能的解決方案之一將使用以下查詢:

SELECT av FROM AlbumView av WHERE av.view.id = ?1

此外,我想要注意的是,在您的第一個示例中,您嘗試每行獲取三個不同的對象,如下面的示例:

SELECT a, av.bigger, av.position FROM Album a, AlbumView av WHERE av.view.id = ? 

它不起作用的原因是因為當你選擇多個列(或對象)時,你不會自動只在一行中獲取所需的字段,因為Hibernate會將結果轉換為Object數組(Object [])查詢結果的每一行。 因此,除非您確實需要在一行中選擇多個對象,否則始終建議在每個SELECT上只返回一個字段或實體。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM