0

I have two domain models mapped using Hibernate @OneToMany. I am trying to create a JSON object in the frontend and send it to the spring mvc controller to set the model data on its own.

Following are my model classes: ConceptModelDetails.java

@Entity
@Table(name="conceptModelDetails")
@SequenceGenerator(name="CONCEPT_SEQ",sequenceName="concept_sequence", initialValue=1, allocationSize=1)
public class ConceptModelDetails implements java.io.Serializable{
    private static final long serialVersionUID = 1L;
    @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="CONCEPT_SEQ")
    private int instructionsId;
    private String operationType;
    private String conceptModelID;
    private String requestor;
    private String status;
    private Timestamp requestDateTime;
    private Timestamp lastExecutedDateTime;
    private Timestamp completedDateTime;
    @OneToMany(fetch = FetchType.EAGER, cascade=CascadeType.ALL, mappedBy="conceptModelDetails")
    @JsonManagedReference   // nested exception is org.springframework.http.converter.HttpMessageNotWritableException: 
    //Could not write JSON: Infinite recursion
//The fix is to get Jackson to be able to handle bi-directional references
    private List<Instructions> instructions = new ArrayList<Instructions>(); 




    public ConceptModelDetails() {
        // TODO Auto-generated constructor stub
    }


//setter & getter methods        
}

and Instructions.java:

@Entity
@Table(name="instructions")
@SequenceGenerator(name="INSTRUCTIONS_SEQ", sequenceName="instructions_sequence",initialValue=1, allocationSize=1)
public class Instructions implements java.io.Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="INSTRUCTIONS_SEQ")
    private int Sno;
    private String instruction;
    @ManyToOne
    @JoinColumn(name="instructionsId")
    @JsonBackReference
    private ConceptModelDetails conceptModelDetails;


    //setter & getter methods
}

This is my send method at frontend to create and send the JSON object:

$scope.send = function() {
    console.log("test");
    var dataObj = {
        "operationType" : $scope.operationType,
        "conceptModelID" : $scope.conceptID,
        "requestor" : $scope.requestor,
        "status" : "new",
        "requestDateTime" : null,
        "lastExecutedDateTime" : null,
        "completedDateTime" : null,
        "instructions" : null

    };
    console.log(dataObj);
    dataObj.instructions = [];    
    console.log($scope.operations_publish);
    var ins = getSelected();
    for ( var i in ins) {
        var temp = {

            instruction : null,
            conceptModelDetails : null

        }
        temp.instruction = ins[i];
        dataObj.instructions.push(temp);
    }
    var response = $http.post(
            'PostService', dataObj);
    response.success(function(data, status, headers, config) {
        $scope.responseData = data;
    });
    response.error(function(data, status, headers, config) {
        alert("Exception details: " + JSON.stringify({
            data : data
        }));
    });
}

Following is my controller:

@RequestMapping(value = "/PostService", method = RequestMethod.POST)
public @ResponseBody String Test(@RequestBody ConceptModelDetails conceptModelDetails){
    ApplicationContext context = new ClassPathXmlApplicationContext(
            "applicationContext.xml");
    ConceptModelDAO obj = (ConceptModelDAO) context.getBean("objDAO");
    System.out.println("concept id: "+conceptModelDetails.getConceptModelID()+" "+ conceptModelDetails.getInstructionsId());
    System.out.println("instructions id: "+conceptModelDetails.getInstructions());
    // ConceptModelDAOImpl objDAO = new ConceptModelDAOImpl();
    obj.add(conceptModelDetails);
    Instructions instructions = new Instructions();
    System.out.println("dimba: " + instructions.getInstruction());
    ArrayList<Instructions> operations = (ArrayList<Instructions>) conceptModelDetails.getInstructions();
    for (int i = 0; i< operations.size(); i++ ) {
        instructions.setInstruction(operations.get(i).getInstruction());
        instructions.setConceptModelDetails(conceptModelDetails);
        obj.addInstructions(instructions);
    }
    return null;
}

I am getting the eror: 400 (Bad Request) because of List<Instructions> instructions. Please suggest how do I deal with this.

3
  • Can you please post the stack trace for the exception you would be getting in the logs? Commented Dec 1, 2015 at 11:02
  • Show us the JSON getting posted from the UI as well. Commented Dec 1, 2015 at 11:38
  • @DeepakKumar: This was the error: org.hibernate.collection.internal.PersistentBag cannot be cast to java.util.ArrayList. It has been resolved. Thanks. Commented Dec 1, 2015 at 12:11

1 Answer 1

2

I have found the problem in this code. As explained by Bozho here,
ArrayList<Instructions> operations = (ArrayList<Instructions>) conceptModelDetails.getInstructions();

should be

List<Instructions> operations = conceptModelDetails.getInstructions(); in the spring controller.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.