Good. Now the schema is completed, so let's build the Web application. Launch Eclipse.

From the Menu reach File | New | Dynamic Web Project

Choose the name AppStore for your Web application and leave the default Settings.

Now add to your Project the JSF 2.0 facets, by selecting Properties | Projects Facets

jsf 2.0 java ee 6 tutorial

Then, enable JSF 2.0 libraries and then instruct Eclipse where JBoss AS libraries are placed
(under "JBOSS_HOME\server\default\deployers\jsf.deployer")

jsf 2.0 jboss 6

Now let's bind the Datasource definition into our Web application by creating the persistence.xml into the WEB-INF/ folder

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="AppStore" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:/MySqlDS</jta-data-source>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        </properties>
    </persistence-unit>
</persistence>

Ok. Now it's time to build our classes. We will start from the model classes which map the database tables.
Building the Model
Here is the Customer class:

package model;

import java.io.Serializable;
import javax.persistence.*;
import java.util.List;
import static javax.persistence.FetchType.EAGER;

@Entity
@Table(name = "customer")
public class Customer implements Serializable {
 private static final long serialVersionUID = 1L;
 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 @Column(name = "ID")
 private int id;
 @Column(name = "COUNTRY")
 private String country;
 @Column(name = "NAME")
 private String name;

 @OneToMany(mappedBy = "customerFK", fetch = EAGER)
 private List<Item> items;

 // Getters and setters here ...

}


and here is the Item class:

package model;

import java.io.Serializable;
@Entity
@Table(name="item")
public class Item implements Serializable {
 private static final long serialVersionUID = 1L;
 @Id
 @GeneratedValue(strategy=GenerationType.AUTO)
 @Column(name="ID")
 private int id;

 @Column(name="PRICE")
 private int price;
 @Column(name="PRODUCT")
 private String product;
 @Column(name="QUANTITY")
 private int quantity;
 //bi-directional many-to-one association to Customer
 @ManyToOne  
 @JoinColumn(name="CUSTOMER_ID")  
 private Customer customerFK;
 
 // Getters and setters here ...
}

 

0
0
0
s2smodern