/jboss-as-7.1.1.Final/testsuite/integration/basic/src/test/java/org/jboss/as/test/integration/jpa/hibernate/EntityTest.java
Java | 161 lines | 92 code | 41 blank | 28 comment | 0 complexity | 3fa58d2bcd0d321cf6923a18a7fa064e MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
1/*
2 * JBoss, Home of Professional Open Source.
3 * Copyright 2011, Red Hat, Inc., and individual contributors
4 * as indicated by the @author tags. See the copyright.txt file in the
5 * distribution for a full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */
22
23package org.jboss.as.test.integration.jpa.hibernate;
24
25import java.util.HashSet;
26import java.util.Set;
27
28import javax.ejb.Stateful;
29import javax.persistence.PersistenceContext;
30import javax.persistence.PersistenceContextType;
31
32import org.hibernate.Session;
33import org.jboss.as.test.integration.jpa.hibernate.entity.Company;
34import org.jboss.as.test.integration.jpa.hibernate.entity.Customer;
35import org.jboss.as.test.integration.jpa.hibernate.entity.Flight;
36import org.jboss.as.test.integration.jpa.hibernate.entity.Ticket;
37
38/**
39 * Stateful session bean for testing Hibernate entities:
40 * One to Many, Many to One, Many to Many mapping and named queries.
41 *
42 * @author Zbyněk Roubal?k
43 */
44@Stateful
45public class EntityTest {
46
47 @PersistenceContext(unitName = "entity_pc", type = PersistenceContextType.EXTENDED)
48 private Session session;
49
50 public Customer oneToManyCreate() throws Exception {
51 Ticket t = new Ticket();
52 t.setNumber("111");
53
54 Customer c = new Customer();
55 Set<Ticket> tickets = new HashSet<Ticket>();
56
57 tickets.add(t);
58 t.setCustomer(c);
59 c.setTickets(tickets);
60
61 session.save(c);
62
63 return c;
64 }
65
66 public Flight manyToOneCreate() throws Exception {
67 Flight f = new Flight();
68 f.setName("Flight number one");
69 f.setId(new Long(1));
70
71 Company comp = new Company();
72 comp.setName("Airline 1");
73 f.setCompany(comp);
74
75 session.save(f);
76
77 return f;
78 }
79
80 public void manyToManyCreate() throws Exception {
81
82 Flight f1 = findFlightById(new Long(1));
83 Flight f2 = new Flight();
84
85 f2.setId(new Long(2));
86 f2.setName("Flight two");
87
88 Company us = new Company();
89 us.setName("Airline 2");
90 f2.setCompany(us);
91
92 Set<Customer> customers1 = new HashSet<Customer>();
93 Set<Customer> customers2 = new HashSet<Customer>();
94
95 Customer c1 = new Customer();
96 c1.setName("John");
97 customers1.add(c1);
98
99 Customer c2 = new Customer();
100 c2.setName("Tom");
101 customers2.add(c2);
102
103 Customer c3 = new Customer();
104 c3.setName("Pete");
105 customers2.add(c3);
106
107 f1.setCustomers(customers1);
108 f2.setCustomers(customers2);
109
110 session.save(c1);
111 session.save(c2);
112 session.save(c3);
113 session.save(f2);
114 }
115
116 public int testAllCustomersQuery() {
117
118 //session.flush();
119 return session.getNamedQuery("allCustomers").list().size();
120 }
121
122 public Customer testCustomerByIdQuery() {
123 Customer c = new Customer();
124 c.setName("Peter");
125
126 session.save(c);
127 session.flush();
128
129 return (Customer) session.getNamedQuery("customerById").setParameter("id", c.getId()).uniqueResult();
130
131 }
132
133 public Customer createCustomer(String name) {
134 Customer c = new Customer();
135 c.setName(name);
136 session.save(c);
137 return c;
138 }
139
140 public void changeCustomer(Long id, String name) {
141 Customer c = (Customer) session.load(Customer.class, id);
142 c.setName(name);
143 }
144
145
146 public Flight findFlightById(Long id) {
147
148 return (Flight) session.load(Flight.class, id);
149 }
150
151 public Company findCompanyById(Long id) {
152
153 return (Company) session.load(Company.class, id);
154 }
155
156 public Customer findCustomerById(Long id) {
157
158 return (Customer) session.load(Customer.class, id);
159 }
160
161}