Java Persistence API Named Query Demo
src\META-INF\persistence.xml
=========================
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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_2_0.xsd">
<persistence-unit name="jcrud">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.xyz.bo.Customer</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test_db"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="root1"/>
<!--
First time comment next line and un comment this line
as the following configuration creates table and inserts data into table, To you have select 1 option
-->
<!--<property name="hibernate.hbm2ddl.auto" value="create"/>
-->
<!--
Second time on time the following configuration updates or selects data from
table. So comment aforesaid line configuration
-->
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
src\CustomerMain.java
==================
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import com.xyz.bo.Customer;
import com.xyz.service.CustomerService;
/**
* @author Pedababu M
*
*/
public class CustomerMain {
public static void main(String args[]) {
CustomerService customerService = new CustomerService();
System.out.println("Choose an option");
System.out.println("1. Add\n 2.Get All Customers");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
switch (choice) {
case 1:
Customer customer = new Customer();
//System.out.println("Enter the customer id");
//customer.setCustomerId(scanner.nextInt());
System.out.println("Enter the customer name");
customer.setCustomerName(scanner.next());
System.out.println("Enter the customer city");
customer.setCity(scanner.next());
customerService.addCustomer(customer);
break;
case 2:
System.out.println("Enter the customer id");
List customerList = null;
customerList = customerService.getAllCustomers();
Iterator iterator = customerList.iterator();
while (iterator.hasNext()) {
Object[] customer1 = (Object[])iterator.next();
System.out.println("\nCustomer Name :"
+ customer1[0] + "\n Customer ID :"
+ customer1[1] + "\n Customer City :" + customer1[2]);
}
}
}
}
src\com\xyz\bo\Customer.java
======================
package com.xyz.bo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
/**
* @author Pedababu M
*
*/
@Entity
@Table(name="jpa_customer")
@NamedQuery(name="serchByCity", query="select customer.customerId,customer.customerName,customer.city from Customer customer")
public class Customer {
//JSR220 - JPA persistance
@Id
@Column(name="customer_id")
@GeneratedValue(strategy=GenerationType.AUTO)
private int customerId;
@Column(name="customer_name")
private String customerName;
@Column(name="customer_city")
private String city;
/**
* @return the customerId
*/
public int getCustomerId() {
return customerId;
}
/**
* @param customerId the customerId to set
*/
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
/**
* @return the customerName
*/
public String getCustomerName() {
return customerName;
}
/**
* @param customerName the customerName to set
*/
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
/**
* @return the city
*/
public String getCity() {
return city;
}
/**
* @param city the city to set
*/
public void setCity(String city) {
this.city = city;
}
}
src\com\xyz\dao\CustomerDao.java
==========================
package com.xyz.dao;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import com.xyz.bo.Customer;
/**
* @author Pedababu M
*
*/
public class CustomerDao {
public void addCustomer(Customer customer) {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("jcrud");
EntityManager entityManager = entityManagerFactory.createEntityManager();
EntityTransaction entityTransaction = entityManager.getTransaction();
try {
entityTransaction.begin();
entityManager.persist(customer);
entityTransaction.commit();
} catch(Exception exception){
entityTransaction.rollback();
exception.printStackTrace();
}
finally {
entityManager.close();
}
}
public List getAllCustomers() {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("jcrud");
EntityManager entityManager = entityManagerFactory.createEntityManager();
EntityTransaction entityTransaction = entityManager.getTransaction();
Customer customer = null;
List customerList = null;
try {
entityTransaction.begin();
Query query =entityManager.createNamedQuery("serchByCity");
//query.setParameter(1, "bangalore");
//TypedQuery<Customer> query = entityManager.createQuery(" select customer from Customer customer", Customer.class);
customerList = query.getResultList();
entityTransaction.commit();
} catch(Exception exception){
entityTransaction.rollback();
exception.printStackTrace();
}
finally {
entityManager.close();
}
return customerList;
}
}
src\com\xyz\service\CustomerService.java
===================================
package com.xyz.service;
import java.util.List;
import com.xyz.bo.Customer;
import com.xyz.dao.CustomerDao;
/**
* @author Pedababu M
*
*/
public class CustomerService {
private CustomerDao customerDao = null;
/**
* @return the customerDao
*/
public CustomerDao getCustomerDao() {
if (customerDao == null) {
customerDao = new CustomerDao();
}
return customerDao;
}
public void addCustomer(Customer customer){
getCustomerDao().addCustomer(customer);
}
public List getAllCustomers(){
List customerList =getCustomerDao().getAllCustomers();
return customerList;
}
}
No comments:
Post a Comment