EclipseLink – 12 – Embedded Objects

Merhaba Arkadaslar
Bu bolumde Embedded objeleri inceleyecegiz. Embedded objeler bir baska Entity objesinin varligina baglidir. Entity’lerle bir iliski soz konusu degildir yani @ManyToOne , @OneToOne gibi annotationlar kullanilmaz.

@Entity annotation yerine @Embedded annotation’i kullanilir. Embedded objelerin bir kimligi (primary key) yoktur dolayisiyla @Id annotation kullanilmaz.
Embedded objelerin durumu/field/instance variable bir baska Entity icin olusturulan tabloya gomulur.

Address.java

package _21.embedded.objects.model;

import javax.persistence.Embeddable;

@Embeddable
public class Address {

	private String street;
	private String road;
	private String no;
	private String city;
	private String zip;
	
	//constructors
        //getters and setters
        //toString

}

@Entity yerine @Embeddable annotation kullaniyoruz. Yine no-arg constructor tanimlanmalidir.

Employee21.java

package _21.embedded.objects.model;

import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Employee21 {

	@Id
	@GeneratedValue(strategy = GenerationType.TABLE)
	private int id;
	private String name;
	private String surname;
	private int salary;

	@Embedded
	private Address address;

        //constructors
        //getters and setters
        //toString
}

Employee21 sinifimizda Address sinifi tipinde instance variable yer almaktadir.Dikkat edecek olursak burada ise @Embedded annotation’ini kullaniyoruz.

EmployeeTest.java

package _21.embedded.objects.test;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import _21.embedded.objects.model.Address;
import _21.embedded.objects.model.Employee21;
import _21.embedded.objects.service.AddressService;
import _21.embedded.objects.service.AddressServiceImpl;
import _21.embedded.objects.service.EmployeeService;
import _21.embedded.objects.service.EmployeeServiceImpl;

public class EmployeeTest {

	public static void main(String[] args) {
		EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("EmployeePersistenceUnit");
		EntityManager entityManager = entityManagerFactory.createEntityManager();
		EntityTransaction entityTransaction = entityManager.getTransaction();

		EmployeeService employeeService = new EmployeeServiceImpl(entityManager);

		entityTransaction.begin();
		Employee21 employee = employeeService.createEmployee("Levent", "Erguder", 1000);
		entityTransaction.commit();

		System.out.println("Persisted :" + employee);

		AddressService addressService = new AddressServiceImpl();
		Address address = addressService.createAddress("myStreet", "myRoad", "37", "Istanbul", "34000");

		entityTransaction.begin();
		employee.setAddress(address);
		entityTransaction.commit();

		entityManager.close();
		entityManagerFactory.close();

	}
}

Ornegimizi inceleyecek olursak Employee21 tipinde ve Address tipinde bir obje olusturuyoruz. Daha sonrasinda setAddress metoduna arguman olarak address referans degiksenimizi bir transaction icerisinde veriyoruz.

Ornegimizi calistirdigimizda sadece bir tane tablo olusacaktir ; Employee21 !
Address sinifinda yer alan instance variable/field bilgileri bu Employee21 tablosunda kolon olarak yer alacaktir. Burada Address tablosu olusmayacaktir ! Bir nevi Address tablosunu Employee21 tablosu icerisine gommus(embed) olduk.

embeddable employee

Address sinifini hem Employee hem de Company icin kullandigimizi dusunelim. Employee21 tablosunda kullandigimiz kolon isimlerini Company icin kullanmak istemeyebilir , degistirmek isteyebiliriz. Bu durumda 2 tane annotation kullanabiliriz.

Birden fazla kolon icin isim override edilecekse @AttributeOverrides kullanilabilir.
@AttributeOverrides icerisinde @AttributeOverride annotation kullanarak bu islemi yapabiliriz.

Eger sadece birtane kolon icin isim override edilecekse @AttributeOverride kullanilabilir.
Birden fazla @AttributeOverride ‘i alt alta kullanmak legal degildir.

Company.java

package _21.embedded.objects.model;

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

@Entity
public class Company {

	@Id
	@GeneratedValue(strategy = GenerationType.TABLE)
	private String name;

	@Embedded
	//birden fazla kolon icin isim override edilecekse @AttributeOverrides kullanilabilir.
	//@AttributeOverrides icerisinde @AttributeOverride annotation kullanarak bu islemi yapabiliriz.
	@AttributeOverrides(value ={
			@AttributeOverride(name = "road", column = @Column(name = "avenue")),
			@AttributeOverride(name = "zip", column = @Column(name = "postal_code"))
	})
	//eger sadece birtane kolon icin isim override edilecekse @AttributeOverride kullanilabilir.
	//birden fazla @AttributeOverride 'i alt alta kullanmak legal degildir.
	@AttributeOverride(name = "city", column = @Column(name = "province"))
	private Address address;

	//constructors
        //getters and setters
        //toString
		
}

Company sinifimizi inceleyecek olursak @AttributeOverrides kullanarak 2 tane kolon icin override islemi gerceklestirdik. “road” degerini “avenue” ve “zip” degerini “postal_code” olarak override ettik.

Benzer sekilde @AttributeOverride kullanarak da sadece bir kolon icin override islemi yapilabilir. “city” degeri yerine “province” degeri override edildi. Alt alta @AttributeOverride kullanamayiz burada oldugu gibi eger birden fazla kolon icin override islemi yapacaksak @AttributeOverrides kullanmamiz gereklidir.

CompanyTest.java

package _21.embedded.objects.test;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import _21.embedded.objects.model.Address;
import _21.embedded.objects.model.Company;
import _21.embedded.objects.service.AddressService;
import _21.embedded.objects.service.AddressServiceImpl;
import _21.embedded.objects.service.CompanyService;
import _21.embedded.objects.service.CompanyServiceImpl;

public class CompanyTest {

	public static void main(String[] args) {
		EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("EmployeePersistenceUnit");
		EntityManager entityManager = entityManagerFactory.createEntityManager();
		EntityTransaction entityTransaction = entityManager.getTransaction();

		CompanyService companyService = new CompanyServiceImpl(entityManager);

		entityTransaction.begin();
		Company company = companyService.createCompany("The Injavawetrust Company");
		entityTransaction.commit();

		System.out.println("Persisted :" + company);

		AddressService addressService = new AddressServiceImpl();
		Address address = addressService.createAddress("company street", "myAvenue", "100", "Istanbul", "34000");

		entityTransaction.begin();
		company.setAddress(address);
		entityTransaction.commit();

		entityManager.close();
		entityManagerFactory.close();

	}
}

Ornegimizi calistirdigimizda Company tablomuz olusacaktir. Tablomuza baktigimizda kolon isimlerinin @AttributeOverrides ve @AttributeOverride annotation i kullanarak guncelledimiz/override ettigimiz yeni isimler oldugunu gorebiliriz.

embeddable company

Kaynak kodlar : Injavawetrust.jpa_v10

Yazimi burada sonlandiriyorum.
Herkese Bol Javali Gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
OCP, Java SE 6 Programmer
OCE, Java EE 6 Web Component Developer

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *