6

I am working with a DXA 2.0 Java implementation where I have created 2 custom models, one for a schema and the second one for an embedded schema

Below is the code

This is for Entity 1

@SemanticEntity(entityName = "Entity1", vocabulary = SDL_CORE, public_ = true)
@Data
@EqualsAndHashCode(callSuper = true)
public class Entity1 extends AbstractEntityModel{

@JsonProperty("ContentItems")
@SemanticProperty("contentItems")
public List<Entity2> contentItems;

@JsonProperty("Items")
@SemanticProperty("items")
public List<Paragraph> items;

@JsonProperty("SomeText")
@SemanticProperty("someText")
public String someText;

}

This is for Entity 2

@Data
@EqualsAndHashCode(callSuper = true)
public class Entity2 extends AbstractEntityModel {

    @JsonProperty("Key")
    private String key;

    @JsonProperty("TextValue")
    private RichText textValue;

    @JsonProperty("MultimediaValue")
    private MediaItem multimediaValue;

}

Note that Entity1 was created referring the default Article class and Entity2 was created referring the default Paragraph class.

I also have a controller that renders these entities on a view. And for tracing I have a System.out.println statement which prints out the entity object. The code for which is mentioned below

@RequestMapping(method = RequestMethod.GET, value = {"/Entity1/{entityId}"})
    public ModelAndView Entity1Display(HttpServletRequest request, @PathVariable String entityId) {
        EntityModel entity = (EntityModel) request.getAttribute("_entity_");
        System.out.println(entity);     
        ModelAndView m = new ModelAndView();        
        m.setViewName("/ModuleName/Entity/ViewName");
        return m;
    }

The System.out.println statement displays this

Entity1(contentItems=[Entity2(key=null, textValue=null, multimediaValue=null), Entity2(key=null, textValue=null, multimediaValue=null)], items=[Paragraph(subheading=Something Paragraph, content=Something, media=null, caption=null), Paragraph(subheading=Something Paragraph 2, content=22222, media=null, caption=null)], someText=Test123)

Where you see that Entity1 is mapped correctly and gets data except for the Entity2 data which is null even though there is content in it on the CM.

So what seems to be wrong here with the Entity2 model? Should these models be in a specific package? I guess the package might not be an issue since Entity1 gets its data. I think it might be an issue with the embedded schema and I might have missed a step here while creating the embedded schema.

Edit:

These are the thing I have tried but it still doesn't work

-Having semantic entity at entity 2 as well

@SemanticEntity(entityName = "Entity2", vocabulary = SDL_CORE, public_ = true)
@Data
@EqualsAndHashCode(callSuper = true)
public class Entity2 extends AbstractEntityModel {

@JsonProperty("Key")
@SemanticProperty("key")
private String key;

@JsonProperty("TextValue")
@SemanticProperty("textValue")
private RichText textValue;

@JsonProperty("MultimediaValue")
@SemanticProperty("multimediaValue")
private MediaItem multimediaValue;

}

-Tried SCHEMA_ORG instead of SDL_CORE in vocabulary

My Component source looks like this

<Entity1 xmlns="uuid:8bbc604b-26fc-46d8-855d-bc16fb54ef0a">
    <contentItems>
        <key>Test1FW</key>
        <textValue>TestFW</textValue>
    </contentItems>
    <contentItems>
        <key>Test2FW</key>
        <textValue>TestFW</textValue>
    </contentItems>
    <items>
        <subheading>Something Paragraph</subheading>
        <content>Something</content>
    </items>
    <items>
        <subheading>Something Paragraph 2</subheading>
        <content>22222</content>
    </items>
    <someText>Test123</someText>
</Entity1>
  • Can you try putting @SemanticProperty for Entity2? – Manish Mehmood Jun 12 '18 at 17:08
  • You mean @SemanticEntity, right. Indeed, the Entity2 class should have such an annotation referring to to root element/type name of the embedded schema (unless that happens to be the same as the class name). – Rick Pannekoek Jun 12 '18 at 17:22
  • Hi Rick and Manish, I had @SemanticEntity there before which didn't work so I removed it as the default Paragraph class doesn't have it either. – Reynold Bhatia Jun 12 '18 at 17:25
  • Also, the name of the embedded schema and the class name are same – Reynold Bhatia Jun 12 '18 at 18:28
  • Does the DXA log file reveal any useful diagnostics (when log level is set to DEBUG or TRACE)? – Rick Pannekoek Jun 12 '18 at 18:56
3

So I got it to work. The solution is to have the Model for the embedded schema to be in the package com.sdl.webapp.addon.model.entity. It seems like a limitation on DXA where you cannot create a model that gets mapped to an embedded schema to be in a custom package and has to be in the package mentioned above.

In my case, I moved the Entity2 class to com.sdl.webapp.addon.model.entity package and it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.