[英]How to get Datastore entity id from com.google.datastore.v1.Entity
[英]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.