package util;
import com.example.ws.Book;
import com.example.ws.Library;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
/**
*
* @author hilal.tarakci
*/
public class HibernateUtil {
@PersistenceUnit(unitName = "HibernateLazyInitFixPU")
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
if (emf == null) {
emf = Persistence.createEntityManagerFactory("HibernateLazyInitFixPU");
}
return emf.createEntityManager();
}
public void create(Library item) throws Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(item);
em.getTransaction().commit();
} catch (Exception ex) {
try {
if (em != null) {
em.getTransaction().rollback();
}
} catch (Exception re) {
re.printStackTrace();
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public List retrieveRecordsOfBook() {
EntityManager em = getEntityManager();
try {
Query q = em.createQuery("select object(o) from Book as o");
List bookList = new ArrayList();
List tempBookList = q.getResultList();
for (Book tempBook : tempBookList) {
Book book = new Book();
book.setId(tempBook.getId());
book.setTitle(tempBook.getTitle());
bookList.add(book);
}
return bookList;
} finally {
em.close();
}
}
public List retrieveRecordsOfLibrary() {
EntityManager em = getEntityManager();
try {
Query q = em.createQuery("select object(o) from Library as o");
List libList = new ArrayList();
List tempLibList = q.getResultList();
for (Library tempLib : tempLibList) {
Library lib = new Library();
lib.setId(tempLib.getId());
lib.setName(tempLib.getName());
List bookList = new ArrayList();
for(Book tempBook: tempLib.getBookList()) {
Book book = new Book();
book.setId(tempBook.getId());
book.setTitle(tempBook.getTitle());
bookList.add(book);
}
lib.setBookList(bookList);
libList.add(lib);
}
return libList;
} finally {
em.close();
}
}
}
package com.example.ws.service;
import com.example.ws.Book;
import com.example.ws.Library;
import java.util.List;
import javax.jws.Oneway;
import javax.jws.WebMethod;
import javax.jws.WebService;
import util.HibernateUtil;
@WebService()
public class HibernateLazyInit {
private HibernateUtil util = new HibernateUtil();
@WebMethod(operationName = "insertRecords")
@Oneway
public void insertRecords() {
try {
Book book1 = new Book();
book1.setTitle("A Good Story");
Book book2 = new Book();
book2.setTitle("A Bad Story");
Library library1 = new Library();
library1.setName("An Excellent Library");
library1.getBookList().add(book1);
library1.getBookList().add(book2);
util.create(library1);
} catch (Exception ex) {
ex.printStackTrace();
}
}
@WebMethod(operationName = "retrieveRecordsOfBook")
public List retrieveRecordsOfBook() {
return util.retrieveRecordsOfBook();
}
@WebMethod(operationName = "retrieveRecordsOfLibrary")
public List retrieveRecordsOfLibrary() {
return util.retrieveRecordsOfLibrary();
}
}
public class Main {
void insertRecords() {
try { // Call Web Service Operation
com.example.ws.service.HibernateLazyInitService service = new com.example.ws.service.HibernateLazyInitService();
com.example.ws.service.HibernateLazyInit port = service.getHibernateLazyInitPort();
port.insertRecords();
} catch (Exception ex) {
// TODO handle custom exceptions here
ex.printStackTrace();
}
}
int retrieveRecordsOfBook() {
try { // Call Web Service Operation
com.example.ws.service.HibernateLazyInitService service = new com.example.ws.service.HibernateLazyInitService();
com.example.ws.service.HibernateLazyInit port = service.getHibernateLazyInitPort();
// TODO process result here
java.util.List result = port.retrieveRecordsOfBook();
System.out.println("Result = "+result);
return result.size();
} catch (Exception ex) {
// TODO handle custom exceptions here
ex.printStackTrace();
return 0;
}
}
int retrieveRecordsOfLibrary() {
try { // Call Web Service Operation
com.example.ws.service.HibernateLazyInitService service = new com.example.ws.service.HibernateLazyInitService();
com.example.ws.service.HibernateLazyInit port = service.getHibernateLazyInitPort();
// TODO process result here
java.util.List result = port.retrieveRecordsOfLibrary();
System.out.println("Result = "+result);
return result.size();
} catch (Exception ex) {
// TODO handle custom exceptions here
ex.printStackTrace();
return 0;
}
}
public static void main(String[] args) {
// TODO code application logic here
Main client = new Main();
client.insertRecords();
System.out.println("Size of books : " + client.retrieveRecordsOfBook() );
System.out.println("Size of library : " + client.retrieveRecordsOfLibrary() );
}
btw, the ide is netbeans 6.5.1 with glassfish.