facebook

Examples for use of generated Spring Scaffolded Named Query

  1. MyEclipse IDE
  2.  > 
  3. Spring Development
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #320708 Reply

    ssquire
    Member

    I really need help here. I have studied for the last week and a half, and I still can’t figure out how to use the automatically generated Named Queries in my Spring Scaffolded application. I am using Struts & MySQL with Spring capabilities and I have been studying the book “Pro JPA 2, Mastering the Java Persistence API”. I have only scaffolded 1 table ‘userlogintbl’. This table has 3 rows and 3 fields: username, password, & agencyid. The Spring Scaffolding generated the following Named Queries:

    @NamedQueries({
    @NamedQuery(name = “findAllUserlogintbls”, query = “select myUserlogintbl from Userlogintbl myUserlogintbl”),
    @NamedQuery(name = “findUserlogintblByAgencyid”, query = “select myUserlogintbl from Userlogintbl myUserlogintbl where myUserlogintbl.agencyid = ?1”),
    @NamedQuery(name = “findUserlogintblById”, query = “select myUserlogintbl from Userlogintbl myUserlogintbl where myUserlogintbl.id = ?1”),
    @NamedQuery(name = “findUserlogintblByPrimaryKey”, query = “select myUserlogintbl from Userlogintbl myUserlogintbl where myUserlogintbl.id = ?1”),
    @NamedQuery(name = “findUserlogintblBySecuritykey”, query = “select myUserlogintbl from Userlogintbl myUserlogintbl where myUserlogintbl.securitykey = ?1”),
    @NamedQuery(name = “findUserlogintblBySecuritykeyContaining”, query = “select myUserlogintbl from Userlogintbl myUserlogintbl where myUserlogintbl.securitykey like ?1”),
    @NamedQuery(name = “findUserlogintblByUsername”, query = “select myUserlogintbl from Userlogintbl myUserlogintbl where myUserlogintbl.username = ?1”),
    @NamedQuery(name = “findUserlogintblByUsernameContaining”, query = “select myUserlogintbl from Userlogintbl myUserlogintbl where myUserlogintbl.username like ?1”) })
    @Table(catalog = “fhhcs”, name = “userlogintbl”)

    Syntactically, how do I use these queries to retrieve information from my database. I’ve done the JPA Tutorial (1&2), the Struts Tutorial, the Database Tutorial, etc.

    #320747 Reply

    cconway
    Member

    Take a look in the scaffolded application’s dao package. In there should be a <tablename>DAOImpl.java file. You’ll see in there methods that call the named queries.

    For example, in my project I scaffolded the Product table from the Derby database packaged with MyEclipse. My ProductDAOImpl class has a method:

    public Set<Product> findAllProducts(int startResult, int maxRows) throws DataAccessException {
    Query query = createNamedQuery(“findAllProducts”, startResult, maxRows);
    return new LinkedHashSet<Product>(query.getResultList());
    }

    This method calls the “findAllProducts” named Query. This DAO method is called from the generated service layer.

    Does that help?

    #320824 Reply

    ssquire
    Member

    Bear with me please, Cindy. I’m still studying.

    So, in my Struts Action Class .java file, which I am using as a skeleton (from the struts tutorial), basically, I want to search my database for a match in the username of the username that was entered in my form – do I assign a variable of the table type (eg. UserlogintblDAO) then call any respective methods in the UserlogintblDAOImpl.java & UserlogintblServiceImpl.java files in my code to locate any records with matching usernames?

    Does this sound feasible? That is what I’m looking for an example of. Instead of comparing the username and password to myeclipse in the tutorial, I want to replace that code with a call to the method findUserlogintblByUsername and then call my .jsp success page.

    I hope I was clear in how I understand what I’m trying to do.

    #320859 Reply

    cconway
    Member

    Sorry, I can’t understand from your post where the real problem lies. Is the problem that you are having difficulty getting the Action to call anything from Spring?

    If so, maybe one of these articles would help:
    http://struts.apache.org/2.0.14/docs/struts-2-spring-2-jpa-ajax.html
    http://www.ibm.com/developerworks/java/library/j-sr2/index.html

    #320864 Reply

    ssquire
    Member

    Thanks for the examples, Cindy!

    I think this is what I am looking for. In your listings from the link http://struts.apache.org/2.0.14/docs/struts-2-spring-2-jpa-ajax.html, there is a method in ‘package quickstart.service’ called ‘findall()’ that looks like this:

    @SuppressWarnings(“unchecked”)
    public List<Person> findAll() {
    Query query = getEntityManager().createQuery(“select p FROM Person p”);
    return query.getResultList();

    My question is what syntax do I use to call ‘findall()’?

    Sorry to frustrate you (more than myself).

    #320867 Reply

    cconway
    Member

    Sorry, I’m not a Struts programmer so I don’t know if the problem is something I just don’t understand about Struts, or I’m just not understanding the question. I apologize if that comes across as frustration, it’s not meant to.

    If you search that tutorial for the word findAll, you’ll see down further where they call it from a Struts Action named PersonAction. At a high level, they pass the service into the constructor of the Action and store a reference to it. Then in the action’s execute() method, they just call service.findAll();

    The relevant pieces are:

    public class PersonAction implements Preparable {
    private PersonService service; <<<<< The service reference
    private List<Person> persons;
    private Person person;
    private Integer id;

    public PersonAction(PersonService service) {
    this.service = service; <<<<<<<< Store the reference in the Action constructor.
    }

    public String execute() {
    this.persons = service.findAll(); <<<<<< Call the method assigning the result to a member variable of type: List<Person>
    return Action.SUCCESS;
    }

    #320907 Reply

    ssquire
    Member

    Thanks Cindy – I got it figured out! I appreciate your patience and help!

    Got it working!

Viewing 7 posts - 1 through 7 (of 7 total)
Reply To: Examples for use of generated Spring Scaffolded Named Query

You must be logged in to post in the forum log in