banner



How To Create An Entity Class In Java


Install and Use NetBeans 6.9 part 4

What do we have in this session 4?

  1. Creating the Entity Class and Session Facade

  2. Creating the Entity Class

Creating the Entity Class and Session Facade

In this section you will create an entity class and a session facade for the entity class. An entity class is a plain old Java object (POJO), a simple Java class that is identified as an entity by the @Entity annotation. Starting with the Java EE 5 specification, you can use entity classes as persistent objects to represent tables in a database. The Java Persistence API enables you to use persistence in web applications without the need to create an EJB module.

The session facade for the entity class in this application is a stateless session bean. The Enterprise JavaBean (EJB) 3.1 architecture introduced as part of the Java EE 6 specification enables you to create session beans without the business interfaces that were required in EJB 3.0. The Java EE 6 specification also allows you to package EJB components directly in a WAR archive. This simplifies development of smaller web applications by eliminating the need to create separate EJB modules that are packaged as a JAR (Java Application) archive in an EAR (Enterprise Application) archive. However, for larger enterprise applications that are distributed across different machines, you will still want to create EAR archives to separate your business logic from the presentation layer.

Creating the Entity Class

In this exercise you will use the New Entity Class wizard to create a simple persistent entity class. You will also use the wizard to create a persistence unit that defines the data source and entity manager used in the application. You will add one field in the class to represent the data in your table and generate a getter and setter for the new field.

An entity class must have a primary key. When you create the entity class using the wizard, the IDE by default generates the field id and annotates the field with the @Id annotation to declare the field as the primary key. The IDE also adds the @GeneratedValue annotation and specifies the key generation strategy for the primary id field.

Using Java Persistence in your project greatly simplifies application development by removing the need for configuring deployment descriptors to provide object-relational mapping information for persistent fields or properties. Instead, you can use annotations to define these properties directly in a simple Java class.

Entity persistence is managed by the EntityManager API. The EntityManager API handles the persistence context, and each persistence context is a group of entity instances. When developing your application, you can use annotations in your class to specify the persistent context instance of your entity instances. The life-cycle of the entity instances is then handled by the container.

To create the entity class, perform the following steps.

Right-click the project node and chooseNew > Other.

NetBeans IDE: selecting the Java web application project, invoking the new project menu

Select Entity Class from the Persistence category. Click Next.

NetBeans IDE: selecting the Java web application project, creating the persistence entity class

Type Message for the Class Name.

Type entities for the Package.

Click Create Persistence Unit. In NetBeans IDE 6.9, the steps for creating the persistence unit are incorporated into the New Entity Class wizard.

--------------------------------------------------------------------------------------------------------------------------

NetBeans IDE: selecting the Java web application project, new entity class project name and location

Select a datasource (for example, select jdbc/sample if you want to use JavaDB).

The datasource for jdbc/sample is bundled with the IDE when you install the IDE and the GlassFish server, but you can specify a different datasource if you want to use a different database.

You can keep the other default options (Persistence Unit Name and EclipseLink Persistence Provider). Confirm that the persistence unit is using the Java Transaction API and that the Table Generation Strategy is set to Create so that the tables based on your entity classes are created when the application is deployed.

Click Finish.

NetBeans IDE: selecting the Java web application project, the provider and database settings

When you click Finish, the IDE creates the entity class and opens the class in the editor. You can see that the IDE generated the id field private Long id; and annotated the field with @Id and @GeneratedValue(strategy = GenerationType.AUTO) .

NetBeans IDE: selecting the Java web application project, the generated persistence entity class file templates

In the editor, manually add the message field (in bold) below the id field.

private Long id;

private String message;

NetBeans IDE: selecting the Java web application project, adding object variables

Right-click in the editor and choose Insert Code (Ctrl+I) and then select Getter and Setter.

NetBeans IDE: selecting the Java web application project, invoking the Insert Code context menu

NetBeans IDE: selecting the Java web application project, selecting Getter and Setter methods

In the Generate Getters and Setters dialog box, select the message field and click Generate.

The IDE generates getter and setter methods for the field message.

-------------------------------------------------------------

NetBeans IDE: selecting the Java web application project, Getter and Setter variables methods

Save your changes.

The entity class represents a table in the database. When you run this application, a database table for Message will be automatically created. The table will contain the columns id and message .

If you look at the persistence unit in the XML editor, you can see that the application will use the Java Transaction API (JTA) ( transaction-type="JTA" ). This specifies that the responsibility for managing the life-cycle of entities in the persistence context is assigned to the container. This results in less code because the entity lifecycle is managed by the container and not by the application. For more information about using JTA to manage transactions, see the Java Transaction API documentation.

NetBeans IDE: selecting the Java web application project,the Getter and Setter methods

< NetBeans and Java Web 3 | Java and Friends | NetBeans and Java Web 5 >


NetBeans and Java Web 1 | NetBeans and Java Web 2 | NetBeans and Java Web 3 | NetBeans and Java Web 4 | NetBeans and Java Web 5 | NetBeans and Java Web 6 | NetBeans and Java Web 7 | NetBeans and Java Web 8 | Java and Friends

How To Create An Entity Class In Java

Source: https://www.javaguicodexample.com/netbean89javaeejsfweb3.html

Posted by: kerbereaceforehis.blogspot.com

0 Response to "How To Create An Entity Class In Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel