Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.67 KB | None | 0 0
  1. package pl.polsl.zti.db1.dao;
  2.  
  3. import java.util.Date;
  4. import javax.persistence.EntityManager;
  5. import javax.persistence.EntityManagerFactory;
  6. import javax.persistence.EntityTransaction;
  7. import javax.persistence.Persistence;
  8. import pl.polsl.zti.db1.ConfigConsts;
  9. import pl.polsl.zti.db1.domain.Client;
  10. import pl.polsl.zti.db1.domain.Order;
  11. import pl.polsl.zti.db1.util.DbSchemaDef;
  12. import pl.polsl.zti.db1.util.JdbcUtils;
  13.  
  14. import org.junit.Before;
  15. import org.junit.BeforeClass;
  16. import org.junit.Test;
  17. import static org.junit.Assert.*;
  18.  
  19. public class ClientOrdersTest {
  20.  
  21. @BeforeClass
  22. public static void oneTimeSetUp() {
  23. }
  24.  
  25. @Before
  26. public void setUp() {
  27. JdbcUtils.restoreDbSchema(new DbSchemaDef());
  28. }
  29.  
  30. @Test
  31. public void testAddClientOrder() {
  32. //fail("Odkomentować poniższy kod w trzeciej części ćwiczenia.");
  33.  
  34. final EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(ConfigConsts.PERSISTANCE_UNIT_NAME);
  35. final EntityManager entityManager = entityManagerFactory.createEntityManager();
  36.  
  37. final EntityTransaction transaction = entityManager.getTransaction();
  38. transaction.begin();
  39.  
  40. Client client = new Client();
  41. client.setName("olo");
  42. Order order = new Order();
  43. order.setDescription("aaaa");
  44. order.setDate(new Date());
  45. order.setNo("111");
  46. order.setClient(client);
  47. client.getOrders().add(order);
  48. entityManager.persist(client);
  49. Client c = entityManager.find(Client.class, 1);
  50. for(Order o:c.getOrders())
  51. System.out.println(o.getDescription());
  52.  
  53. transaction.commit();
  54. entityManager.close();
  55.  
  56. long countClients = (Long) JdbcUtils.executeScalar("SELECT COUNT(*) FROM CLIENTS WHERE NAME='olo'");
  57. long countOrders = (Long) JdbcUtils.executeScalar("SELECT COUNT(*) FROM ORDERS WHERE ORDER_NO='111'");
  58.  
  59. assertEquals("Klient nie został dodany", 1, countClients);
  60. assertEquals("Zamówienie nie zostało dodane", 1, countOrders);
  61. }
  62. }
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. ---------4 - - 1---------_-------
  70.  
  71.  
  72.  
  73. package pl.polsl.zti.db1.domain;
  74.  
  75. import java.io.Serializable;
  76. import java.util.Date;
  77.  
  78. import javax.persistence.Column;
  79. import javax.persistence.Entity;
  80. import javax.persistence.FetchType;
  81. import javax.persistence.GeneratedValue;
  82. import javax.persistence.GenerationType;
  83. import javax.persistence.Id;
  84. import javax.persistence.JoinColumn;
  85. import javax.persistence.ManyToOne;
  86. import javax.persistence.Table;
  87. import javax.persistence.Temporal;
  88. import javax.persistence.TemporalType;
  89.  
  90. @Entity
  91. @Table(name="ORDERS")
  92. public class Order implements Serializable {
  93.  
  94. private static final long serialVersionUID = 1L;
  95. private int id;
  96. private String no;
  97. private Client client;
  98. private Date date;
  99. private String description;
  100.  
  101. @Id
  102. @GeneratedValue(strategy = GenerationType.IDENTITY)
  103. public int getId() {
  104. return id;
  105. }
  106.  
  107. public void setId(int id) {
  108. this.id = id;
  109. }
  110.  
  111. @Column(name = "ORDER_NO")
  112. public String getNo() {
  113. return no;
  114. }
  115.  
  116. public void setNo(String no) {
  117. this.no = no;
  118. }
  119.  
  120. @ManyToOne (fetch = FetchType.LAZY)
  121. @JoinColumn(name = "CLIENT_ID", nullable=false)
  122. public Client getClient() {
  123. return client;
  124. }
  125.  
  126. public void setClient(Client client) {
  127. this.client = client;
  128. }
  129.  
  130. @Column(name = "ORDER_DATE")
  131. @Temporal(TemporalType.DATE)
  132. public Date getDate() {
  133. return date;
  134. }
  135.  
  136. public void setDate(Date date) {
  137. this.date = date;
  138. }
  139.  
  140. @Column(name = "ORDER_DESC")
  141. public String getDescription() {
  142. return description;
  143. }
  144.  
  145. public void setDescription(String description) {
  146. this.description = description;
  147. }
  148. }
  149.  
  150.  
  151.  
  152. 11175/5%1=2+2/54/
  153.  
  154.  
  155.  
  156. package pl.polsl.zti.db1.domain;
  157.  
  158. import java.io.Serializable;
  159. import java.util.HashSet;
  160. import java.util.Set;
  161.  
  162. import javax.persistence.CascadeType;
  163. import javax.persistence.Column;
  164. import javax.persistence.Entity;
  165. import javax.persistence.FetchType;
  166. import javax.persistence.GeneratedValue;
  167. import javax.persistence.GenerationType;
  168. import javax.persistence.Id;
  169. import javax.persistence.OneToMany;
  170. import javax.persistence.Table;
  171. @Entity
  172. @Table (name="clients")
  173. public class Client implements Serializable {
  174.  
  175. private static final long serialVersionUID = 1L;
  176. private int id;
  177. private int no;
  178. private Set<Order> orders = new HashSet<Order>(0);
  179. private String name;
  180. private String ssn;
  181. private String address;
  182.  
  183. @Id
  184. @GeneratedValue (strategy = GenerationType.IDENTITY)
  185. public int getId() {
  186. return this.id;
  187. }
  188.  
  189. public void setId(int id) {
  190. this.id = id;
  191. }
  192.  
  193. @Column (name="client_no")
  194. public int getNo() {
  195. return this.no;
  196. }
  197.  
  198. public void setNo(int no) {
  199. this.no = no;
  200. }
  201.  
  202. public String getName() {
  203. return this.name;
  204. }
  205.  
  206. public void setName(String name) {
  207. this.name = name;
  208. }
  209.  
  210. public String getSsn() {
  211. return this.ssn;
  212. }
  213.  
  214. public void setSsn(String ssn) {
  215. this.ssn = ssn;
  216. }
  217.  
  218. public String getAddress() {
  219. return this.address;
  220. }
  221.  
  222. public void setAddress(String address) {
  223. this.address = address;
  224. }
  225. @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "client")
  226. public Set<Order> getOrders() {
  227. return this.orders;
  228. }
  229. public void setOrders(Set<Order> orders) {
  230. this.orders = orders;
  231. }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement