Announcement Announcement Module
Collapse
No announcement yet.
Unable to load lazy collection after JPARepository.save() Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unable to load lazy collection after JPARepository.save()

    Hi,

    I have an model class which has a lazy loaded child collection. In one of my controllers, I will pass on an entity from a form to a @Transactional annotated service method, which calls JPARepository.save(). After that call (still within the same service method), I want to access the collection, but it's always empty. In another service method, which is called after a GET request, I call JPARepository.findOne(), and the collection is there as expected. What am I missing?

    Code:
    @Entity
    public class Test implements Identifiable<Integer> {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Integer id;
    
        @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "test")
        private List<TestLabel> testLabels = new ArrayList<TestLabel>();
    Code:
    @Entity
    public class TestLabel implements Identifiable<Integer> {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Integer id;
    
        @ManyToOne(fetch = FetchType.LAZY)
        private Test test;
    
        @ManyToOne(fetch = FetchType.LAZY)
        private Label label;
    Code:
    @RequestMapping(value="/addlabel", method=RequestMethod.POST)
    public String addLabel(@ModelAttribute("testForm") TestForm testForm, BindingResult result, ModelMap model) {
        Test test = testForm.getTest();
        Label label = testForm.getLabel();
        test = testService.save(test);
        logger.info("number of labels in controller: " + test.getTestLabels().size());
        TestLabel testLabel = testLabelService.save(new TestLabel());
        testLabel.setLabel(label);
        test.add(testLabel);
        test = testService.save(test);
        return "redirect:/test/" + test.getId();
    }
    Code:
    @Override
    @Transactional
    public Test save(Test test) {
        test = testRepository.save(test);
        logger.info("number of labels in service save: " + test.getTestLabels().size());
        return test;
    }
    
    @Override
    @Transactional
    public Test findOne(Integer id) {
        Test test = testRepository.findOne(id);
        logger.info("number of labels in service findone: " + test.getTestLabels().size());
        return test;
    }
    Code:
    2014-05-08 16:29:51 DEBUG AbstractTransactionImpl:160 - begin
    2014-05-08 16:29:51 DEBUG JdbcTransaction:69 - initial autocommit status: true
    2014-05-08 16:29:51 DEBUG JdbcTransaction:71 - disabling autocommit
    2014-05-08 16:29:51 DEBUG AbstractTransactionImpl:175 - committing
    2014-05-08 16:29:51 DEBUG JdbcTransaction:113 - committed JDBC Connection
    2014-05-08 16:29:51 DEBUG JdbcTransaction:126 - re-enabling autocommit
    2014-05-08 16:29:51 DEBUG AbstractTransactionImpl:160 - begin
    2014-05-08 16:29:51 DEBUG JdbcTransaction:69 - initial autocommit status: true
    2014-05-08 16:29:51 DEBUG JdbcTransaction:71 - disabling autocommit
    2014-05-08 16:29:51 DEBUG Loader:2136 - Loading entity: [test.model.Test#0]
    2014-05-08 16:29:51 DEBUG SQL:109 - select test0_.id as id1_1_1_, test0_.name as name2_1_1_, testlabels1_.test_id as test_id3_1_3_, testlabels1_.id as id1_2_3_, testlabels1_.id as id1_2_0_, testlabels1_.label_id as label_id2_2_0_, testlabels1_.test_id as test_id3_2_0_ from Test test0_ left outer join TestLabel testlabels1_ on test0_.id=testlabels1_.test_id where test0_.id=?
    2014-05-08 16:29:51 DEBUG Loader:951 - Result set row: 0
    2014-05-08 16:29:51 DEBUG Loader:1485 - Result row: EntityKey[test.model.TestLabel#0], EntityKey[test.model.Test#0]
    2014-05-08 16:29:51 DEBUG Loader:1305 - Found row of collection: [test.model.Test.testLabels#0]
    2014-05-08 16:29:51 DEBUG Loader:951 - Result set row: 1
    2014-05-08 16:29:51 DEBUG Loader:1485 - Result row: EntityKey[test.model.TestLabel#1], EntityKey[test.model.Test#0]
    2014-05-08 16:29:51 DEBUG Loader:1305 - Found row of collection: [test.model.Test.testLabels#0]
    2014-05-08 16:29:51 DEBUG Loader:951 - Result set row: 2
    2014-05-08 16:29:51 DEBUG Loader:1485 - Result row: EntityKey[test.model.TestLabel#2], EntityKey[test.model.Test#0]
    2014-05-08 16:29:51 DEBUG Loader:1305 - Found row of collection: [test.model.Test.testLabels#0]
    2014-05-08 16:29:51 DEBUG Loader:951 - Result set row: 3
    2014-05-08 16:29:51 DEBUG Loader:1485 - Result row: EntityKey[test.model.TestLabel#3], EntityKey[test.model.Test#0]
    2014-05-08 16:29:51 DEBUG Loader:1305 - Found row of collection: [test.model.Test.testLabels#0]
    2014-05-08 16:29:51 DEBUG Loader:951 - Result set row: 4
    2014-05-08 16:29:51 DEBUG Loader:1485 - Result row: EntityKey[test.model.TestLabel#4], EntityKey[test.model.Test#0]
    2014-05-08 16:29:51 DEBUG Loader:1305 - Found row of collection: [test.model.Test.testLabels#0]
    2014-05-08 16:29:51 DEBUG Loader:951 - Result set row: 5
    2014-05-08 16:29:51 DEBUG Loader:1485 - Result row: EntityKey[test.model.TestLabel#5], EntityKey[test.model.Test#0]
    2014-05-08 16:29:51 DEBUG Loader:1305 - Found row of collection: [test.model.Test.testLabels#0]
    2014-05-08 16:29:51 DEBUG TwoPhaseLoad:160 - Resolving associations for [test.model.TestLabel#0]
    2014-05-08 16:29:51 DEBUG TwoPhaseLoad:286 - Done materializing entity [test.model.TestLabel#0]
    2014-05-08 16:29:51 DEBUG TwoPhaseLoad:160 - Resolving associations for [test.model.Test#0]
    2014-05-08 16:29:51 DEBUG TwoPhaseLoad:286 - Done materializing entity [test.model.Test#0]
    2014-05-08 16:29:51 DEBUG TwoPhaseLoad:160 - Resolving associations for [test.model.TestLabel#1]
    2014-05-08 16:29:51 DEBUG TwoPhaseLoad:286 - Done materializing entity [test.model.TestLabel#1]
    2014-05-08 16:29:51 DEBUG TwoPhaseLoad:160 - Resolving associations for [test.model.TestLabel#2]
    2014-05-08 16:29:51 DEBUG TwoPhaseLoad:286 - Done materializing entity [test.model.TestLabel#2]
    2014-05-08 16:29:51 DEBUG TwoPhaseLoad:160 - Resolving associations for [test.model.TestLabel#3]
    2014-05-08 16:29:51 DEBUG TwoPhaseLoad:286 - Done materializing entity [test.model.TestLabel#3]
    2014-05-08 16:29:51 DEBUG TwoPhaseLoad:160 - Resolving associations for [test.model.TestLabel#4]
    2014-05-08 16:29:51 DEBUG TwoPhaseLoad:286 - Done materializing entity [test.model.TestLabel#4]
    2014-05-08 16:29:51 DEBUG TwoPhaseLoad:160 - Resolving associations for [test.model.TestLabel#5]
    2014-05-08 16:29:51 DEBUG TwoPhaseLoad:286 - Done materializing entity [test.model.TestLabel#5]
    2014-05-08 16:29:51 DEBUG CollectionLoadContext:232 - 1 collections were found in result set for role: test.model.Test.testLabels
    2014-05-08 16:29:51 DEBUG CollectionLoadContext:280 - Collection fully initialized: [test.model.Test.testLabels#0]
    2014-05-08 16:29:51 DEBUG CollectionLoadContext:240 - 1 collections initialized for role: test.model.Test.testLabels
    2014-05-08 16:29:51 DEBUG Loader:2160 - Done entity load
    2014-05-08 16:29:51 INFO  TestServiceImpl:35 - number of labels in service save: 0
    2014-05-08 16:29:51 DEBUG AbstractTransactionImpl:175 - committing
    I'm not sure if it's relevant, but I'm using a OpenEntityManagerInViewFilter.

    Thanks!
Working...
X