EclipseLink – 04 – Mapping Simple Types

Merhaba Arkadaslar
Bu bolumde su konu basliklarini inceleyecegiz.

  • Mapping to a Table , @Table annotation
  • Mapping Simple Types
  • Column Mapping , @Column
  • Enumerated Types , @Enumerated
  • Temporal Types , @Temporal
  • Large Objects , @Lob
  • Lazy Fetching , @Basic(fetch=FetchType.LAZY)

Mapping to a Table

Simdiye kadar tablo ismi ile sinif ismini birebir eslesecek sekilde kullandik. Yani varsayilan olarak sinif ismi neyse tablomuzun ismi de o oldu. Peki kendimiz tablomuza isim veremez miyiz ? Bunun icin @Table annotation’i kullanmamiz yeterli olacaktir.

Table annotation name , catalog , schema gibi atribute’ler alir.
name , Tablonun adi
catalog , katalog adi
schema , sema adi

Employee5.java

package _05.mapping.table.model;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="EMP5")
public class Employee5 {

	@Id
	private int id;
	private String name;
	private String surname;
	private int salary;

	public Employee5() {
		super();
	}

	public Employee5(int id, String name, String surname, int salary) {
		super();
		this.id = id;
		this.name = name;
		this.surname = surname;
		this.salary = salary;
	}

	//getters setter
       //toString
}

Ornegimizi calistirdigimizda veritabanimizda EMP5 tablomuz olustu.

table annotation

MySQL icin schema kavrami vardir. schema attribute kullanirsak , ilgili schema da “EMP5” tablomuz olusur. Tabi burada ilgili schema (another.schema) tanimli olmalidir.

package _05.mapping.table.model;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="EMP5" ,schema="another.schema")
public class Employee5 {

	//...

	//getter setter
}

Mapping Simple Types

Simdiye kadar orneklerimizde int , String gibi instance variable/field kullandik. Entity Persistent Field(instance variable) / Properties(getter/setter) su tiplerde olabilir.

  • Primitive/ilkel tipler : byte, int, short, long, boolean, char, float, double
  • Primitive tiplerin Wrapper karsiligi: Byte, Integer, Short, Long, Boolean, Character,
    Float, Double
  • Byte and character array/diziler : byte[], Byte[], char[], Character[]
  • Buyuk numeric tipler: java.math.BigInteger, java.math.BigDecimal
  • Strings : java.lang.String
  • Java temporal tipler: java.util.Date, java.util.Calendar
  • JDBC temporal tipler : java.sql.Date, java.sql.Time, java.sql.Timestamp
  • Enumerated tipler
  • Serializable objeler

Burada listeledigimiz basit tipler(simple types) ile yapilan mapping basic mapping olarak adlandirilir. Basic mapping icin opsiyonel olarak @Basic annotation kullanilabilir.

Column Mappings

@Table annotation’i kullanarak tablomuzun varsayilan ismini override ettik. Benzer sekilde @Column annotation’i kullanarak column/kolon ismini degistirebiliriz.

Employee6.java

package _06.mapping.column.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Employee6 {

	@Id
	@Column(name = "emp_id")
	private int id;
	@Column(name = "first_name")
	private String name;
	@Column(name = "last_name")
	private String surname;
	@Column(name = "wage")
	private int salary;

	public Employee6() {
		super();
	}

	public Employee6(int id, String name, String surname, int salary) {
		super();
		this.id = id;
		this.name = name;
		this.surname = surname;
		this.salary = salary;
	}

	//getters and setters
        //toString

}

Employee6 tablomuza baktigimizda column isimlerinin @Column annotation ile override ettigimiz degerleri gorebiliriz.

column annotation

Enumerated Types

Bir baska simple type ornegi olarak enum yapisini kullanabiliriz. Ornek olarak EmployeeType isminde bir enum yapimiz olsun ve su sekilde degerler olsun.

EmployeeType.java

package _07.mapping.enumerated.model;

public enum EmployeeType {
	FULL_TIME_EMPLOYEE, PART_TIME_EMPLOYEE, CONTRACT_EMPLOYE
}

Employee7.java

package _07.mapping.enumerated.model;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;

@Entity
public class Employee7 {

	@Id
	private int id;
	private String name;
	private String surname;
	private int salary;
	private EmployeeType employeeType;

	public Employee7() {
		super();
	}

	public Employee7(int id, String name, String surname, int salary, EmployeeType employeeType) {
		super();
		this.id = id;
		this.name = name;
		this.surname = surname;
		this.salary = salary;
		this.employeeType = employeeType;
	}

	//getters and setters
        //toString
}

Veritabanina baktigimizda EMPLOYEETYPE kolonunda 0 , 1 , 2 degerlerini goruruz. Varsayilan olarak enum degerleri ordinal/sirali olarak veritabanina kaydedilir.Bu durumda FULL_TIME_EMPLOYEE 0 a , PART_TIME_EMPLOYEE 1 e , CONTRACT_EMPLOYE 2 ye karsilik gelir.

enumerated jpa

@Enumerated annotation EnumType olarak 2 tane deger alabilir.

  • EnumType.ORDINAL
  • EnumType.STRING

Eger EnumType.ORDINAL yani 0 ,1 ,2 seklinde kullanmak yerine enum’larin String karsiligini kullanmak istersek bu durumda @Enumerated(EnumType.STRING) annotation’i kullanmamiz gerekir.

Employee7.java

package _07.mapping.enumerated.model;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;

@Entity
public class Employee7 {

	@Id
	private int id;
	private String name;
	private String surname;
	private int salary;

	@Enumerated(EnumType.STRING)
	// @Enumerated(EnumType.ORDINAL)
	// varsayilan olarak ORDINAL olarak calisir.
	// index 0 dan baslar!
	private EmployeeType employeeType;

	public Employee7() {
		super();
	}

	public Employee7(int id, String name, String surname, int salary, EmployeeType employeeType) {
		super();
		this.id = id;
		this.name = name;
		this.surname = surname;
		this.salary = salary;
		this.employeeType = employeeType;
	}

	//getters and setters
        //toString
}

enumerated jpa string

Temporal Types

Temporal Types sunlardir ;

  • java.sql.Date
  • java.sql.Time
  • java.sql.Timestamp
  • java.util.Date
  • java.util.Calendar

Bu tiplerle birlikte  @Temporal annotation kullanilabilir. @Temporal annotation’i enumerated type olarak DATE , TIME , TIMESTAMP degerlerini alir.

  • @Temporal(TemporalType.DATE) , year month day
  • @Temporal(TemporalType.TIME) , hour minute second
  • @Temporal(TemporalType.TIMESTAMP) , year month day hour minute second

Employee8.java

package _08.mapping.temporal.model;

import java.util.Calendar;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
public class Employee8 {

	@Id
	private int id;
	private String name;
	private String surname;
	private int salary;
	@Temporal(TemporalType.DATE)
	// year month day
	private Date startDate;
	// @Temporal(TemporalType.TIME) -> hour minute second
	@Temporal(TemporalType.TIMESTAMP)
	// year month day hour minute second
	private Calendar birthDay;

	public Employee8() {
		super();
	}

	public Employee8(int id, String name, String surname, int salary, Date startDate, Calendar birthDay) {
		super();
		this.id = id;
		this.name = name;
		this.surname = surname;
		this.salary = salary;
		this.startDate = startDate;
		this.birthDay = birthDay;
	}

	//getter setter

}

EmployeeTest.java

package _08.mapping.temporal.test;

import java.util.Calendar;
import java.util.Date;

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

import _08.mapping.temporal.dao.EmployeeDAO;
import _08.mapping.temporal.dao.EmployeeDAOImpl;
import _08.mapping.temporal.model.Employee8;

public class EmployeeTest {

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

	EmployeeDAO employeeService = new EmployeeDAOImpl(entityManager);

	entityTransaction.begin();

	Calendar birthCalendar = prepareCalendarAndDate(1989, 5, 18);
	// 18.06.1989
	Date startDate = prepareCalendarAndDate(2013, 9, 7).getTime();
	// 07.10.2013

	Calendar birthCalendar2 = prepareCalendarAndDate(1955, 4, 19);
	Date startDate2 = prepareCalendarAndDate(2011, 7, 1).getTime();

	Calendar birthCalendar3 = prepareCalendarAndDate(1961, 7, 28);
	Date startDate3 = prepareCalendarAndDate(2004, 6, 1).getTime();

	Employee8 employee = employeeService.insertEmployee(1, "Levent", "Erguder", 1000,
		startDate, birthCalendar);
	Employee8 employee2 = employeeService.insertEmployee(2, "James", "Gosling", 10000,
		startDate2, birthCalendar2);
	Employee8 employee3 = employeeService.insertEmployee(3, "Joshua", "Bloch", 10000,
		startDate3, birthCalendar3);
	entityTransaction.commit();

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

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

    }

    public static Calendar prepareCalendarAndDate(int year, int month, int day) {
	Calendar calendar = Calendar.getInstance();
	calendar.set(Calendar.YEAR, year);
	calendar.set(Calendar.MONTH, month);// Not : Ocak/June --> 0 dan baslar.
	calendar.set(Calendar.DAY_OF_MONTH, day);

		return calendar;
	}
}

temporal type

Large Objects

Farkli veritabanlarinda farkli veri tipleri icin alabilecegi/tutabilecegi veri belli kapasitededir.
Large Object veri tipleri ile ,veritabaninda bir column gigabyte seviyesinde veriye karsilik gelebilir.
Bunun icin kullanilan veri tipleri BLOB ve CLOB tur.

  • BLOB – binary large object , byte[] Byte[] , Serializable types
  • CLOB – character large object , char[], Character[], String

MySQL icin BLOB ornegi kullanabiliriz, CLOB’u Oracle’da kullanabiliriz.
Ornegin calisanlarin resim/picture bilgisini veritabaninda tutmak istersek byte[] veri yapisinda BLOB olarak tutabiliriz.

Employee9.java

package _09.mapping.lob.model;

import java.util.Arrays;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;

@Entity
public class Employee9 {

	@Id
	private int id;
	private String name;
	private String surname;
	private int salary;	
	@Lob
	private byte[] picture;

	Employee9() {
		super();
	}

	public Employee9(int id, String name, String surname, int salary, byte[] picture) {
		super();
		this.id = id;
		this.name = name;
		this.surname = surname;
		this.salary = salary;
		this.picture = picture;
	}
        
        //getter setter

}

EmployeeTest.java

package _09.mapping.lob.test;

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

import _09.mapping.lob.dao.EmployeeDAO;
import _09.mapping.lob.dao.EmployeeDAOImpl;
import _09.mapping.lob.model.Employee9;

public class EmployeeTest {

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

		EmployeeDAO employeeService = new EmployeeDAOImpl(entityManager);

		byte[] picture = { 1, 5, 6, 7, 2, 5, 10, 22, 40, 50 };
		// picture byte , sample

		entityTransaction.begin();
		Employee9 employee = employeeService.createEmployee(1, "Levent", "Erguder", 1000, picture);
		Employee9 employee2 = employeeService.createEmployee(2, "James", "Gosling", 10000, picture);
		Employee9 employee3 = employeeService.createEmployee(3, "Joshua", "Bloch", 10000, picture);
		entityTransaction.commit();

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

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

	}
}

blob

Lazy Fetching

Fetch , gidip getirmek anlamina gelmektedir. Bir tabloda bazi alanlara ihtiyacimiz cok nadiren olabilir. Boyle durumlarda performans arttirabilmek icin Lazy Fetching yaklasimini kullanabiliriz.

Lazy Fetching , Lazy Loading , Deferred Loading , On-Demand Fetching hepsi ayni anlama gelmektedir. Aslinda bu kavram sadece veritabani icin olan bir kavram degildir.
Burada genel mantik ilgili veri/alan/field gercekten ihtiyac duyuldugunda getirilmesi/hazir edilmesi calistirilmasidir.

Lazy Fetching ‘i @Basic annotation’i kullanarak yapabiliriz. @Basic annotation’i 2 tane FetchType enumerated type almaktadir.

  • @Basic(fetch=FetchType.LAZY)
  • @Basic(fetch=FetchType.EAGER)

Varsayilan olarak Basic Mapping/Simple Types icin FetchType.EAGER dir. Pratikte Basic Mapping/Simple Types icin Lazy Fetching pek kullanilan bir yaklasim degildir. Bunun yerine tablolar arasinda iliskiler soz konusu oldugunda ve tablolarda yuzlerde alan oldugunda performans icin kullanisli olacaktir.

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 *