EJB3 plugin

This plugins generates EJB3 entities.

Example

Suppose we have two tables in a database :

  • Table CITY
  • Table PERSON

Here is the corresponding MySQL DDL :

CREATE TABLE city (
  id_city int(11) NOT NULL auto_increment,
  name varchar(50) NOT NULL,
  PRIMARY KEY (id_city)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
        
CREATE TABLE person (
  id_person int(11) NOT NULL auto_increment,
  name varchar(50) NOT NULL,
  first_name varchar(50) NOT NULL,
  age int(11) NOT NULL,
  id_city int(11) NOT NULL,
  PRIMARY KEY (id_person),
  KEY fk_person_city (id_city),
  CONSTRAINT fk_person_city FOREIGN KEY (id_city) REFERENCES city (id_city)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

A person lives in a city, and for each city there are many persons living in it. So we have a many-to-one relationship.

EJB3 plugin generate two entities, City and Person :

City.java

package com.salto.ejb3;

import java.util.List;
import java.io.Serializable;
import java.sql.Timestamp;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;
import javax.persistence.Embeddable;

/**
 * <p>Pojo mapping table city</p>
 * <p></p>
 *
 * <p>Generated at Tue Feb 06 10:43:30 CET 2007</p>
 * @author Salto-db Generator Ant v1.0.15 / EJB3
 * 
 */
@Entity
@Table(name = "city", catalog = "sdb")
@SuppressWarnings("serial")
public class City implements Serializable {

        /**
         * Attribute idCity.
         */
        private Integer idCity;
        
        /**
         * Attribute name.
         */
        private String name;
        
        /**
         * List of Person
         */
        private List<Person> persons = null;

        
        /**
         * @return idCity
         */
        @Basic
        @Id
        @GeneratedValue
        @Column(name = "id_city")
        public Integer getIdCity() {
                return idCity;
        }

        /**
         * @param idCity new value for idCity 
         */
        public void setIdCity(Integer idCity) {
                this.idCity = idCity;
        }
        
        /**
         * @return name
         */
        @Basic
        @Column(name = "name")
        public String getName() {
                return name;
        }

        /**
         * @param name new value for name 
         */
        public void setName(String name) {
                this.name = name;
        }
        
        /**
         * Get the list of Person
         */
         @OneToMany(mappedBy="city")
         public List<Person> getPersons() {
                return this.persons;
         }
         
        /**
         * Set the list of Person
         */
         public void setPersons(List<Person> persons) {
                this.persons = persons;
         }


}

Person.java

package com.salto.ejb3;

import java.util.List;
import java.io.Serializable;
import java.sql.Timestamp;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;
import javax.persistence.Embeddable;

/**
 * <p>Pojo mapping table person</p>
 * <p></p>
 *
 * <p>Generated at Tue Feb 06 10:43:30 CET 2007</p>
 * @author Salto-db Generator Ant v1.0.15 / EJB3
 * 
 */
@Entity
@Table(name = "person", catalog = "sdb")
@SuppressWarnings("serial")
public class Person implements Serializable {

        /**
         * Attribute idPerson.
         */
        private Integer idPerson;
        
        /**
         * Attribute name.
         */
        private String name;
        
        /**
         * Attribute firstName.
         */
        private String firstName;
        
        /**
         * Attribute age.
         */
        private Integer age;
        
        /**
         * Attribute city
         */
         private City city;     

        
        /**
         * @return idPerson
         */
        @Basic
        @Id
        @GeneratedValue
        @Column(name = "id_person")
        public Integer getIdPerson() {
                return idPerson;
        }

        /**
         * @param idPerson new value for idPerson 
         */
        public void setIdPerson(Integer idPerson) {
                this.idPerson = idPerson;
        }
        
        /**
         * @return name
         */
        @Basic
        @Column(name = "name")
        public String getName() {
                return name;
        }

        /**
         * @param name new value for name 
         */
        public void setName(String name) {
                this.name = name;
        }
        
        /**
         * @return firstName
         */
        @Basic
        @Column(name = "first_name")
        public String getFirstName() {
                return firstName;
        }

        /**
         * @param firstName new value for firstName 
         */
        public void setFirstName(String firstName) {
                this.firstName = firstName;
        }
        
        /**
         * @return age
         */
        @Basic
        @Column(name = "age")
        public Integer getAge() {
                return age;
        }

        /**
         * @param age new value for age 
         */
        public void setAge(Integer age) {
                this.age = age;
        }
        
        /**
         * get city
         */
        @ManyToOne
        @JoinColumn(name = "id_city")
        public City getCity() {
                return this.city;
        }
        
        /**
         * set city
         */
        public void setCity(City city) {
                this.city = city;
        }



}

Once entities are generated you can use them with any EJB3 persistence manager, such as Hibernate annotations, toplink, and so on.