Collection Mapping Spring Data Jpa
For Explanation Watch Video:
Directory Structure
application.properties
#datasource
spring.datasource.url=jdbc:mysql://localhost:3306/new
spring.datasource.username=root
spring.datasource.password=root
#jpa
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
SpringBootDataJpaCollectionMappingApplication
package com.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootDataJpaCollectionMappingApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDataJpaCollectionMappingApplication.class, args);
}
}
Employee
package com.app.entity;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.MapKeyColumn;
import javax.persistence.OrderColumn;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "emptab")
public class Employee {
@Id
@Column(name = "eid")
private Integer empId;
@Column(name = "ename")
private String empName;
@ElementCollection
@CollectionTable(
name = "emp_friends",//child tab name
joinColumns = @JoinColumn(name="eid")//FK col name
)
@OrderColumn(name = "pos")
@Column(name = "frd_name")
private List<String> friends;
@ElementCollection
@CollectionTable(
name = "emp_cards",//child tab name
joinColumns = @JoinColumn(name="eid")//FK col name
)
@MapKeyColumn(name = "cname")
@Column(name = "cno")
private Map<String,String> cards;
@ElementCollection
@CollectionTable(
name = "emp_proj",//child tab name
joinColumns = @JoinColumn(name="eid")//FK col name
)
@Column(name = "projName")
private Set<String> projects;
}
EmployeeRepository
package com.app.repo;
import org.springframework.data.jpa.repository.JpaRepository;
import com.app.entity.Employee;
public interface EmployeeRepository extends JpaRepository<Employee, Integer>{
}
TestRunner
package com.app.runner;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import com.app.entity.Employee;
import com.app.repo.EmployeeRepository;
@Component
public class TestRunner implements CommandLineRunner{
@Autowired
private EmployeeRepository repo;
@Override
public void run(String... args) throws Exception {
//friends names
List<String> list = Arrays.asList("jhon","angela");
//cards info
Map<String,String> map = Map.of("aadhar","1234","pan","MK34");
//proj names
Set<String> set = Set.of("p1","p2");
repo.save(new Employee(101,"sam",list,map,set));
repo.findAll().forEach(System.out::println);
}
}
Comments
Post a Comment