簡體   English   中英

Google 數據存儲區實體不返回 ID。 實體 ID 為 null

[英]Google Datastore Entity does not return ID. Entity ID is null

我有一個從 DataType Entity 擴展而來的產品實體。 像這樣:

@

Entity(name = "Product")
@Getter
@Setter
public class ProductEntity extends DataTypeEntity{

        public Date created;
        String name;
        String url;
        String code;
        String description;
        String fixedCost;
}

@Getter
@Setter
@NoArgsConstructor
public class DataTypeEntity {

    @Id
    public Long id;
    public Date created;
    public Date lastUpdated;

}

我有 ProductDao 從數據庫中檢索產品

@Repository
public interface ProductDao extends DatastoreRepository<ProductEntity, Long> {

    List<ProductEntity> findAll();

    List<ProductEntity> findByCode(String code);

當我進行查詢時。 ID 是 null。

點擊這里查看查詢截圖

我的谷歌雲數據存儲實體是這樣的:點擊這里查看數據存儲實體的截圖

我想從該實體中檢索 Key Product id:5748154649542656。 請幫忙。 提前感謝

使用@Inheritance

Entity(name = "Product")
@Getter
@Setter
public class ProductEntity extends DataTypeEntity{

        public Date created;
        String name;
        String url;
        String code;
        String description;
        String fixedCost;
}

新的DataTypeEntity將是這樣的

@Getter
@Setter
@NoArgsConstructor
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public class DataTypeEntity {

    @Id
    public Long id;
    public Date created;
    public Date lastUpdated;

}

或使用@MappedSuperclass

@Getter
@Setter
@NoArgsConstructor
@MappedSuperclass  
public class DataTypeEntity {

    @Id
    public Long id;
    public Date created;
    public Date lastUpdated;

}

此快速入門之后,我使用 Datastore 設置了 Spring 引導應用程序,並修改了 Book class 以從您的DataTypeEntity繼承。

Book.java:

package com.example.demo;

import com.DataTypeEntity;
import org.springframework.cloud.gcp.data.datastore.core.mapping.Entity;

@Entity(name = "books")
public class Book extends DataTypeEntity{
    String title;

    String author;

    int year;

    public Book(String title, String author, int year) {
            this.title = title;
            this.author = author;
            this.year = year;
    }

    public long getId() {
            return this.id;
    }

    @Override
    public String toString() {
            return "Book{" +
                            "id=" + this.id +
                            ", created='" + this.created + '\'' +
                            ", lastUpdated='" + this.lastUpdated + '\'' +
                            ", title='" + this.title + '\'' +
                            ", author='" + this.author + '\'' +
                            ", year=" + this.year +
                            '}';
    }
} 

數據類型實體.java:

package com;

import com.google.cloud.Date;
import org.springframework.data.annotation.Id;

public class DataTypeEntity {

    @Id
    public Long id;
    public Date created;
    public Date lastUpdated;

} 

取書時,ID屬性按預期填寫

暫無
暫無

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

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