0

Using Spring Boot I'm Having the following abbreviated structure of entities:

@Entity
@Table(name = "item")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Item implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", updatable = false)
    protected Long id;
...
}
@Entity
@Table(name = "book")
public class Book extends Item implements Serializable {
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "item_author", joinColumns = @JoinColumn(name = "item_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "author_id", referencedColumnName = "id"))
    private Set<Author> authors;
}
@Entity
@Table(name = "author")
public class Author implements Serializable {
    @Id
    @GeneratedValue
    private Long id;

    @ManyToMany(mappedBy="authors")
    private List<Book> books = new ArrayList<Book>();

    private String name;
}

My DAOs are just plain simple RestResource interfaces for all entities, like:

@RestResource(path="items", rel="items")
  public interface ItemDao extends CrudRepository<Item, Long> {
}

When I query an entity by id, it is all good

GET > http://localhost:8080/shelfventory/authors/1
{
  "name" : "Jhonny Cash",
  "used" : true,
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/shelfventory/authors/1"
    },
    "author" : {
      "href" : "http://localhost:8080/shelfventory/authors/1"
    },
    "books" : {
      "href" : "http://localhost:8080/shelfventory/authors/1/books"
    }
  }
}

But when I try to follow the links for a related object I just get an empty embedded:

GET > http://localhost:8080/shelfventory/authors/1/books
{
  "_embedded" : {
    "books" : [ ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/shelfventory/authors/1/books"
    }
  }
}

What am I doing wrong, how to solve it?

obeliksz
  • 468
  • 9
  • 24
  • Probably I got some sort of DB problem, as after changing the DB from mysql to h2, the problem went away, after clearing the mysql db, it works now just fine. If someone knows why this DB error did occur, please give it in the answer. – obeliksz Jan 22 '18 at 14:33

1 Answers1

0

Consider adding these two properties to your application.properties to keep your @Entity and schema in sync:

spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=true
DaShaun
  • 3,722
  • 2
  • 27
  • 29
  • like this could have helped to resolve my issue whithout dropping my tables and recreating it with Spring? Why do you think this ddl could help? – obeliksz Jan 22 '18 at 16:10
  • It can update your schema automatically. It might have prevented you from dropping your tables. I generally update my @Entity more often than the DB schema directly. With these two properties, my DB gets updated for me. – DaShaun Jan 22 '18 at 19:08
  • If I understand it correctly these ddl settings would change the schema if my entities change on the fly and makes sure that my db is according to the entity settings, changes it if I manually altered somewhat my schema etc. – obeliksz Jan 22 '18 at 19:39
  • Correct. With that said, probably not the best properties for your production environment. (But then again YOLO) – DaShaun Jan 22 '18 at 19:45