Embeddable Entity : Persistence « EJB3 « Java

Embeddable Entity

File: EmployeeService.java

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public class EmployeeService implements EmployeeServiceLocal, EmployeeServiceRemote {
  EntityManager em;
  public EmployeeService() {
  public void doAction(){

    Employee cust = new Employee();
    Address address = new Address();


File: EmployeeServiceLocal.java

import java.util.Collection;

import javax.ejb.Local;

public interface EmployeeServiceLocal {
    public void doAction();


File: EmployeeServiceRemote.java

import java.util.Collection;

import javax.ejb.Remote;

public interface EmployeeServiceRemote{
  public void doAction();  


File: Address.java

import javax.persistence.Column;
import javax.persistence.Embeddable;

public class Address implements java.io.Serializable {
   private String street;
   private String city;
   private String state;

   public String getStreet() { return street; }
   public void setStreet(String street) { this.street = street; }

   public String getCity() { return city; }
   public void setCity(String city) { this.city = city; }

   public String getState() { return state; }
   public void setState(String state) { this.state = state; }

File: Employee.java

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

public class Employee implements java.io.Serializable {
  private long id;

  private String firstName;

  private String lastName;

  private Address address;

  public long getId() {
    return id;

  public void setId(long id) {
    this.id = id;

  public String getFirstName() {
    return firstName;

  public void setFirstName(String first) {
    this.firstName = first;

  public String getLastName() {
    return lastName;

  public void setLastName(String last) {
    this.lastName = last;

  @AttributeOverrides( { @AttributeOverride(name = "street", column = @Column(name = "STREET")),
      @AttributeOverride(name = "city", column = @Column(name = "CITY")),
      @AttributeOverride(name = "state", column = @Column(name = "STATE")) })
  public Address getAddress() {
    return address;

  public void setAddress(Address address) {
    this.address = address;

File: Main.java

import java.util.Collection;

import javax.naming.InitialContext;

public class Main {

  public static void main(String[] a) throws Exception {

    EmployeeServiceRemote service = null;

    // Context compEnv = (Context) new InitialContext().lookup("java:comp/env");

    // service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
    service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeService/remote");




File: jndi.properties



EJB-EmbeddableEntity.zip( 4,489 k)

Related examples in the same category

1.Entity With Date
2.Get List Of Employees From Ejb
3.Flush Data In EJB
4.Use PersistenceUnit
5.Use PersistenceContext annotation to Link Persistence Context
6.Set Flush Mode
7.Set Column Name For Entity Attribute
8.Retrieve Data From Ejb
9.Persistence Context Type: TRANSACTION
10.Persistence Context Type: EXTENDED