Guest User

Untitled

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