Java EE5 Platform Java Persistence Development (EJB3 and JSF)

Source: Internet
Author: User
Tags hash object object

1. Prerequisite Conditions
First, the machine must have the following environment, if not can be downloaded to the Sun website
netbeans5.5
Sun Java System Application Server Platform Edition 9
Jdk5

2. Create Web Project
Create new Project File > New project from the menu. Select the Web application from the Web directory.
Project name Zooapp, server item selects the Sun Java System application Server, Java EE Version chooses the 5, then next.
Select the Java Server faces check box

3. Create a persistence unit
Right-click the project node in the Engineering window and open the wizard from New > File/folder
Select Persistence unit from the persistence directory and then Next
Use the default persistence unit Name.
Persistence Provider uses TopLink (default).
Data source, using the data source jdbc/sample. (This default Java database has been set up for Sun Java System application Server)
Confirm that the Java Transaction API is selected
Table Generation Strategy Select Create (this will automatically create tables when the system is released)
and finish.

4. Create Entity Bean
A right click on the project node and select New > File/folder.
B Select entity class from the persistence directory and then next.
C Input Animal as the class name,entity as the package name, the Primary Key type selects Long. and then Finish
D Add the following properties to the generated code

String name;
String kind;
String weight;
Pavilion Pavilion;

e then right-click the code interface Refactor > Encapsulate fields, in the pop-up interface, select the We want to generate
Getters,setters the check box for the method, and then determine. In the Encapsulate Fields dialog box that appears below the editing interface
Click Do Refactoring.
f The Default field name in the database named is name, if we want to replace name with Animalname, the variable in name must be
Add the following markup before the declaration
@Column (name= "Animalname")
G We would like to pavilion record animal and pavilion have a many-to-many relationship (Many-to-one), then add
Mark below
@ManyToOne
H Press alt-shift-f combination shortcut key to import all the classes that need to use (this time the hint can not find pavilion is normal)
I save

Creating Pavilion Entity Beans
Step a B C ditto, simply change the class name to Pavilion
D Add the following properties
String name;
String address;
Collection <Animal> Animals;
Step e Ibid.
F Change the name of the database field by adding the following tag before the name declaration
@Column (name= "Pavilionname")
g before animals, add the following markers to indicate a one-to-many relationship
@OneToMany (mappedby= "Pavilion")
Step h I ditto

5. Create Web Interface
A right click on the project node and select New > File/folder.
b choose to find JSF Pages from Entity Class from the persistence then next.
C in the JSF Page Creation Wizard from entity Class, select Add all to select our two entity beans
D JSF page folder defaults to NULL, type entity as the package name for the build-related class, and finish

6, add the following code in the Pavilion class

Code for validating information when creating new item
public int hashcode () {
int hash = 0;
Hash + = (this.id!= null this.id.hashCode (): 0);
return hash;
}

public Boolean equals (Object object) {
if (object = = NULL | |!this.getclass (). Equals (Object.getclass ())) {
return false;
}
Pavilion other = (Pavilion) object;
if (this.id!= other.id && (this.id = = NULL | |!this.id.equals (other.id)))
return false;
return true;
}

7. Operation Engineering
Run the database from menu Tools > Java DB database > Start Java DB Server, which is normally run by default.
Right-click the project node to select Run Project.
You will then see the following interface in the pop-up browser


Then the data can be added to modify the deletion of the operation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.