0

I have a product entity which is extended from DataType Entity. Like this:

@

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;

}

And I have ProductDao to retrieve products from the database

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

    List<ProductEntity> findAll();

    List<ProductEntity> findByCode(String code);

And when I make the query. The ID is null.

Click here to see the screenshot of the query

My Google Cloud datastore entity is like this: Click here to see the screenshot of datastore entity

I would like to retrieve Key Product id:5748154649542656 from this entity. Please help. Thanks in advances

Win Han
  • 9
  • 1

2 Answers2

0

Using @Inheritance

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

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

new DataTypeEntity will be like this

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

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

}

or using @MappedSuperclass

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

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

}
  • Thanks for the answer. But Spring framework doesnt have those annotation as I am using google data cloud. I believe what you are mentioning is Hibernate inheritance annotation – Win Han May 15 '20 at 10:48
0

I set up a Spring Boot App with Datastore following this quickstart and I modified the Book class to inherit from your 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;

} 

When getting the books, the ID property was filled as expected

Jose V
  • 1,356
  • 1
  • 4
  • 12
  • Hey @Jose V. thanks for the answer. Still no luck for me. – Win Han May 19 '20 at 13:05
  • Are you able to see the ID field in the properties section of your entity in the console? For example: https://i.stack.imgur.com/4nEqw.png – Jose V May 19 '20 at 15:54
  • Hi @Jose V I do not have the id field in the properties. I want the Id from the Key Literal or Key. Is there a way to do that? – Win Han May 21 '20 at 09:05