Showing posts with label SL-340-EE6. Show all posts
Showing posts with label SL-340-EE6. Show all posts

30 December 2019

JPA: Composite Primary Key with Foreign Keys



 
Entity for the ProductSales Table:

package com.example.model;

import javax.persistence.*;

@Entity
@IdClass(ProductSalesPK.class)
public class ProductSales {

 @Column(name = "QUANTITY")
 private int quantity_sold;

 @Id
 @ManyToOne
 @JoinColumn(name = "sales_id")
 private Sales sales;

 @Id
 @ManyToOne
 @JoinColumn(name = "product_id")
 private Product product;

 public ProductSales() {
 }

 public ProductSales(Sales sales, Product product, int quantity_sold) {
  this.sales = sales;
  this.product = product;
  this.quantity_sold = quantity_sold;
 }

 public Product getProduct() {
  return product;
 }

 public Sales getSales() {
  return sales;
 }

 public void setProduct(Product product) {
  this.product = product;
 }

 public void setSales(Sales sales) {
  this.sales = sales;
 }

 public int getQuantity_sold() {
  return quantity_sold;
 }

 public void setQuantity_sold(int quantity_sold) {
  this.quantity_sold = quantity_sold;
 }
}

ProductSalesPK Id class:

package com.example.model;

import java.util.Objects;

public class ProductSalesPK {
    private int sales;
    private String product;

 public ProductSalesPK(int sales_id, String product_id) {
  this.sales = sales_id;
  this.product = product_id;
 }

 @Override
 public int hashCode() {
  int hash = 5;
  hash = 83 * hash + this.sales;
  hash = 83 * hash + Objects.hashCode(this.product);
  return hash;
 }

 @Override
 public boolean equals(Object obj) {
  if (obj == null) {
   return false;
  }
  if (getClass() != obj.getClass()) {
   return false;
  }
  final ProductSalesPK other = (ProductSalesPK) obj;
  if (this.sales != other.sales) {
   return false;
  }
  if (!Objects.equals(this.product, other.product)) {
   return false;
  }
  return true;
 } 
}
Entity class fpor Product:

package com.example.model;
package com.example.model;

import java.util.Objects;
import javax.persistence.*;
import javax.validation.constraints.*;
@Entity
public class Product {
 @NotNull(message="product id not null") @Size (min = 1,message="product id not empty") 
 @Id
    private String product_id; 
 @NotNull(message="product name not null") @Size (min = 10,message="product name length at least 10")
    private String prod_name;
 @Min(value=5, message="price >= {value}")
    private double price;
    private String prod_desc;

 public Product() {
 }

  
    public Product(String id, String name, double price, String description) {
        this.product_id = id;
        this.prod_name = name;
        this.price = price;
        this.prod_desc = description;
    }

    public String getId() {
        return product_id;
    }

    public String getName() {
        return prod_name;
    }

    public void setName(String name) {
        this.prod_name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getDescription() {
        return prod_desc;
    }

    public void setDescription(String description) {
        this.prod_desc = description;
    }
    
    @Override
    public String toString() {
        String s = String.format("Product id: %s\n"
        + "Name: %s\n"
        + "Description: %s\n"
        + "Price: $%.2f", product_id, prod_name, prod_desc, price);
        return s;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 97 * hash + Objects.hashCode(this.product_id);
        hash = 97 * hash + Objects.hashCode(this.prod_name);
        hash = 97 * hash + (int) (Double.doubleToLongBits(this.price) ^ (Double.doubleToLongBits(this.price) >>> 32));
        hash = 97 * hash + Objects.hashCode(this.prod_desc);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Product other = (Product) obj;
        if (!Objects.equals(this.product_id, other.product_id)) {
            return false;
        }
        if (!Objects.equals(this.prod_name, other.prod_name)) {
            return false;
        }
        if (Double.doubleToLongBits(this.price) != Double.doubleToLongBits(other.price)) {
            return false;
        }
        if (!Objects.equals(this.prod_desc, other.prod_desc)) {
            return false;
        }
        return true;
    }
    
}

7 June 2018

Java EE web technology version history (updated)


JSR numbers are included as links.
Servers are webcontainers.Take care, web containers do not include JSF. You have to add it yourself or use an enterprise application server. Enterprise application servers are included in the
complete Java EE version overview.

Java EEservletJSP     ELJSTLJSF    WebSocketMVCServer       What’s new
2.1
1.0

iPlanet
jetty 1
ServletContext, RequestDispatcher
1.2
2.2
1.1
tomcat 3.3
jetty 3
war
1.3
53 2.3
53 1.2
tomcat 4.1
jetty 4
filters
1.4
154 2.4
152 2.0
1.0
52 1.1
tomcat 5.5
jetty 5
5
2.5
245 2.1
2.1
1.2
127 1.2

tomcat 6
jetty 6
annotations
6
315 3.0
2.2
2.2

314 2.0

tomcat 7
jetty 8
asynchronous servlets, EL method calls, more...
7
340 3.1
 341 3.0

344 2.2
356 1.0

tomcat 8
jetty 9.1
HTML 5, non blocking (Listener) servlet IO
8
369 4.0


372 2.3
1.1
371 1.0tomcat 9 HTTP/2

27 April 2016

NetBeans hints (edit)

Your first NetBeans project
  1. On the start page click the Learn & Discover tab
  2. Under Demo's and Tutorials select Java SE applications
  3. Start the Java Quick Start Tutorial 
NetBeans 7.4 Developer Guide
If you're searching how to do someting in netbeans just type it in the ... search box, it will not only search in your project,  but also in the online help.
Keyboard shortcuts
  • view, search & change shortcuts: tools => options => keymap
    • You can set the keymap to a profile from another IDE (Eclipse, IntelliJ...) here as well.
  • help => keyboard shortcuts card. The pdf that is shown, lives in
    netbeansInstallDir
    /nb/shortcuts.pdf. 
  • Some additional useful shortcuts:
    • Go to
      • to definition: CTRL click
        • to implementation: CTRL ALT click
    • Edit
      • Code completion
        • autocomplete popup (+ javadoc): CTRL SPACE
        • javadoc inline popup: CTRL SHIFT SPACE
          • ALT F1 to see in browse
          • Netbeans bundles plenty of javadoc you can browse supplied javadoc for your project libraries from help>Javadoc references
        • complete to recently typed word: CTRL k
      • Add semicolon at end of line:  CTRL ;
      • Add new line below + go there: SHIFT ENTER
    • View
      • fold/unfold code: CTRL –/+
      • members/herarchy of current selection: CTRL/ALT SHIFT F12
      • zoom: ALT + mousewheel up/down 
      • editor only: CTRL SHIFT ENTER (>= 7.4)
Running code
  • After you ran a program, and corrected some errors Rerun using the >> buttons.

  • The lighter double arrows below allow you to rerun with different parameters. You can change the arguments you run with in the Ant properties window.
 
  • Similar options are available for (individual) tests.
    • You can also launch an individual test by  positioning the cursor in the method and selecting "Run focused test" from the right click menu. 


Samples
Netbeans comes with plenty of sample projects for you to explore

HTTP Monitor

The server side monitor is enabled by default for Tomcat and can be enabled in the properties of GlassFish. You may have to undeploy/redeploy your webapp for the monitor to show up.
When the server receives an HTTP request, NetBeans will show a window with all request details. It extracts all parameters and stuff for you, but does not show the raw bodies of POST requests. It does not show responses either.
There is also a client monitor. If you use the internal browser (or the NetBeans Chrome plugin) you can use Window > Web > Network Monitor (NetBeans >= 7.4). It does not show all client side traffic but is targeted at in-page initiated traffic (AJAX, websockets) and failed requests.

12 November 2013

StartWebLogic: \xxx was unexpected at this time

Just installed weblogic 12c with an initial domain on windows.
When running startweblogic in the domain i get an error
\xxx was unexpected at this time 

WebLogic seems to stumble over some stuff in my CLASSPATH. Mine 
has jars for the Belgium identity Card in it (others had a problem with QuickTime: QTJava.zip).

Easiest solution is the cleean my classpath, but then other applications will have problems.

After the SETLOCAL line in the startWebLogic.cmd and stopWeblogic.cmd in my domain, I added a line
set CLASSPATH=

Now it starts fine. Updating all these scripts is not the ideal solution however.

29 July 2012

What’s new in Servlet 3 (edit)

Servlets 3 (JSR 315) highlights:

  • Instead of configuring the web application in the web.xml deployment description you now have more options
    • modular deployment descriptor
      jars can be bundled with your webapp containing a web-fragment.xml file. These files are merged with the main web.xml file of your webapp. This allows for easy plugging in of web application modules. If you plugin a framework jar, you won’t have to modify the web.xml of your application anymore (e.g. to send all *.do files to a framework servlet).
    • annotations
      @WebServlet ("/jeeves")
      public class ZServlet extends HttpServlet {
      ....
      }
      • Extends HttpServlet, no POJO (yes!)
    • programmatic configuration
    • Asynchronous servlets
      @WebServlet ("/jeeves", asyncSupported=true) 
      public class ZServlet extends HttpServlet { 
      private AsyncContext ctx
      
      public void doGet( HttpServletRequest req, HttpServletResponse res) { 
      
         ctx = req.startAsync(); 
        // kick off a thread for async work
        ctx.start(new L8r());
        //method returns immediatly, no response sent
      } // end doGet method
      
      class L8r implements Runnable{
         public void run(){
          // do work
          ...
          // response ready
          ctx.complete();
          // alternative: ctx.dispatch("responseViewer.jsp");
        } // end run method
       } //end L8r class
      } //end ZServlet class

      The asynchronous servlet doGet() method does not wait for the L8r thread to complete. The request/response parameters however are not committed, but they are cached in AsyncContext. The L8r thread can then use these to reply to the waiting client.
    • Security
      • ProgrammaticLogin class
      • Java EE5 authorisation annotations
        • @RolesAllowed
        • @PermitAll
        • @DenyAll
      • @transportprotected: use SSL
      • Session security
      • <session-config>
        <!-- do not expose session id by using URL rewriting --> 
          <tracking-mode>COOKIE</tracking-mod>
          <cookie-config>
            <!-- do not expose cookie to javascript-->
            <http-only>true</ttp-only>
            <!-- only transit cookie over encrypted connection-->
            <secure>true</secure>
          </cookie-config>
        </session-config>
        
    • EL 2.2
      Method calls are now possible from EL, e.g.:
        #{portfolio.add('ORCL',100)}

    • File upload (aka multipart support) 

    21 June 2012

    Expression Language 3.0 is in public review

    The Expression Language (EL) is widely used in JSP and JSF. With version 3 the EL now becomes an independent specification. The EL v3 (JSR 314) is in public review until the end of July. It will be released in sync with Java EE 7 (Update: EL 3.0 was released in may 2013). New features include:

    •  As an independent specification, EL now comes with a standalone API, allowing usage outside a web container.
      • You can write your own ELProcessor. The source code is available, but I wish they had put the javadoc for  browsing at the project site as well.
    • Java 8 stuff
      • Lambda expressions
      • Collection literals follow proposed Java 8 syntax
        • List: [1, "two", 3.0]
        • Set: {1, 2, 3}
        • Map: {"one":1, "two":2, "three":3}
      • Collection streaming
    • Assignment operator (=) for changing values:
      • ${customer.name = 'Jan'}
    • String concatenation: + or cat operator
      • ${‘Welcome ‘ += customer.name += ‘ to our site’}.
    • Support for static references using the T construct (Type). 
      • By default only java.* and javax.* packages are available, but you can modify this using the EL API.
        • ${T(java.util.Calendar).APRIL}
        • ${T(java.util.Calendar).getInstance()}
      •  You can call constructors as a static method without a name. To call the default Calendar constructor:
        • ${T(java.util.Calendar).()}
      • For java.lang you can use the class without using the T construct
        • ${Boolean.TRUE}

    9 May 2012

    Apache Tommee is Java EE6 web profile certified. (edit)


    Apache Tommee is not a new product, but a bundle of Tomcat and Apache Java EE projects (OpenEJB, OpenJPA,...) by the Apache OpenEJB team. The lot is certified as a Java EE 6 Web Profile server.
    Tommee is pretty lean (24Mb) and can run embedded.
    The server bundles a set of components, all Apache gear: 
    EJBOpenEJB
    CDI OpenWebBeans
    JSP/servlet Tomcat
    JSF MyFaces
    JPAOpenJPA
    JTA Geronimo Transaction
    JavaMailGeronimo JavaMail

    Version 1.0 of Tommee was released on april 30th 2012.