【问题标题】:Google 数据存储区实体不返回 ID。实体 ID 为空
【发布时间】:2020-08-29 21:50:43
【问题描述】:

我有一个从 DataType 实体扩展而来的产品实体。像这样:

@

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 为空。

Click here to see the screenshot of the query

我的 Google Cloud 数据存储实体如下所示: Click here to see the screenshot of datastore entity

我想找回钥匙 此实体的产品 ID:5748154649542656。请帮忙。 提前致谢

【问题讨论】:

    标签: java spring-boot google-cloud-datastore spring-cloud-gcp


    【解决方案1】:

    使用@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;
    
    }
    

    【讨论】:

    • 感谢您的回答。但是 Spring 框架没有这些注释,因为我使用的是谷歌数据云。我相信你提到的是Hibernate继承注释
    【解决方案2】:

    我在 this quickstart 之后设置了一个带有 Datastore 的 Spring Boot 应用程序,并且我修改了 Book 类以从您的 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 +
                                '}';
        }
    } 
    

    DataTypeEntity.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属性按预期填写

    【讨论】:

    • 嘿@Jose V. 感谢您的回答。我仍然没有运气。
    • 您能否在控制台的实体属性部分看到 ID 字段?例如:i.stack.imgur.com/4nEqw.png
    • 嗨@Jose V 我在属性中没有id 字段。我想要来自 Key Literal 或 Key 的 Id。有没有办法做到这一点?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-23
    • 2017-11-06
    • 1970-01-01
    相关资源
    最近更新 更多