Query an Entity as Query a Table : EJB Query Language « JPA « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » JPA » EJB Query Language 
22.25.6.Query an Entity as Query a TablePrevious/Next

File: Student.java

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Student {
  @Id
  private long id = 0;

  private String name;

  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String toString() {
    return "\n\nID:" + id + "\nName:" + name + "\n\n";
  }
}

File: Helper.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

public class Helper {
  public static void checkData() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    Connection conn = DriverManager.getConnection("jdbc:hsqldb:data/tutorial""sa""");
    Statement st = conn.createStatement();

    ResultSet mrs = conn.getMetaData().getTables(null, null, null, new String[] { "TABLE" });
    while (mrs.next()) {
      String tableName = mrs.getString(3);
      System.out.println("\n\n\n\nTable Name: "+ tableName);

      ResultSet rs = st.executeQuery("select * from " + tableName);
      ResultSetMetaData metadata = rs.getMetaData();
      while (rs.next()) {
        System.out.println(" Row:");
        for (int i = 0; i < metadata.getColumnCount(); i++) {
          System.out.println("    Column Name: "+ metadata.getColumnLabel(i + 1)",  ");
          System.out.println("    Column Type: "+ metadata.getColumnTypeName(i + 1)":  ");
          Object value = rs.getObject(i + 1);
          System.out.println("    Column Value: "+value+"\n");
        }
      }
    }
  }
}

File: Main.java

import java.util.Collection;
import java.util.List;

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

public class Main {
  static EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAService");
  static EntityManager em = emf.createEntityManager();

  public static void main(String[] athrows Exception {
    em.getTransaction().begin();
    
    
    Student student = new Student();
    student.setId(1);
    student.setName("Joe");
    
    em.persist(student);
    em.flush();
    em.getTransaction().commit();

    
    Query query = em.createQuery("SELECT e FROM Student e");
    List<Student> list = (List<Student>query.getResultList();
    System.out.println(list);
    
    
    em.close();
    emf.close();
    
    Helper.checkData();
  }
}

File: persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence" version="1.0">
  <persistence-unit name="JPAService" transaction-type="RESOURCE_LOCAL">
    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
      <property name="hibernate.hbm2ddl.auto" value="update"/>
      <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
      <property name="hibernate.connection.username" value="sa"/>
      <property name="hibernate.connection.password" value=""/>
      <property name="hibernate.connection.url" value="jdbc:hsqldb:data/tutorial"/>
    </properties>
  </persistence-unit>
</persistence>
  Download:  JPA-QueryAnEntityAsQueryATable.zip( 5,287 k)
22.25.EJB Query Language
22.25.1.EJB QL Get Two Properties From Entity
22.25.2.EJB QL Get Single result
22.25.3.EJB QL Get Field From Entity
22.25.4.EJB QL Get Entity From Entity
22.25.5.EJB QL Get Distinct Entity
22.25.6.Query an Entity as Query a Table
22.25.7.Return List Of Entities From Query
22.25.8.Query Flush Mode
22.25.9.EJB SQL Count Function
22.25.10.Like statement: Wild Card Escape
22.25.11.Like statement and Wild Card
22.25.12.Where Clause With SubQuery
22.25.13.Use Between To check Salary Range
22.25.14.Two Aggregate Functions
22.25.15.Three Level Field Reference
22.25.16.Using SubQuery in EJB Query Langauge
22.25.17.Size function
22.25.18.Range Check With Comparison Operator
22.25.19.Query Field Of Field
22.25.20.EJB QL Pass Entity As Parameter
22.25.21.Query with Parameters
22.25.22.EJB QL Order By Two Fields
22.25.23.Query One To Many Collection
22.25.24.Order Descending
22.25.25.Use Object Function
22.25.26.Not In function
22.25.27.EJB Query Language: Not Exist
22.25.28.EJB QL Max With Date Type
22.25.29.EJB QL Left Join One To Many Map
22.25.30.EJB QL Join Two Entities
22.25.31.EJB QL Join Three Entities
22.25.32.EJB QL Join One To One Return Two Entities
22.25.33.EJB QL Join One To One
22.25.34.EJB QL Join One To Many Map
22.25.35.EJB QL Join One to Many Left Join Fetch
22.25.36.EJB QL Join Many To Many
22.25.37.EJB QL Join Entity And Its Field
22.25.38.EJB QL is Field empty
22.25.39.EJB QL In With Subquery
22.25.40.EJB QL In Function With One To Many
22.25.41.EJB QL In Function
22.25.42.EJB QL Having Clause
22.25.43.EJB QL Exist And Subquery
22.25.44.EJB QL Create new Object In Select Statement
22.25.45.EJB QL Count Entity
22.25.46.EJB QL Count distinct
22.25.47.EJB QL Concat Function Substring
22.25.48.EJB QL Between With Date Type Value
22.25.49.Aggregate function: AVG
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.