Persistence API (JPA) series (iii) Development of entity beans-establish a connection to the database __javaee

Source: Internet
Author: User
Tags jboss jboss server java se

In EJB 2.x, EJBS have 3 types of beans, which are session beans, message-driven beans (message-driven beans), and entity beans (Entity beans), respectively.

With the introduction of EJB 3, the entity beans in ejb2.x are being replaced by the JPA specification, which is used not only in the EJB environment, but also in the Java SE, Java EE Environment, and is more widely used than entity beans in EJB 2.x.

But here we still call it the entity bean.

Like session beans and message-driven beans, the new entity Bean is also a simple Java object (POJO) with an annotation character (@Entity), the entity relationship and the O/R mapping are defined by annotation characters, and several different database operating specifications are provided.

Once accessed by Entitymanager, it becomes a persistent object and becomes part of the persistence context. At this point we can use entity objects just like Hibernate, IBATIS, MyBatis.

This article will explain the development technology of entity bean in detail.

1, establish a connection with the database, demo entity Bean development and call process.
2. Entity Manager: Methods for performing database updates.
3, life cycle: Entity Bean's listening and callback.
4. Relational entity mapping: A method for developing entities.
5, JPQL Query Language: Execute database entity query.
6. Native SQL query: Execute native SQL statement.


The relationships between them, as shown in the figure, manipulate entity beans through the entity Manager to implement updates to the database, JPQL queries, and native SQL queries. The Entity Manager is the tool, and the entity bean is the data.

The following is a brief description of the call process for the entity bean, and then the process of configuring and developing is demonstrated by developing the first entity bean, including the following:

1, configure the data source.
2, specify the data source.
3, the development of the first entity Bean--student.java.
4, development session beans to invoke--studentdaoremote.java and Studentdao.java.
5. Package and deploy to JBoss server.
6, the development of client testing--studentdaoclient.java.


The final implementation, through the establishment of entity beans and MySQL database connection, to insert a record in the datasheet.
one, the entity Bean's working principleEntity beans are managed as persistent classes by the EJB container, and to implement calls to the persisted class, you must go through the following steps.
(1) Configure the data source connection.
(2) Specify the data source in the configuration file Persistence.xml.
(3) Develop entity beans.
(4) Call entity beans in Session bean, Java SE, or Java EE.

Entity beans can be invoked not only by the session bean, but also by any Java class, JSP, and servlet, and the purpose of the invocation is to implement operations on the database. Its meaning is identical with hibernate, Ibatis, it is the DAO layer of the system, realizes the access to the database.
Here we demonstrate the process of creating a connection to a database, in order from bottom to top.
Second, configure the data source

Jndi-name: Specifies the Jndi name.
Connection-url: Database connection URL.
Driver: Database-driven class.
User-name: Database login user name.
Password: Database login password.
We only need to refer to the data source by referencing the Jndi command Ksmysqlds, which is simply a simple way to specify the alias in Persistence.xml
third, specify the data source--persistence.xmlThe data sources configured above are loaded and managed by the JBoss server, and to use these data sources, you need to specify which data source to reference in our application. The referenced method is simply to add a profile persistence.xml in the applied/Project/meta-inf directory and specify the referenced data source Jndi name in the file, and you can set the associated action properties for the data source.
Configuration file Persistence.xml

It contains 3 configuration elements, respectively, as follows.
Persistence-unitElement: You can have one or more, each persistence-unit element defines the persisted content name, the data source name used, and the hibernate attribute. The Name property is used to set the persistent name.
Jta-data-sourceelement: Used to specify the data source name Ksmysqlds used by the entity bean, java:/prefix cannot be missing when specifying a data source name, and note the case of the data source name.
Propertieselement: Used to specify the properties of the hibernate, and if the value of the Hibernate.hbm2ddl.auto is set to update, the entity bean can add the corresponding field in the datasheet when it adds a property.
(1) Property element properties are different in the persistent products used by each application server, such as JBoss using HIBERNATE,WEBLOGIC10 to use Kodo,glassfish/sun application server/ Oralce use TopLink.
(2) The JBoss server causes the release and uninstall of entity beans when it is started or closed.

Iv. Development of the first entityThe entity bean actually corresponds to the table in the database, which is the representation of the tables in the database in the Java class, usually the most common Pojo class. The EJB container can automatically create data tables in the database based on entity beans, which requires that the entity classes correspond to the structure of the datasheet, including table names, field names, field lengths, field types, primary keys, and so on.
To develop a single table entity bean that corresponds to a database table, we first design a data structure for the Student table student, which includes 7 fields, as shown in table 6-1.
Student table student The field type in the MySQL database corresponds to the type of field in the Pojo class, which is the type of the variable in the entity bean.
To develop an entity bean that corresponds to the table, simply create a new Pojo class, add 7 variables with the same name as the table's fields, and use some annotation characters to represent the corresponding mapping relationship between the entity Bean and the datasheet student.
Bean class Student.java. First, let's take a look at the code for the complete entity class, as follows:

Entity Bean Class Student.java
[Java]  View plain copy package com.ejb.entitybean;         import  java.io.serializable;     import java.util.date;     import  javax.persistence.column;     import javax.persistence.entity;     import javax.persistence.generatedvalue;     import javax.persistence.id;      import javax.persistence.table;         @ Suppresswarnings ("Serial")      @Entity     @Table (name =  "Student")      public class student implements serializable {          private integer studentid;      // School Number          private String name;         //Name          private boolean sex;             //Sex          private  short age;          //Age          private date birthday;      //Date of birth           private string address;     //Address           private String telephone         //Tel               @Id          @GeneratedValue         public Integer  Getstudentid ()  {             return  studentid;    

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.