Total members 11893 |It is currently Thu Nov 14, 2024 6:28 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Following code show you how to create a named query within your code (Not at orm.xml when u r are working on application server ) , Am creating here a named query using enhanced query or what it is named EJB-QL .
Code:

package com
.codemiles.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public abstract class JPAService {
    private EntityManager entityManager;

    /**
     * @param args
     */
    public void showTopics() {
    EntityManagerFactory factory = Persistence
            
.createEntityManagerFactory("persistenceUnitName");
    entityManager = factory.createEntityManager();
    entityManager.getTransaction().begin();

    List<Topic> list = loadAllTopics();
    if (list != null) {
        for (Topic currentTopic : list) {
        System.out.println("Topic id#(" + currentTopic.getId()
                + " ). Title = " + currentTopic.getTitle()
                + " , Content = " + currentTopic.getContent());
        }
    }
 
    entityManager.getTransaction().commit();
     entityManager.close();
     factory.close();

    }

    public List<Topic> loadAllTopics() {
    List<Topic> resultList = entityManager.createQuery(
            "SELECT t FROM Topic t").getResultList();

    return resultList;
    }

    public Topic loadAllTopicsById(int id) {
    return (Topic) entityManager.createQuery(
            "SELECT t FROM Topic t.id=" + id).getSingleResult();
    }
}
 




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : create named query
 create a named query within entity class     -  
 Named Native Query     -  
 Create auto numbered column in mysql query     -  
 How are Java source code files named     -  
 EJB-QL IN where query     -  
 SQL LIKE query Command     -  
 using parameter with name query     -  
 query about execution     -  
 more than one parameter with name query     -  
 Query about jsp and printer     -  



cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com