0

我有一个从 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);

当我进行查询时。标识为空。

点击这里查看查询截图

我的谷歌云数据存储实体是这样的: 点击这里查看数据存储实体的截图

我想从该实体中检索 Key Product id:5748154649542656。请帮忙。提前感谢

4

2 回答 2

0

我按照这个快速入门设置了一个带有 Datastore 的 Spring Boot 应用程序,并修改了 Book 类以从您的DataTypeEntity.

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

于 2020-05-18T08:38:08.957 回答
0

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

}
于 2020-05-13T20:47:55.990 回答