Posting an entity with manyToMany, ManyToOne - rest

I've always avoided #xToX relationships in my APIs but finally wanted to give it a go, and I'm struggling with it.
I've managed to make the get methods returning me the right informations with any recursiveness (at least I made a part), and now I want to post data to my API with those relationships in place (and seems working so far).
So I have a few objects a book, a book style, a book category and an author.
A book has only one author (even if it's false sometimes but as I only read Sci-Fi it will do the trick) and an author have many books (same for category).
And a style can have many books, also books can have many styles.
So I went with a #ManyToOne on the book's author property and with #OneToMany on the author's books property (same for category).
And between the style and the bok #ManyToMany
I want to be able to post a book to my API like follows, using the author and style ids :
{
"isbn": "15867jhg8",
"title": "Le portrait de Dorian Gray",
"author": 1,
"category":1,
"style": [1,2,3]
}
I've annotated my properties with #JsonBackReference, #JsonManagedReference, #JsonSerialize, #JsonIdentityInfo but I truly think I might have made it too much ...
I'm not sure about the way I used the #JsonSerialize, nor the #JsonIdentityInfo.
I've omitted the useless properties to keep the example 'simple'.
So let's dive in.
The Book class first :
#Entity
#JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id"
)
public class Book implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(updatable = false, nullable = false)
private Integer id;
private String isbn;
private String title;
#ManyToOne
#JoinColumn(name="category_id", nullable = false)
#JsonBackReference(value = "category")
private BookCategory category;
#ManyToOne
#JoinColumn(name="author_id", nullable = false)
#JsonBackReference(value = "author")
private Author author;
#ManyToMany
#JsonManagedReference(value = "styles")
#JsonSerialize(using = BookStyleListSerializer.class)
private List <BookStyle> styles;
}
Then the author class :
#Entity
#JsonIdentityInfo (
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id"
)
public class Author implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(updatable = false, nullable = false)
private Integer id;
#OneToMany(mappedBy = "author")
#JsonManagedReference(value = "authorBooks")
#JsonSerialize(using = BookListSerializer.class)
private List <Book> books;
}
As the category is quite the same, I'm only pasting the books property of it :
#OneToMany(mappedBy = "category")
#JsonManagedReference(value = "categoryBooks")
#JsonSerialize(using = BookListSerializer.class)
private List <Book> books;
And finally my bookStyle class :
#Entity
#JsonIdentityInfo(
generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id"
)
public class BookStyle implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(updatable = false, nullable = false)
private Integer id;
#ManyToMany
#JsonManagedReference(value = "styleBooks")
#JsonSerialize(using = BookListSerializer.class)
private List <Book> books;
}
The serializer are the same, it's only the type that changes :
public class BookStyleListSerializer extends StdSerializer <List <BookStyle>> {
public BookStyleListSerializer() {
this(null);
}
public BookStyleListSerializer(Class<List<BookStyle>> t) {
super(t);
}
#Override
public void serialize(List <BookStyle> bookStyles, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
List<Integer> ids = new ArrayList <> ( );
for (BookStyle style: bookStyles){
ids.add(style.getId ());
}
jsonGenerator.writeObject ( ids );
}
}
Of what I understood (and as english is not my native language i might misunderstood a few things here and there while coding) :
#JsonBackReference is to be used for the property we don't want to be serialized as opposite to #JsonManagedReference
#JsonSerialize is the custom serializer so that the elements will be serialized as we want them to (in this case, only using IDs)
And as it might be obvious to some of you : none of what I've coded works for posting data, here's the exception as i received it when i post something via API (and it's doubled, not a copy paste error) :
.c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class com.rz.librarian.domain.entity.Author]]: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot handle managed/back reference 'categoryBooks': no back reference property found from type [collection type; class java.util.List, contains [simple type, class com.rz.librarian.domain.entity.Book]]
.c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class com.rz.librarian.domain.entity.Author]]: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot handle managed/back reference 'categoryBooks': no back reference property found from type [collection type; class java.util.List, contains [simple type, class com.rz.librarian.domain.entity.Book]]
I've tried many things but after three days on this issue i wanted to ask (cry?) for help.
Thank you guys and sorry for the long post!

Related

Does hibernate search - 6.0 version support projection on embedded entities?

I am developing a search api where I need to return in response the resource with only the fields/properties asked in request. The fields can be of sub elements as well. E.g - book.author.name where book is the parent resource and author a sub resource under it, may be with a many to one relationship.
I have learned in earlier versions of hibernate (5.x.x) projections is not supported on embedded entities.
So wanted to know if this feature is added in 6.0
When there is a single author, yes, you can do a projection on the author (but you could already in Search 5, though in a less convenient way):
#Entity
#Indexed
class Book {
#Id
private Long id;
#GenericField
private String title;
#ManyToOne
#IndexedEmbedded
private Author author;
// ...getters and setters...
}
#Entity
class Author {
#Id
private Long id;
#GenericField(projectable = Projectable.YES)
private String firstName;
#GenericField(projectable = Projectable.YES)
private String lastName;
#OneToMany(mappedBy = "author")
private List<Book> books = new ArrayList<>();
// ...getters and setters...
}
class MyProjectedAuthor {
public final String firstName;
public final String lastName;
public MyProjectedAuthor(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}
SearchSession searchSession = Search.session(entityManager);
List<MyProjectedAuthor> projectedAuthors = searchSession.search(Book.class)
.asProjection(f -> f.composite(
MyProjectedAuthor::new,
f.field("author.firstName", String.class),
f.field("author.lastName", String.class),
))
.predicate(f -> f.matchAll())
.fetchHits(20);
Multi-valued projections (e.g. if you have multiple authors per book) are not supported yet, but we will be working on it before the 6.0.0 release: https://hibernate.atlassian.net/browse/HSEARCH-3391
If you were talking about loading the authors from the database instead of projections, then there is no such built-in feature yet. We'll be looking into it when we address HSEARCH-3071, but I can't tell how long it will take.
As a workaround, for single-valued associations, you can implement loading manually:
#Entity
#Indexed
class Book {
#Id
private Long id;
#GenericField
private String title;
#ManyToOne
#IndexedEmbedded
private Author author;
// ...getters and setters...
}
#Entity
class Author {
#Id
#GenericField(projectable = Projectable.YES)
private Long id;
private String firstName;
private String lastName;
#OneToMany(mappedBy = "author")
private List<Book> books = new ArrayList<>();
// ...getters and setters...
}
SearchSession searchSession = Search.session(entityManager);
List<Author> authors = searchSession.search(Book.class)
.asProjection(f -> f.composite(
authorId -> entityManager.getReference(Author.class, authorId),
f.field("author.id", Long.class)
))
.predicate(f -> f.matchAll())
.fetchHits(20);
// Or, for more efficient loading:
SearchSession searchSession = Search.session(entityManager);
List<Long> authorIds = searchSession.search(Book.class)
.asProjection(f -> f.field("author.id", Long.class))
.predicate(f -> f.matchAll())
.fetchHits(20);
List<Author> authors = entityManager.unwrap(Session.class).byMultipleIds(Author.class)
.withBatchSize(20)
.multiLoad(authorIds);
EDIT: According to your comment, your problem was related to many fields, not just the author. Essentially, you are concerned about loading as few associations as possible.
The most common solution to this problem in Hibernate ORM is to set all your associations' fetch mode to lazy (which is what you should do by default, anyway).
Then when searching, do not even think about loading: just ask Hibernate Search to retrieve the entities you need. Associations will not be loaded at that point.
Then, when you serialize your entities to JSON, only the associations you actually use will be loaded. If you correctly set the default batch fetch size (with hibernate.default_batch_fetch_size, here, performance should be comparable to what you'll achieve with more complicated solutions, at a fraction of the development time.
If you really want to fetch some of the associations eagerly, the easiest solution would probably be to leverage JPA's entity graphs: they tell Hibernate ORM which associations to load exactly when it loads the Book entity.
There's no built-in functionnality for that in Hibernate Search 6 yet, but you can do it manually:
#Entity
#Indexed
class Book {
#Id
private Long id;
#GenericField
private String title;
#ManyToOne // No need for an #IndexedEmbedded with this solution, at least not for loading
private Author author;
// ...getters and setters...
}
#Entity
class Author {
#Id // No need for an indexed ID with this solution, at least not for loading
private Long id;
private String firstName;
private String lastName;
#OneToMany(mappedBy = "author")
private List<Book> books = new ArrayList<>();
// ...getters and setters...
}
SearchSession searchSession = Search.session(entityManager);
List<Long> bookIds = searchSession.search(Book.class)
.asProjection(f -> f.composite(ref -> (Long)ref.getId(), f.entityReference()))
.predicate(f -> f.matchAll())
.fetchHits(20);
// Note: there are many ways to build a graph, some less verbose than this one.
// See https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#fetching-strategies-dynamic-fetching-entity-graph
javax.persistence.EntityGraph<Book> graph = entityManager.createEntityGraph( Book.class );
graph.addAttributeNode( "author" );
// Ask for the author association to be loaded eagerly
graph.addSubgraph( "author" ).addAttributeNode( "name" );
List<Book> booksWithOnlySomeAssociationsFetched = entityManager.unwrap(Session.class).byMultipleIds(Book.class)
.with(graph, org.hibernate.graph.GraphSemantic.FETCH)
.withBatchSize(20)
.multiLoad(bookIds);
Note that, even with this solution, you should probably set the fetch mode to lazy in the mapping (#OnyToMany, ...) for as many associations as possible, because Hibernate ORM doesn't allow making an eager association lazy though a fetch graph.

How to expose a complete tree structure with Spring Data REST and HATEOAS?

I have a JPA tree structure
#Entity
public class Document {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String text;
#ManyToOne
#JoinColumn(name = "parent")
Document parent;
#OneToMany(mappedBy = "parent", fetch = FetchType.EAGER)
Set<Document> children;
(getters and setters)
}
and a projection
#Projection(name = "all", types = Document.class)
public interface AllDocumentsProjection {
int getId();
String getText();
Set<Document> getChildren();
}
When I make a GET request with url
localhost:8080/documents/1?projection=all
I only get the first children of the root document. Not children of the children. Is this possible with projections? Or is there an other way?
#Projection(name = "all", types = Document.class)
public interface AllDocumentsProjection {
int getId();
String getText();
Set<AllDocumentsProjection> getChildren();
}
This works perfect for me.
I'm almost certain there is no way to recursively embed resources via projections. Only other thing I think of is to handle this logic manually in the controller :/
Try excerpts.
You should add to your repository definition the excerptProjection field like below:
#RepositoryRestResource(excerptProjection = AllDocumentsProjection.class)
interface DocumentRepository extends CrudRepository<Document, Integer> {}

Cannot use an #IdClass attribute for a #ManyToOne relationship

I have a Gfh_i18n entity, with a composite key (#IdClass):
#Entity #IdClass(es.caib.gesma.petcom.data.entity.id.Gfh_i18n_id.class)
public class Gfh_i18n implements Serializable {
#Id #Column(length=10, nullable = false)
private String localeId = null;
#Id <-- This is the attribute causing issues
private Gfh gfh = null;
....
}
And the id class
public class Gfh_i18n_id implements Serializable {
private String localeId = null;
private Gfh gfh = null;
...
}
As this is written, this works. The issue is that I also have a Gfh class which will have a #OneToMany relationship to Gfh_i18n:
#OneToMany(mappedBy="gfh")
#MapKey(name="localeId")
private Map<String, Gfh_i18n> descriptions = null;
Using Eclipse Dali, this gives me the following error:
In attribute 'descriptions', the "mapped by" attribute 'gfh' has an invalid mapping type for this relationship.
If I just try to do, in Gfh_1i8n
#Id #ManyToOne
private Gfh gfh = null;
it solves the previous error but gives one in Gfh_i18n, stating that
The attribute matching the ID class attribute gfh does not have the correct type es.caib.gesma.petcom.data.entity.Gfh
This question is similar to mine, but I do not fully understand why I should be using #EmbeddedId (or if there is some way to use #IdClass with #ManyToOne).
I am using JPA 2.0 over Hibernate (JBoss 6.1)
Any ideas? Thanks in advance.
You are dealing with a "derived identity" (described in the JPA 2.0 spec, section 2.4.1).
You need to change your ID class so the field corresponding to the "parent" entity field in the "child" entity (in your case gfh) has a type that corresponds to either the "parent" entity's single #Id field (e.g. String) or, if the "parent" entity uses an IdClass, the IdClass (e.g. Gfh_id).
In Gfh_1i8n, you should declare gfh like this:
#Id #ManyToOne
private Gfh gfh = null;
Assuming GFH has a single #Id field of type String, your ID class should look like this:
public class Gfh_i18n_id implements Serializable {
private String localeId = null;
private String gfh = null;
...
}

JPA Query Many To One nullable relationship

I have the following entities and would like to seek help on how to query for selected attributes from both side of the relationship. Here is my model. Assume all tables are properly created in the db. JPA provider I am using is Hibernate.
#Entity
public class Book{
#Id
private long id;
#Column(nullable = false)
private String ISBNCode;
#ManyToOne(cascade = CascadeType.DETACH, fetch = FetchType.LAZY, optional = false)
private Person<Author> author;
#ManyToOne(cascade = CascadeType.DETACH, fetch = FetchType.LAZY, optional = true)
private Person<Borrower> borrower;
}
#Inheritance
#DiscriminatorColumn(name = "personType")
public abstract class Person<T>{
#Id
private long id;
#OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Info information;
}
#Entity
#DiscriminatorValue(PersonType.Author)
public class Author extends Person<Author> {
private long copiesSold;
}
#Entity
#DiscriminatorValue(PersonType.Borrower)
public class Borrower extends Person<Borrower> {
.....
}
#Entity
public class Info {
#Id
private long id;
#Column(nullable=false)
private String firstName;
#Column(nullable=false)
private String lastName;
......;
}
As you can see, the book table has a many to one relation to Person that is not nullable and Person that is nullable.
I have a requirement to show, the following in a tabular format -
ISBNCode - First Name - Last Name - Person Type
How can I write a JPA query that will allow me to select only attributes that I would want. I would want to get the attributes ISBN Code from Book, and then first and last names from the Info object that is related to Person Object that in turn is related to the Book object. I would not want to get all information from Info object, interested only selected information e.g first and last name in this case.
Please note that the relation between the Borrower and Book is marked with optional=true, meaning there may be a book that may not have been yet borrowed by someone (obviously it has an author).
Example to search for books by the author "Marc":
Criteria JPA Standard
CriteriaQuery<Book> criteria = builder.createQuery( Book.class );
Root<Book> personRoot = criteria.from( Book.class );
Predicate predicate = builder.conjunction();
List<Expression<Boolean>> expressions = predicate.getExpressions();
Path<Object> firtsName = personRoot.get("author").get("information").get("firstName");
expressions.add(builder.equal(firtsName, "Marc"));
criteria.where( predicate );
criteria.select(personRoot);
List<Book> books = em.createQuery( criteria ).getResultList();
Criteria JPA Hibernate
List<Book> books = (List<Book>)sess.createCriteria(Book.class).add( Restrictions.eq("author.information.firstName", "Marc") ).list();
We recommend using hibernate criterias for convenience and possibilities.
Regards,

Why am I getting "Duplicate entry" errors for related objects upon merge in eclipselink?

I have an entity class that contains a map of key-value pairs which live in a different table and there may be no such pairs for a given entity. The relevant code for the entity classes is below.
Now, when I insert such an entity with persist(), then add key-value pairs, and then save it with merge(), I get duplicate entry errors for the related table that stores the key-value pairs. I tried to hold back insertion until the keys were added, to have one call to persist() only. This led to duplicate entry errors containing an empty (zero) id in the foreign key column (ixSource).
I followed the process in the debugger, and found that eclipselink seems to be confused about the cascading. While it is updating the entity, it executes calls that update the related table. Nonetheless, it also adds those operations to a queue that is processed afterwards, which is when the duplicate entry errors occur. I have tried CascadeType.ALL and MERGE, with no difference.
I'm using static weaving, if it matters.
Here's the entities`code, shortened for brevity:
#Entity
#Inheritance(strategy = InheritanceType.JOINED)
#DiscriminatorColumn(name = "sType")
#Table(name = "BaseEntity")
public abstract class BaseEntity extends AbstractModel
{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "ix")
private long _ix;
}
#Entity
#Table(name = "Source")
public class Source extends BaseEntity
{
#OneToMany(cascade = CascadeType.MERGE)
#JoinTable(name = "SourceProperty", joinColumns = { #JoinColumn(name = "ixSource") })
#MapKey(name = "sKey")
private Map<String, SourceProperty> _mpKeys;
// ... there's more columns that probably don't matter ...
}
#Entity
#Table(name = "SourceProperty")
#IdClass(SourcePropertyKey.class)
public class SourceProperty
{
#Id
#Column(name = "sKey", nullable = false)
public String sKey;
#Id
#Column(name = "ixSource", nullable = false)
public long ixSource;
#Column(name = "sValue", nullable = true)
public String sValue;
}
public class SourcePropertyKey implements Serializable
{
private final static long serialVersionUID = 1L;
public String sKey;
public long ixSource;
#Override
public boolean equals(Object obj)
{
if (obj instanceof SourcePropertyKey) {
return this.sKey.equals(((SourcePropertyKey) obj).sKey)
&& this.ixSource == ((SourcePropertyKey) obj).ixSource;
} else {
return false;
}
}
}
I can't see how those errors would occur. Could you include the SQL and ful exception.
What version of EclipseLink are you using, did you try the latest release?
Why are you calling merge? Are you detaching the objects through serialization, if it is the same object, you do not need to call merge.
It could be an issue with the #MapKey, does it work if you remove this?