Spring Data Neo4j polymorphic association appears embedded -
i'm having problem exposing relationship sub-type via rest. have abstract class called page:
@nodeentity @jsontypeinfo(use = jsontypeinfo.id.name, include = jsontypeinfo.as.property, property = "category", visible = true) @jsonsubtypes({ @type(value = musician.class), @type(value = book.class), @type(value = song.class) }) public abstract class page extends baseentity{ @fetch @createdby private user creator; @jsonserialize(using = localdatetimeserializer.class) @jsondeserialize(using = localdatetimedeserializer.class) @graphproperty(propertytype = long.class) @createddate private localdatetime timecreated; @notempty @size(min = 1, max = 160) @indexed(indextype = indextype.fulltext, indexname = "search") private string screenname; @fetch @relatedto(type = "channel") private channel channel = new channel(); public channel getchannel() { return channel; } public void setchannel(channel channel) { this.channel = channel; } public string getscreenname() { return screenname; } public void setscreenname(string screenname) { this.screenname = screenname; } // work around bug type name not exported sdr. @jsongetter(value = "category") public string gettype() { return this.getclass().getsimplename(); } public user getcreator() { return creator; } public void setcreator(user creator) { this.creator = creator; } public localdatetime gettimecreated() { return timecreated; } public void settimecreated(localdatetime timecreated) { this.timecreated = timecreated; } }
two subtypes song , musician:
@nodeentity @jsontypename("song") @jsontypeinfo(use=jsontypeinfo.id.name, include=jsontypeinfo.as.property, property="category", visible=true) public class song extends page { @fetch @relatedto(type = "singer") private musician singer; public musician getsinger() { return singer; } public void setsinger(musician singer) { this.singer = singer; } }
and
@nodeentity @jsontypename("musician") @jsontypeinfo(use=jsontypeinfo.id.name, include=jsontypeinfo.as.property, property="category", visible=true) public final class musician extends page { }
and repository supposed manage subtypes of page:
@repositoryrestresource(collectionresourcerel = "pages", path = "pages") public interface pagerepository extends pagingandsortingrepository<page, long> { org.springframework.data.domain.page<musician> findmusicianbyscreennamelike(@param("0") string screenname, pageable page); }
when song instance api json looks like:
{ "uuid" : "ee9daf8b-4285-45bb-a583-e37f54284c43", "timecreated" : null, "screenname" : "songtest", "singer" : null, "id" : 213, "category" : "song", "_links" : { "self" : { "href" : "http://localhost:8080/api/pages/213" }, "channel" : { "href" : "http://localhost:8080/api/pages/213/channel" }, "creator" : { "href" : "http://localhost:8080/api/pages/213/creator" } } }
the problem singer field appears embedded. , can not associate existing musician singer field. when try assign uri of existing musician singer field, complains can not convert string musician. if provide json instead of uri, creates new musician same field values. when subtype musician referenced entity, treated if not managed it's supertype's repository. how can make singer exported other associated top level resources under links section , can assigned existing resource accepting uri? or not possible @ all?
i believe you'll need separate repositories musician, book, , song spring data rest correctly determine relationships between entities. once that's fixed, should behave way expect - returning links embedded entities instead of json, , accepting uri's when posting embedded entities.
you might want take @ answer idea of how handle inheritance in repository definitions:
Comments
Post a Comment