Google数据存储区实体未返回ID。实体ID为空

问题描述 投票:0回答:2

我有一个从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为空。

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
2个回答
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;

}

0
投票

我在this quickstart之后设置了一个带有数据存储区的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属性已按预期填充

© www.soinside.com 2019 - 2024. All rights reserved.