| /* |
| * Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved. |
| * |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v. 2.0, which is available at |
| * http://www.eclipse.org/legal/epl-2.0. |
| * |
| * This Source Code may also be made available under the following Secondary |
| * Licenses when the conditions for such availability set forth in the |
| * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| * version 2 with the GNU Classpath Exception, which is available at |
| * https://www.gnu.org/software/classpath/license.html. |
| * |
| * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| */ |
| |
| package org.glassfish.tests.ejb.sample; |
| |
| import java.util.Collection; |
| import java.util.Date; |
| |
| import javax.ejb.Stateless; |
| |
| import javax.persistence.PersistenceContext; |
| import javax.persistence.EntityManager; |
| import javax.persistence.Query; |
| |
| /** |
| * @author Jerome Dochez |
| */ |
| @Stateless |
| public class SimpleEjb extends SimpleBase { |
| |
| @PersistenceContext(unitName="test") EntityManager em; |
| |
| public String testJPA() { |
| String result = null; |
| Query q = em.createNamedQuery("SimpleEntity.findAll"); |
| Collection entities = q.getResultList(); |
| int s = entities.size(); |
| for (Object o : entities) { |
| SimpleEntity se = (SimpleEntity)o; |
| SimpleRelated sr = se.getRelated(); |
| System.out.println("Found related: " + ((sr == null)? sr : sr.getName())); |
| } |
| |
| if (s < 10) { |
| System.out.println("Record # " + (s + 1)); |
| SimpleEntity e = new SimpleEntity("Entity number " + (s + 1) + " created at " + new Date()); |
| SimpleRelated r = new SimpleRelated("Related to " + (s + 1)); |
| e.setRelated(r); |
| r.setEntity(e); |
| em.persist(e); |
| result = "Entity number " + (s + 1); |
| } else { |
| result = "10 entities created"; |
| } |
| return result; |
| |
| } |
| } |