Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0" encoding="UTF-8"?>
- <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
- <persistence-unit name="primary">
- <jta-data-source>java:jboss/dataDS</jta-data-source>
- <properties>
- <property name="hibernate.hbm2ddl.auto" value="update"/>
- <property name="hibernate.show_sql" value="false"/>
- <property name="hibernate.format_sql" value="false"/>
- </properties>
- </persistence-unit>
- </persistence>
- <?xml version="1.0"?>
- <datasources xmlns="http://www.jboss.org/ironjacamar/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <datasource jndi-name="java:jboss/dataDS" pool-name="PostgreSQLPool">
- <connection-url>jdbc:postgresql://localhost:5432/itsfortest</connection-url>
- <driver>postgresql-42.2.1.jar</driver>
- <driver-class>org.postgresql.Driver</driver-class>
- <pool>
- <max-pool-size>30</max-pool-size>
- </pool>
- <security>
- <user-name>admin</user-name>
- <password></password>
- </security>
- </datasource>
- </datasources>
- @Entity
- public class GoodsEntity {
- @Id @GeneratedValue(strategy = GenerationType.AUTO)
- private long id;
- private String name;
- private int price;
- @OneToMany(mappedBy = "goodsEntity")
- private List<GoodsInOrderEntity> goodsInOrderEntity;
- public List<GoodsInOrderEntity> getGoodsInOrderEntity() {
- return goodsInOrderEntity;
- }
- public void setGoodsInOrderEntity(List<GoodsInOrderEntity> goodsInOrderEntity) {
- this.goodsInOrderEntity = goodsInOrderEntity;
- }
- public long getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getPrice() {
- return price;
- }
- public void setPrice(int price) {
- this.price = price;
- }
- }
- @Entity
- public class GoodsInOrderEntity {
- @Id
- private long id;
- @ManyToOne
- private GoodsEntity goodsEntity;
- @ManyToOne
- private OrderEntity orderEntity;
- public long getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public GoodsEntity getGoodsEntity() {
- return goodsEntity;
- }
- public void setGoodsEntity(GoodsEntity goodsEntity) {
- this.goodsEntity = goodsEntity;
- }
- public OrderEntity getOrderEntity() {
- return orderEntity;
- }
- public void setOrderEntity(OrderEntity orderEntity) {
- this.orderEntity = orderEntity;
- }
- }
- @Entity
- public class OrderEntity {
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- private long id;
- private int summaryPrice;
- @OneToMany(mappedBy = "orderEntity")
- private List<GoodsInOrderEntity> goodsInOrderEntity;
- public List<GoodsInOrderEntity> getGoodsInOrderEntity() {
- return goodsInOrderEntity;
- }
- public void setGoodsInOrderEntity(List<GoodsInOrderEntity> goodsInOrderEntity) {
- this.goodsInOrderEntity = goodsInOrderEntity;
- }
- public long getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public int getSummaryPrice() {
- return summaryPrice;
- }
- public void setSummaryPrice(int summaryPrice) {
- this.summaryPrice = summaryPrice;
- }
- }
- @Stateless
- @LocalBean
- public class ManagingGoodsEJB {
- @PersistenceContext(unitName = "primary")
- private
- EntityManager entityManager;
- public GoodsEntity creatingThing(String name, int price){
- GoodsEntity goodsEntity = entityManager.find(GoodsEntity.class, name);
- goodsEntity.setName(name);
- goodsEntity.setPrice(price);
- entityManager.persist(goodsEntity);
- return goodsEntity;
- }
- public List<GoodsEntity> getGoods(){
- TypedQuery<GoodsEntity> query = entityManager.createQuery("SELECT c FROM GoodsEntity c", GoodsEntity.class);
- return query.getResultList();
- }
- }
- @Stateless
- @LocalBean
- public class ManagingOrderEJB {
- @PersistenceContext(unitName = "primary")
- EntityManager entityManager;
- public OrderEntity creatingOrder(){
- OrderEntity orderEntity = new OrderEntity();
- entityManager.persist(orderEntity);
- return orderEntity;
- }
- public boolean addingToOrder(long orderId, long thingId){
- OrderEntity orderEntity = entityManager.find(OrderEntity.class, orderId);
- GoodsEntity goodsEntity = entityManager.find(GoodsEntity.class, thingId);
- if (orderEntity==null||goodsEntity==null){
- return false;
- }
- GoodsInOrderEntity goodsInOrderEntity = new GoodsInOrderEntity();
- goodsInOrderEntity.setOrderEntity(orderEntity);
- goodsInOrderEntity.setGoodsEntity(goodsEntity);
- entityManager.persist(goodsInOrderEntity);
- return true;
- }
- public List<GoodsEntity> getGoodsInOrder(long orderId){
- OrderEntity orderEntity = entityManager.find(OrderEntity.class, orderId);
- if(orderEntity==null){
- return Collections.emptyList();
- }
- List <GoodsInOrderEntity> goodsInOrderEntity = orderEntity.getGoodsInOrderEntity();
- List<GoodsEntity> result = new ArrayList<>();
- for (GoodsInOrderEntity goods :
- goodsInOrderEntity) {
- result.add(goods.getGoodsEntity());
- }
- return result;
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml"
- xmlns:h="http://xmlns.jcp.org/jsf/html"
- xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
- xmlns:f="http://xmlns.jcp.org/jsf/core">
- <h:head>Shop</h:head>
- <h:body>
- <h:form>
- <table>
- <tr>
- <td><h:inputText value="#{managingGoodsCDI.name}"/></td>
- <td><h:inputText value="#{managingGoodsCDI.price}"/></td>
- </tr>
- <tr>
- <td>
- Наименование
- </td>
- <td>
- Цена
- </td>
- </tr>
- <tr>
- <td>
- <h:commandButton value="Создать товар" action="#{managingGoodsCDI.createThing}"/>
- </td>
- <td>
- <h:commandButton value="Создать заказ" action="#{managingGoodsCDI.createOrder}"/>
- </td>
- </tr>
- </table>
- Созданные товары
- <h:dataTable value="#{managingGoodsCDI.goods}" var="Thing">
- <h:column>
- <h:outputText value="#{Thing.name}"/>
- </h:column>
- <h:column>
- <h:outputText value="#{Thing.price}"/>
- </h:column>
- <h:column>
- <h:commandButton value="Добавить" action="#
- {managingGoodsCDI.addingToOrder(Thing)}"/>
- </h:column>
- </h:dataTable>
- Добавленные товары
- <h:dataTable value="#{managingGoodsCDI.goodsInOrder}" var="Thing">
- <h:column>
- <h:outputText value="#{Thing.name}"/>
- </h:column>
- <h:column>
- <h:outputText value="#{Thing.price}"/>
- </h:column>
- </h:dataTable>
- </h:form>
- </h:body>
- </html>
Add Comment
Please, Sign In to add comment