0

I have an sample application on spring-jpa using mongodb. I have my service class as

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
//import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Component
public class Test  {

    @PersistenceContext
         EntityManager em;
         EntityManagerFactory emf;

         @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public String persist(Details details) {
             Query query = em.createNativeQuery("db.Details.findOne(username= "+details.getUsername()+"& password= "+details.getPassword(), Details.class);
             List<Details> resultList = query.getResultList();
             System.out.println(query.toString());
             Object t = null;
                if (!em.contains(t)) {
                em.persist(details);
                em.flush();
                System.out.println("Persist successful ...");
                }
                em.clear();
                em.close();
            return "persist";
         }
}

Controller class

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
//import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class HelloWorldController {

@Autowired
private Test test;

    @RequestMapping("/")
            public String hello() {
                return "hello";
    }
    @RequestMapping(value = "/hi", method = RequestMethod.GET)
    public String hi(@RequestParam("username") String username, @RequestParam("password") String password, ModelMap model) throws Exception{
        Details details = new Details();
        test.persist(details);
        model.addAttribute("username", details.getUsername());
        model.addAttribute("password", details.getPassword());
        return "hi";
    }

}

and my pojo class is

@Entity
@Table(name="Details")
public class Details {
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "details")
    @TableGenerator(name="details")
    @Column
    private int Id;
    @Column
    private String username;
    @Column
    private String password;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }


}

My application runs good but the data is not inserting into the database i am not sure on the QUERY. can any help me out what i need to make changes in query so that i can retrieve the data. Thanks in advance.

1 Answer 1

1

You can not use spring-data-jpa to access mongo. You need to use spring-data-mongodb which uses different model, starting with its entity annotations.

The easiest way to access mongo is

  • adding spring-data-mongodb dependency to your POM
  • creating a Repository interface
  • annotating your POJO acting as DAO at least with @Document("aCollectionName") and your id field with `@Id.

Let this serve you as minimun requirements to get it working. For specific details follow the corresponding doc and tutorial.

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

3 Comments

Guess so, but never tried it. My advice is always limited by my experience.
I think i need to research more because i think it may be possible. but i need the correct query. and ill definitely follow your sugestion.thanks for the response.
Why can't we use spring-data-jpa to access mongo?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.