Guest User

Untitled

a guest
Feb 15th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
  3. <persistence-unit name="primary">
  4. <jta-data-source>java:jboss/dataDS</jta-data-source>
  5. <properties>
  6. <property name="hibernate.hbm2ddl.auto" value="update"/>
  7. <property name="hibernate.show_sql" value="false"/>
  8. <property name="hibernate.format_sql" value="false"/>
  9. </properties>
  10. </persistence-unit>
  11. </persistence>
  12.  
  13. <?xml version="1.0"?>
  14. <datasources xmlns="http://www.jboss.org/ironjacamar/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  15. <datasource jndi-name="java:jboss/dataDS" pool-name="PostgreSQLPool">
  16. <connection-url>jdbc:postgresql://localhost:5432/itsfortest</connection-url>
  17. <driver>postgresql-42.2.1.jar</driver>
  18. <driver-class>org.postgresql.Driver</driver-class>
  19. <pool>
  20. <max-pool-size>30</max-pool-size>
  21. </pool>
  22. <security>
  23. <user-name>admin</user-name>
  24. <password></password>
  25. </security>
  26. </datasource>
  27. </datasources>
  28.  
  29. @Entity
  30.  
  31. @Id @GeneratedValue(strategy = GenerationType.AUTO)
  32. private long id;
  33. private String name;
  34. private int price;
  35.  
  36. @OneToMany(mappedBy = "goodsEntity")
  37. private List<GoodsInOrderEntity> goodsInOrderEntity;
  38.  
  39. public List<GoodsInOrderEntity> getGoodsInOrderEntity() {
  40. return goodsInOrderEntity;
  41. }
  42.  
  43. public void setGoodsInOrderEntity(List<GoodsInOrderEntity> goodsInOrderEntity) {
  44. this.goodsInOrderEntity = goodsInOrderEntity;
  45. }
  46.  
  47. public long getId() {
  48. return id;
  49. }
  50.  
  51. public void setId(int id) {
  52. this.id = id;
  53. }
  54.  
  55. public String getName() {
  56. return name;
  57. }
  58.  
  59. public void setName(String name) {
  60. this.name = name;
  61. }
  62.  
  63. public int getPrice() {
  64. return price;
  65. }
  66.  
  67. public void setPrice(int price) {
  68. this.price = price;
  69. }
  70.  
  71. @Id
  72. private long id;
  73.  
  74. @ManyToOne
  75. private GoodsEntity goodsEntity;
  76.  
  77. @ManyToOne
  78. private OrderEntity orderEntity;
  79.  
  80. public long getId() {
  81. return id;
  82. }
  83.  
  84. public void setId(int id) {
  85. this.id = id;
  86. }
  87.  
  88. public GoodsEntity getGoodsEntity() {
  89. return goodsEntity;
  90. }
  91.  
  92. public void setGoodsEntity(GoodsEntity goodsEntity) {
  93. this.goodsEntity = goodsEntity;
  94. }
  95.  
  96. public OrderEntity getOrderEntity() {
  97. return orderEntity;
  98. }
  99.  
  100. public void setOrderEntity(OrderEntity orderEntity) {
  101. this.orderEntity = orderEntity;
  102. }
  103.  
  104. @Entity
  105.  
  106. @Id
  107. @GeneratedValue(strategy = GenerationType.AUTO)
  108. private long id;
  109. private int summaryPrice;
  110.  
  111. @OneToMany(mappedBy = "orderEntity")
  112. private List<GoodsInOrderEntity> goodsInOrderEntity;
  113.  
  114. public List<GoodsInOrderEntity> getGoodsInOrderEntity() {
  115. return goodsInOrderEntity;
  116. }
  117.  
  118. public void setGoodsInOrderEntity(List<GoodsInOrderEntity> goodsInOrderEntity) {
  119. this.goodsInOrderEntity = goodsInOrderEntity;
  120. }
  121.  
  122. public long getId() {
  123. return id;
  124. }
  125.  
  126. public void setId(int id) {
  127. this.id = id;
  128. }
  129.  
  130. public int getSummaryPrice() {
  131. return summaryPrice;
  132. }
  133.  
  134. public void setSummaryPrice(int summaryPrice) {
  135. this.summaryPrice = summaryPrice;
  136. }
  137.  
  138. @Stateless
  139.  
  140. @PersistenceContext(unitName = "primary")
  141. private
  142. EntityManager entityManager;
  143.  
  144. public GoodsEntity creatingThing(String name, int price){
  145. GoodsEntity goodsEntity = entityManager.find(GoodsEntity.class, name);
  146.  
  147. goodsEntity.setName(name);
  148. goodsEntity.setPrice(price);
  149. entityManager.persist(goodsEntity);
  150.  
  151. return goodsEntity;
  152. }
  153.  
  154.  
  155. public List<GoodsEntity> getGoods(){
  156. TypedQuery<GoodsEntity> query = entityManager.createQuery("SELECT c FROM GoodsEntity c", GoodsEntity.class);
  157. return query.getResultList();
  158. }
  159.  
  160. @Stateless
  161.  
  162. @PersistenceContext(unitName = "primary")
  163. EntityManager entityManager;
  164.  
  165.  
  166. public OrderEntity creatingOrder(){
  167. OrderEntity orderEntity = new OrderEntity();
  168.  
  169. entityManager.persist(orderEntity);
  170. return orderEntity;
  171. }
  172.  
  173. public boolean addingToOrder(long orderId, long thingId){
  174.  
  175. OrderEntity orderEntity = entityManager.find(OrderEntity.class, orderId);
  176. GoodsEntity goodsEntity = entityManager.find(GoodsEntity.class, thingId);
  177.  
  178. if (orderEntity==null||goodsEntity==null){
  179. return false;
  180. }
  181.  
  182. GoodsInOrderEntity goodsInOrderEntity = new GoodsInOrderEntity();
  183. goodsInOrderEntity.setOrderEntity(orderEntity);
  184. goodsInOrderEntity.setGoodsEntity(goodsEntity);
  185.  
  186. entityManager.persist(goodsInOrderEntity);
  187.  
  188. return true;
  189. }
  190.  
  191. public List<GoodsEntity> getGoodsInOrder(long orderId){
  192. OrderEntity orderEntity = entityManager.find(OrderEntity.class, orderId);
  193.  
  194. if(orderEntity==null){
  195. return Collections.emptyList();
  196. }
  197.  
  198. List <GoodsInOrderEntity> goodsInOrderEntity = orderEntity.getGoodsInOrderEntity();
  199. List<GoodsEntity> result = new ArrayList<>();
  200. for (GoodsInOrderEntity goods :
  201. goodsInOrderEntity) {
  202. result.add(goods.getGoodsEntity());
  203. }
  204. return result;
  205. }
  206.  
  207. <?xml version="1.0" encoding="UTF-8"?>
  208. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  209. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  210. <html xmlns="http://www.w3.org/1999/xhtml"
  211. xmlns:h="http://xmlns.jcp.org/jsf/html"
  212. xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
  213. xmlns:f="http://xmlns.jcp.org/jsf/core">
  214. <h:head>Shop</h:head>
  215. <h:body>
  216.  
  217. <h:form>
  218. <table>
  219. <tr>
  220. <td><h:inputText value="#{managingGoodsCDI.name}"/></td>
  221. <td><h:inputText value="#{managingGoodsCDI.price}"/></td>
  222. </tr>
  223. <tr>
  224. <td>
  225. Наименование
  226. </td>
  227. <td>
  228. Цена
  229. </td>
  230. </tr>
  231. <tr>
  232. <td>
  233. <h:commandButton value="Создать товар" action="#{managingGoodsCDI.createThing}"/>
  234. </td>
  235. <td>
  236. <h:commandButton value="Создать заказ" action="#{managingGoodsCDI.createOrder}"/>
  237. </td>
  238. </tr>
  239. </table>
  240. Созданные товары
  241. <h:dataTable value="#{managingGoodsCDI.goods}" var="Thing">
  242. <h:column>
  243. <h:outputText value="#{Thing.name}"/>
  244. </h:column>
  245. <h:column>
  246. <h:outputText value="#{Thing.price}"/>
  247. </h:column>
  248. <h:column>
  249. <h:commandButton value="Добавить" action="#
  250. {managingGoodsCDI.addingToOrder(Thing)}"/>
  251. </h:column>
  252. </h:dataTable>
  253. Добавленные товары
  254.  
  255. <h:dataTable value="#{managingGoodsCDI.goodsInOrder}" var="Thing">
  256. <h:column>
  257. <h:outputText value="#{Thing.name}"/>
  258. </h:column>
  259. <h:column>
  260. <h:outputText value="#{Thing.price}"/>
  261. </h:column>
  262. </h:dataTable>
  263. </h:form>
  264. </h:body>
  265. </html>
Add Comment
Please, Sign In to add comment