Eclipse + JBoss 5 + EJB3 Development Guide (6): Writing the first entity Bean program

Source: Internet
Author: User
Tags table name jboss mysql database

The EJB3 container manages entity beans through the Entitymanager object, the primary function of which is to map each other between records and JavaBean. For example, we find a record from a database that has field1 and field2 two fields, assuming a JavaBean, and also contains Field1 and Field2 properties. Then Entitymanager can map the values in this record to the Field1 and Field2 properties of JavaBean. In other words, a record in the database corresponds to an JavaBean object instance. Here's a simple example to show how to implement a program that applies entity beans. In this example, a stateless session bean is used to look up a record from a datasheet through a Entitymanager object, map the record to an object instance of an entity bean, and then return a field value for that record through the session Bean's method.

Table Name: T_users

Fields and records in a table

    id    name    password_md5
     1     bill    TaokQpoNJQb02eafO/JgYw==

First, configure the JBoss database connection pool

Create a Mysql-ds.xml file in the <jboss installation directory >\server\default\deploy directory, which reads as follows:

<?xml version="1.0" encoding="UTF-8"?>
<datasources>
  <local-tx-datasource>
    <jndi-name>MyPIM</jndi-name>
    <connection-url>jdbc:mysql://localhost:3306/pim</connection-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <user-name>root</user-name>
    <password>1234</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>
</datasources>

The Mysql-ds.xml file is used to configure the MySQL data source. In fact, the file can be found in the <jboss installation directory >\docs\examples\jca directory, and the reader needs to change the database name, username and password, and Jndi name to their own. Finally, copy the modified Mysql-ds.xml file into the Deploy directory (this file name must be called).

A JDBC driver (jar file) is required to connect to the MySQL database and copy the file to the <jboss installation directory >\server\default\lib directory.

Related Article

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.