Development tools: Eclipse Java ee ide for webdevelopers, JBoss 5.1.0.ga, MySQL 5.0.22
There are three types of EJB (Enterprise JavaBean): Session Bean, message Bean, and Entity Bean. Session Bean is divided into stateful Session Bean and stateless Session Bean ).
Here we will focus on entity beans.
The entity bean can be guessed by the name. This is an Orm. For children who have learned hibernate, it is easy to accept the entity bean, and in terms of transactions, ejb3 released the JPA specification, so that as long as JTA is followed for entity writing, these entities have cross-framework portability. This implementation is also due to the progress of ejb3 over ejb2 and the use of pojo (for ejb2, I only know that it is very troublesome to develop, and I have not studied it in detail ).
If you compare hibernate with ejb3.0, you will find that session and entitymanager, sessionfactory, and persistencecontext are similar. for special reasons, I first learned hibernate after EJB, since the object management object name in EJB is more image-like, it is still not difficult to understand session over entitymanager.
The development of EJB is different from that of hibernate. In some respects, Hibernate requires you to download the hibernate package from the Internet. What is different about EJB is that it is implemented by the application server, my practice here is to introduce all the jar packages in the client under the JBoss root directory, so that the EJB development environment is available. So what should I do to create my first entitybean?
First, copy the database driver to jboss_home/Server/default/lib, and then copy docs/examples/JCA/mysql-ds.xml to the/Servet/default/deploy directory, and modify the database username and password.
Click file-> New-> EJB project and enter the project name. Configure the items in configuration. If not configured, you will find that the annotation of the EJB you created is invalid. Click Modify to enter the projectfacets panel, set configuration to custom, and select ejbmodele, Java, JPA in project fecet. The versions are 3.0, 1.6, and 2.0 respectively. If JPA is not selected, the annotations in your entitybean are invalid. Ear
Membership and working sets are skipped for the moment. Click Nest. If you can finish the settings directly at this time, if not, continue next, select "type" Disable library configuration ", and finish the settings.
Now you see the source code root directory under the META-INF, will find the persistence. xml file, this file you can understand is the core configuration file of hibernate, In this Configuration:
MySQL Database Configuration and persistence configuration are as follows:
persistence.xml<persistence-unit name="test" transaction-type="JTA"><jta-data-source>java:/MySqlDS</jta-data-source><properties><property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/><property name="hibernate.hbm2ddl.auto" value="update"/></properties></persistence-unit>mysql-ds.xml<local-tx-datasource> <jndi-name>MySqlDS</jndi-name> <connection-url>jdbc:mysql://localhost:3306/ejb3</connection-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <user-name>root</user-name> <password>admin</password> <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name> <metadata> <type-mapping>mySQL</type-mapping> </metadata> </local-tx-datasource>
Now, you can develop the Entity Bean. Create a new class in the project named "user". If you have experienced hibernate development, so it is easy to get started with the development of Entity Bean annotations, because they all adopt a set of standardized JPA. In addition, when developing entity beans, it is best to use JPA standard annotations and try not to use every framework extension, because the entity beans developed in this way, copying data to any framework that supports JPA is acceptable.
Similar to the object annotation method of hibernate, you first need a @ entity to indicate this class as an object, and then need to represent the ID, and generate a policy for ID Selection: person. java (the get and set methods are not listed)
public class Person implements Serializable {private static final long serialVersionUID = 1L;@Id@GeneratedValueprivate int id;private String name;}
In this way, the development of an entity bean is complete, so because hibernate. hbm2ddl. Auto is configured for the database as update, the table structure is automatically mapped during entitymanager initialization.
Next, write another manager to operate on the person:
@Stateless@Remotepublic class PersonManagerBean implements PersonManager {@PersistenceContext(unitName="test")private EntityManager em;@Overridepublic void addPerson(String name) {Person person=new Person();person.setName(name);em.persist(person);}}
I personally think that ejb3.0 is better named than hibernate, persistencecontext is sessionfactory, and entitymanager is session. However, if hibernate involves remote access, it will take a while, while EJB supports remote calls, in addition, it is extremely easy to publish ejb3.0 to WebService.
I want to know their choices through their descriptions and names. Hibernate, lightweight ORM framework, and EJB enterprise-level JavaBean.
If the project does not involve distributed and remote method calls, Hibernate is still good.
So far, the development of the first entity bean is over. I personally feel that the understanding of ejb3.0 is best established on Hibernate, because ejb3.0 has started to use annotations extensively, note-based development is simple and eliminates the need to switch back and forth between two windows. However, it is helpful to understand how to use XML file development throughout the process.
Finally, let's talk about the choice of annotations and configuration files. The emergence of annotations is definitely not a replacement for XML. However, due to the simplicity of annotations, it is imperative to widely use annotations.