0

I am having a problem with findAll() method of MongoOperations class when I try to retrieve data from the database . Actually I would like to get the values of the parameters : iliteID, latitude, longitude, height, etc.. Could anyone help out with this issue ?

Thanks in advance .

Here the data structure in the database : (Note : data are entered in a form and saved into the database from a Servlet)

> db.ILites.find().pretty()
{
    "_id" : ObjectId("56727fd1b955631adcee6fb1"),
    "iLiteID" : "I_Lite1",
    "Latitude" : 48.2188,
    "Target Power" : 32,
    "Status" : "df",
    "Ip Address" : "3.3.3.3",
    "Longitude" : 11.6248,
    "Current Power" : 32,
    "Transmission" : "OFF",
    "Connectivity" : "we",
    "Height" : "23",
    "Nominal Message" : "df",
    "Emergency Message" : "wrwew"
   }
   {
    "_id" : ObjectId("56728061b955631adcee6fb3"),
    "iLiteID" : "I_Lite1",
    "Latitude" : 48.2187,
    "Target Power" : 43,
    "Status" : "dg",
    "Ip Address" : "2.2.2.2",
    "Longitude" : 11.6247,
    "Current Power" : 43,
    "Transmission" : "ON",
    "Connectivity" : "dgdf",
    "Height" : "34",
    "Nominal Message" : "cb",
    "Emergency Message" : "gdf"
   }

Here the java code:

protected List<ILites> getAllILites(){
    ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig.class);
    MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");

    List<ILites> ilites = mongoOperation.findAll(ILites.class, "ILites");

    System.out.println("#3 for advance");
    for (ILites temp : ilites) {

        // I am getting an "null" value instead of the value of the longitude
        System.out.println(temp.getLongitude());

        //This displays something like this : mongodb.model.ILites@732cbcd5 (which i don't really understand)
        System.out.println(temp);
    }

    return ilites;
}

SprinMongoConfig Class

@Configuration
public class SpringMongoConfig {

public @Bean MongoDbFactory mongoDbFactory() throws Exception {
    return new SimpleMongoDbFactory(new MongoClient(), "ILiteDB");
}

/**
 *
 * @output MongoTemplate
 * @description
 */
public @Bean MongoTemplate mongoTemplate() throws Exception {   


    final MappingMongoConverter converter = new MappingMongoConverter(
            mongoDbFactory(), new MongoMappingContext());
    /**
     * To remove "_class" field in collection which is useless and added by
     * default
     */
    converter.setTypeMapper(new DefaultMongoTypeMapper(null));

    final MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory(),
            converter);

    return mongoTemplate;

}
}

Spring Controller Class

/**
 *
 * @output ModelAndView
 * @description redirect to dashboard
 */

@RequestMapping(value = "/adminDashboard", method = RequestMethod.GET)
public ModelAndView adminPage() {
    userDao.updateLastLogin(getCurrentUser());
    final ModelAndView model = new ModelAndView();
    model.addObject("iLiteList", getAllILites());
    model.setViewName("adminDashboard");
    return model;
}

Entity Class

@Document(collection = "ILites")
public class ILites {

private String status;
private String ipAddress;
private String transmission;
private String connectivity;
private String emergencyMessage;
private String nominalMessage;
//private String manualCommand;
private Double latitude;
private Double longitude;
private Double currentPower;
private Double targetPower;
private Double height;

/**
 * Primary key - Unique Identifier
 */
@Id
private String iLiteID;

public ILites(final String iLiteID, final String status,final String ipAddress,
    final String transmission, final String connectivity,final String emergencyMessage,
    final String nominalMessage,  final Double latitude,
    final Double longitude, final Double currentPower,final Double targetPower,final Double height) {
super();
this.iLiteID = iLiteID;
this.status = status;
this.ipAddress = ipAddress;
this.transmission = transmission;
this.connectivity = connectivity;
this.emergencyMessage = emergencyMessage;
this.nominalMessage = nominalMessage;
//this.manualCommand = manualCommand;
this.longitude = longitude;
this.latitude = latitude;

this.currentPower = currentPower;
this.targetPower = targetPower;
this.height = height;
 }


public Double getHeight() {
    return height;
}


public void setHeight(Double height) {
    this.height = height;
}


public String getILiteID() {
    return iLiteID;
}

public void setILiteID(String iLiteID) {
    this.iLiteID = iLiteID;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public String getIpAddress() {
    return ipAddress;
}

public void setIpAddress(String ipAddress) {
    this.ipAddress = ipAddress;
}

public String getTransmission() {
    return transmission;
}

public void setTransmission(String transmission) {
    this.transmission = transmission;
}

public String getConnectivity() {
    return connectivity;
}

public void setConnectivity(String connectivity) {
    this.connectivity = connectivity;
}

public String getEmergencyMessage() {
    return emergencyMessage;
}

public void setEmergencyMessage(String emergencyMessage) {
    this.emergencyMessage = emergencyMessage;
}

public String getNominalMessage() {
    return nominalMessage;
}

public void setNominalMessage(String nominalMessage) {
    this.nominalMessage = nominalMessage;
}

/*public String getManualCommand() {
    return manualCommand;
}

public void setManualCommand(String manualCommand) {
    this.manualCommand = manualCommand;
}
*/
public Double getLatitude() {
    return latitude;
}

public void setLatitude(Double latitude) {
    this.latitude = latitude;
}

public Double getLongitude() {
    return longitude;
}

public void setLongitude(Double longitude){
    this.longitude = longitude;
}

public Double getCurrentPower() {
    return currentPower;
}

public void setCurrentPower(Double currentPower) {
    this.currentPower = currentPower;
}

public Double getTargetPower() {
    return targetPower;
}

public void setTargetPower(Double targetPower) {
    this.targetPower = targetPower;
}

}
Gerko Ford
  • 41
  • 2
  • 12
  • You are not really explaining your problem. But I guess, you expect the values height, status, Ipaddress to be stored in the ILites-Objects. Try to annotate the model-fields with @JsonProperty. – Simon K. Dec 17 '15 at 10:34
  • I am sorry , but yes I expect the values height, status , etc.. to be stored in the ILites Objects. you mean something like this ? @JsonProperty private String status; – Gerko Ford Dec 17 '15 at 10:42
  • look here http://stackoverflow.com/questions/12583638/when-is-the-jsonproperty-property-used-and-what-is-it-used-for . I found the jackson-annotation very helpful when I used them in a spring-project. This is only a suggestion. I still cannot help very much with the given information. – Simon K. Dec 17 '15 at 11:01
  • I am sorry but I am quite newbie in spring java. When I annotate the fields as you suggested , for example @JsonProperty("Status") , It is marked as an error and spring's suggested quick fixes are the following : @create annotation 'JsonProperty' or Fix project setup. which one should I go for ? or maybe there is another way of fixing this error ? – Gerko Ford Dec 17 '15 at 11:26
  • Ok, you are missing the necessary libraries. All in all you need to learn the basics. I suggest reading simple tutorials on java, maven, spring data, spring mongo integration before continuing. Takes time, but helps a lot. – Simon K. Dec 17 '15 at 11:28
  • Ok. I will do that. Thanks ! – Gerko Ford Dec 17 '15 at 11:41
  • Try mongo repository, instead of using mongo template.. CRUD operations are pretty simple using mongo repository. – Rakesh Dec 17 '15 at 15:18
  • Thanks for your help. I have just solved the issue. Actually I just changed the names of the keys of the key/value pairs in my database to match exactly the names of the instance variables in ILites Class and now I can retrieve from the database without any problem. However I will check how it works with mongo repository ;) Thanks again! – Gerko Ford Dec 17 '15 at 15:58

0 Answers0