EclipseLink – 15 – ElementCollection – Map – 02

Merhaba Arkadaslar,
Onceki bolumde @ElementCollention annotation’ini Map ile kullanimini incelemeye basladik.
Onceki bolumlerde List<Vacation> vacations seklinde elemanlari Embeddable Vacation olan List kullanmistik benzer sekilde Map de kullanabiliriz.

@ElementCollection

Eger generic yapi kullanilmazsa bu durumda targetClass parametresi
kullanilmalidir.

@ElementCollection(targetClass=Vacation.class)
@JoinTable annotation'i oldugu gibi benzer sekilde @CollectionTable
annotation'ini kullanabiliriz. "
@CollectionTable(name = "EMP_VACATIONS_MAP", joinColumns = @JoinColumn(name = "EMP_ID"))
Embeddable sinifimiz icin karsilik gelen kolon/column isimlerini
guncelleyebiliriz.

Employee25.java

package _25.elementCollectionEmbeddableMap.model;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.persistence.AttributeOverride;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;

@Entity
public class Employee25 {

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

	@ElementCollection
	// Eger Generic yapi kullanilmazsa bu durumda targetClass parametresi
	// kullanilmalidir.
	// @ElementCollection(targetClass=Vacation.class)
	// @JoinTable annotation'i oldugu gibi benzer sekilde @CollectionTable
	// annotation'ini kullanabiliriz. "
	@CollectionTable(name = "EMP_VACATIONS_MAP", joinColumns = @JoinColumn(name = "EMP_ID"))
	// Embeddable sinifimiz icin karsilik gelen kolon/column isimlerini
	// guncelleyebiliriz.
	@AttributeOverride(name = "days", column = @Column(name = "offDays"))
	private Map<String, Vacation> vacations;

	//constructors
	//getters and setter
	//toString

	
}

Vacation.java

package _25.elementCollectionEmbeddableMap.model;

import java.util.Date;

import javax.persistence.Embeddable;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Embeddable
public class Vacation {

	@Temporal(TemporalType.DATE)
	private Date startDate;
	private int days;

	//constructors
	//getters and setter
	//toString

}

EmployeeTest.java

package _25.elementCollectionEmbeddableMap.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 _25.elementCollectionEmbeddableMap.model.Employee25;
import _25.elementCollectionEmbeddableMap.model.Vacation;
import _25.elementCollectionEmbeddableMap.service.EmployeeService;
import _25.elementCollectionEmbeddableMap.service.EmployeeServiceImpl;
import _25.elementCollectionEmbeddableMap.service.VacationService;
import _25.elementCollectionEmbeddableMap.service.VacationServiceImpl;

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();
		Employee25 employee = employeeService.createEmployee("Levent", "Erguder", 1000);
		entityTransaction.commit();

		VacationService vacationService = new VacationServiceImpl();
		Vacation vacation1 = vacationService.createVacation(new Date(), 5);

		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONTH, 2);

		Vacation vacation2 = vacationService.createVacation(cal.getTime(), 3);

		entityTransaction.begin();
		employee.getVacations().put("key1", vacation1);
		employee.getVacations().put("key2", vacation2);
		entityTransaction.commit();
                
                System.out.println("Persisted : " + employee);

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

	}
}

persistence.xml

<class>_25.elementCollectionEmbeddableMap.model.Employee25</class>	

embaddable value map

Son olarak Value olarak Entity oldugu durumu inceleyelim ;

Employee26.java

package _26.oneToManyMap.model;

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

@Entity
public class Employee26 {

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

	@ManyToOne
	private Department3 department;

	//constructors
	//getters and setter
	//toString

}

Department3.java

package _26.oneToManyMap.model;

import java.util.Map;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.MapKeyColumn;
import javax.persistence.OneToMany;

@Entity
public class Department3 {

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

	@OneToMany
	@JoinTable(name = "EMP_DEPT_MAP", 
	joinColumns = @JoinColumn(name = "EMP_ID"), 
	inverseJoinColumns = @JoinColumn(name = "DEPT_ID"))
	@MapKeyColumn(name = "emp_map_id")
	private Map<String, Employee26> employeesMap;

	//getters and setters

}

EmployeeTest.java

package _26.oneToManyMap.test;

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

import _26.oneToManyMap.model.Department3;
import _26.oneToManyMap.model.Employee26;
import _26.oneToManyMap.service.DepartmentService;
import _26.oneToManyMap.service.DepartmentServiceImpl;
import _26.oneToManyMap.service.EmployeeService;
import _26.oneToManyMap.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();
		Employee26 employee = employeeService.createEmployee("Levent", "Erguder", 1000);
		Employee26 employee2 = employeeService.createEmployee("James", "Gosling", 10000);
		Employee26 employee3 = employeeService.createEmployee("Joshua", "Bloch", 10000);
		entityTransaction.commit();

		DepartmentService departmentService = new DepartmentServiceImpl(entityManager);

		entityTransaction.begin();
		Department3 department = departmentService.createDepartment("Junior Developers Dept.");
		Department3 department2 = departmentService.createDepartment("Master Developers Dept.");
		entityTransaction.commit();
		

		entityTransaction.begin();

		employee.setDepartment(department);
		department.getEmployeesMap().put("key1", employee);

		employee2.setDepartment(department2);
		department2.getEmployeesMap().put("key2", employee2);

		employee3.setDepartment(department2);
		department2.getEmployeesMap().put("key3", employee3);

		entityTransaction.commit();
		
		System.out.println("Persisted :" + employee);
		System.out.println("Persisted :" + employee2);
		System.out.println("Persisted :" + employee3);
		System.out.println("Persisted :" + department);
		System.out.println("Persisted :" + department2);

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

	}
}

persistence.xml

<class>_26.oneToManyMap.model.Employee26</class>
<class>_26.oneToManyMap.model.Department3</class>

entity value

Kaynak kodlar : Injavawetrust.jpa
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 *