<file_sep>package com.itech.test.entity;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "category")
public class SportCategory {

	@Id
	@GeneratedValue
	private Long category_id;
	private String category_name;

	@OneToOne(mappedBy = "sportCategory")
	private Student student;

	@OneToMany(mappedBy = "sportCategory", cascade = CascadeType.ALL)
	private Set<Sport> sports = new HashSet<Sport>();

	public Long getCategory_id() {
		return category_id;
	}

	public void setCategory_id(Long category_id) {
		this.category_id = category_id;
	}

	public String getCategory_name() {
		return category_name;
	}

	public void setCategory_name(String category_name) {
		this.category_name = category_name;
	}

	public Set<Sport> getSports() {
		return sports;
	}

	public void setSports(Set<Sport> sports) {
		this.sports = sports;
	}

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

}
<file_sep>package com.itech.test.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.itech.test.entity.Sport;
import com.itech.test.service.StudentSportService;

@RestController
@RequestMapping(value = "/rest")
public class StudentSportController {

	@Autowired
	private StudentSportService sportService;

	@RequestMapping(value = "/getCategory/{studentId}")
	public @ResponseBody
	String getSportCategory(@PathVariable Long studentId) {
		return sportService.getSportCategory(studentId);
	}

	@RequestMapping(value = "/getSportList/{studentId}")
	public @ResponseBody
	List<String> getSportList(@PathVariable Long studentId) {
		List<String> sportNames = null;
		List<Sport> sports = sportService.getSportList(studentId);
		if (sports != null && sports.size() > 0) {
			sportNames = new ArrayList<String>();
			for (Sport sport : sports) {
				sportNames.add(sport.getSport_name());
			}
		}
		return sportNames;
	}
}
<file_sep>package com.itech.test.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itech.test.dao.StudentSportDAO;
import com.itech.test.entity.Sport;

@Service
public class StudentSportServiceImpl implements StudentSportService{
	
	@Autowired
	private StudentSportDAO sportDAO;

	@Transactional
	public String getSportCategory(Long studentId) {
		return sportDAO.getSportCategory(studentId);
	}

	@Transactional
	public List<Sport> getSportList(Long studentId) {
		return sportDAO.getSportList(studentId);
	}

}
<file_sep># springhibernate_poc
spring hibernate rest intergarion
<file_sep>package com.itech.test.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.itech.test.entity.Sport;
import com.itech.test.entity.SportCategory;
import com.itech.test.entity.Student;

@Repository
public class StudentSportDAOImpl implements StudentSportDAO {

	@Autowired
	private SessionFactory sessionFactory;

	public String getSportCategory(Long studentId) {
		String sportCategory = null;
		Student student = (Student) sessionFactory.getCurrentSession().get(
				Student.class, studentId);
		if (null != student) {
			SportCategory category = student.getSportCategory();
			if (category != null) {
				sportCategory = category.getCategory_name();
			}
		}
		return sportCategory;
	}

	public List<Sport> getSportList(Long studentId) {
		Set<Sport> sports = null;
		Student student = (Student) sessionFactory.getCurrentSession().get(
				Student.class, studentId);
		if (null != student) {
			sports = student.getSports();
		}
		List<Sport> list = new ArrayList<Sport>(sports);
		return list;
	}

}
