PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/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. package org.jboss.as.test.integration.jpa.hibernate;
  23. import java.util.HashSet;
  24. import java.util.Set;
  25. import javax.ejb.Stateful;
  26. import javax.persistence.PersistenceContext;
  27. import javax.persistence.PersistenceContextType;
  28. import org.hibernate.Session;
  29. import org.jboss.as.test.integration.jpa.hibernate.entity.Company;
  30. import org.jboss.as.test.integration.jpa.hibernate.entity.Customer;
  31. import org.jboss.as.test.integration.jpa.hibernate.entity.Flight;
  32. import org.jboss.as.test.integration.jpa.hibernate.entity.Ticket;
  33. /**
  34. * Stateful session bean for testing Hibernate entities:
  35. * One to Many, Many to One, Many to Many mapping and named queries.
  36. *
  37. * @author ZbynÄ›k Roubal?­k
  38. */
  39. @Stateful
  40. public class EntityTest {
  41. @PersistenceContext(unitName = "entity_pc", type = PersistenceContextType.EXTENDED)
  42. private Session session;
  43. public Customer oneToManyCreate() throws Exception {
  44. Ticket t = new Ticket();
  45. t.setNumber("111");
  46. Customer c = new Customer();
  47. Set<Ticket> tickets = new HashSet<Ticket>();
  48. tickets.add(t);
  49. t.setCustomer(c);
  50. c.setTickets(tickets);
  51. session.save(c);
  52. return c;
  53. }
  54. public Flight manyToOneCreate() throws Exception {
  55. Flight f = new Flight();
  56. f.setName("Flight number one");
  57. f.setId(new Long(1));
  58. Company comp = new Company();
  59. comp.setName("Airline 1");
  60. f.setCompany(comp);
  61. session.save(f);
  62. return f;
  63. }
  64. public void manyToManyCreate() throws Exception {
  65. Flight f1 = findFlightById(new Long(1));
  66. Flight f2 = new Flight();
  67. f2.setId(new Long(2));
  68. f2.setName("Flight two");
  69. Company us = new Company();
  70. us.setName("Airline 2");
  71. f2.setCompany(us);
  72. Set<Customer> customers1 = new HashSet<Customer>();
  73. Set<Customer> customers2 = new HashSet<Customer>();
  74. Customer c1 = new Customer();
  75. c1.setName("John");
  76. customers1.add(c1);
  77. Customer c2 = new Customer();
  78. c2.setName("Tom");
  79. customers2.add(c2);
  80. Customer c3 = new Customer();
  81. c3.setName("Pete");
  82. customers2.add(c3);
  83. f1.setCustomers(customers1);
  84. f2.setCustomers(customers2);
  85. session.save(c1);
  86. session.save(c2);
  87. session.save(c3);
  88. session.save(f2);
  89. }
  90. public int testAllCustomersQuery() {
  91. //session.flush();
  92. return session.getNamedQuery("allCustomers").list().size();
  93. }
  94. public Customer testCustomerByIdQuery() {
  95. Customer c = new Customer();
  96. c.setName("Peter");
  97. session.save(c);
  98. session.flush();
  99. return (Customer) session.getNamedQuery("customerById").setParameter("id", c.getId()).uniqueResult();
  100. }
  101. public Customer createCustomer(String name) {
  102. Customer c = new Customer();
  103. c.setName(name);
  104. session.save(c);
  105. return c;
  106. }
  107. public void changeCustomer(Long id, String name) {
  108. Customer c = (Customer) session.load(Customer.class, id);
  109. c.setName(name);
  110. }
  111. public Flight findFlightById(Long id) {
  112. return (Flight) session.load(Flight.class, id);
  113. }
  114. public Company findCompanyById(Long id) {
  115. return (Company) session.load(Company.class, id);
  116. }
  117. public Customer findCustomerById(Long id) {
  118. return (Customer) session.load(Customer.class, id);
  119. }
  120. }