Announcement Announcement Module
Collapse
No announcement yet.
Spring data cross-store: nullpointer when check the existence of a @RelatedDocument Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring data cross-store: nullpointer when check the existence of a @RelatedDocument

    Accessing an "hibrid" entity (part JPA e part mongoDB) obtained from a persistence context throw a null pointer exception when i try to read its mongoDb field using accessor method:

    Code:
    		List<Address> addresses = query.getResultList();
    		for (Address address : addresses) {
    			if(address.getSpatialInfo() == null){
    				SpatialInfo si = new SpatialInfo();
    				// filling bean properties here ....
    				address.setSpatialInfo(si);
    				this.mongo.save(si);  // <--- if i try to save the whole entity mongo save all entity properties as collections!!!!
    			}
    }
    Simply, if no document has been associated with an entity (so I check for the nullity of the property) the program create and associate one in mongoDb.

    Code:
    Caused by: java.lang.NullPointerException
    	at org.springframework.data.persistence.document.mongodb.MongoDocumentBacking.ajc$interMethod$org_springframework_data_persistence_document_mongodb_MongoDocumentBacking$org_springframework_data_persistence_document_DocumentBacked$get_persistent_id(MongoDocumentBacking.aj:183)
    	at org.novaworks.data.Address.get_persistent_id(Address.java:1)
    	at org.springframework.data.persistence.document.mongodb.MongoDocumentBacking.ajc$interMethodDispatch1$org_springframework_data_persistence_document_mongodb_MongoDocumentBacking$org_springframework_data_persistence_document_DocumentBacked$get_persistent_id(MongoDocumentBacking.aj)
    	at org.novaworks.data.Address.spatialInfo_aroundBody3$advice(Address.java:230)
    	at org.novaworks.data.Address.getSpatialInfo(Address.java:268)
    	at org.novaworks.plan.ui.panel.MutableLocalEntityPanel.getGisDataSupportModel(MutableLocalEntityPanel.java:431)
    So: there's a way to check if a persistent entity has already been associated with a document beside is relational part?

  • #2
    I've noticed that the entity (in this case Address) has been weaved with DocumentBacked interface and not the @Document bean itself (SpatialInfo), but probably it's correct while document must be accessed by relation with the entity acting as a "parent", It's strange that no weaving has been applied to the bean annotated with @Document ...

    By the way, there are the sources of two beans involved.
    I hope someone can help me because I'm stuck

    ENTITY BEAN:
    Code:
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.ManyToMany;
    import javax.persistence.OneToMany;
    import javax.persistence.Table;
    import javax.persistence.Transient;
    import javax.validation.constraints.NotNull;
    import javax.validation.constraints.Pattern;
    
    import org.hibernate.annotations.Cache;
    import org.hibernate.annotations.CacheConcurrencyStrategy;
    import org.hibernate.validator.constraints.Length;
    
    import org.novaworks.data.documents.SpatialInfo;
    import org.springframework.data.persistence.document.RelatedDocument;
    
    @Entity
    @Table( name = "LOCATIONS" )
    @Cache(region="long_lived", usage = CacheConcurrencyStrategy.READ_WRITE)
    public class Address extends AbstractPersistentEntity {
    
        private static final long serialVersionUID = -3606726741408514823L;	
    	
        @Length(max = 150)
        @NotNull
        private String street;	
    	
        @Length(max = 50)
        @NotNull
        private String city;
        
        @Length(max = 50)
        @Pattern(regexp = "^\\d*$")   
        private String code;
        
        @Length(max = 50)
        @NotNull
        private String country;
        private String reference;
    
        private String number;
    	
        @Column(name="FLOOR")
        private String floor; 
        private String area;
        
        @Transient
        @RelatedDocument
        private SpatialInfo spatialInfo;
    
        
        @ManyToMany( fetch = FetchType.LAZY , mappedBy = "addresses")
        private Set<Contact> contacts;
        
        
        public Address() {}
        
        // ... accessors
    }
    DOCUMENT BEAN:

    Code:
    import org.springframework.data.annotation.Id;
    import org.springframework.data.mongodb.core.mapping.Document;
    
    @Document
    public class SpatialInfo {
    	
        @Id
        private String id;
    	
        private double latitude;
        private double longitude;
        private double altitude;
            
        // ... accessors
    }

    Comment

    Working...
    X