- This topic has 6 replies, 2 voices, and was last updated 13 years ago by ssquire.
-
AuthorPosts
-
ssquireMemberI 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.
cconwayMemberTake 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?
ssquireMemberBear 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.
cconwayMemberSorry, 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
ssquireMemberThanks 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).
cconwayMemberSorry, 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;
}
ssquireMemberThanks Cindy – I got it figured out! I appreciate your patience and help!
Got it working!
-
AuthorPosts