Employee.java
@Table(name="EMPLOYEE")
public class Employee {
@Id
@Column(name = "wid")
@GeneratedValue(strategy= GenerationType.AUTO)
private Integer wid;
@Column(name = "wname")
private String wname;
@ManyToOne(optional = false)
@JoinColumn(name = "bidd")
private BloodGroup bloodgroup;
@ManyToOne(optional = false)
@JoinColumn(name = "sidd")
private Section section;
.....
//Getters And Setters
}
BloodGroup.java
@Entity
@Table(name="BLOODGROUP")
public class BloodGroup {
@Id
@Column(name = "bid")
@GeneratedValue (strategy= GenerationType.AUTO)
private Integer bid;
@Size(min=1,max = 30)
@Column(name = "blood_name")
private String blood_name;
@OneToMany(mappedBy = "bloodgroup",fetch=FetchType.LAZY,
targetEntity=Employee.class, cascade=CascadeType.PERSIST)
private List<Employee> emp;
.....
//Getters And Setters
}
Section.java
@Entity
@Table(name = "SECTION")
public class Section {
@Id
@Column(name = "sid")
@GeneratedValue (strategy = GenerationType.AUTO)
private Integer sid;
@Column(name = "sname")
@Size(min = 2 , max = 30)
private String sname;
@OneToMany(mappedBy = "section",fetch=FetchType.LAZY,
targetEntity=Employee.class, cascade=CascadeType.PERSIST)
private List<Employee> emp;
.....
//Getters And Setters
}
AbstractDao.java
public abstract class AbstractDao<PK extends Serializable, T> {
private final Class<T> persistentClass;
@SuppressWarnings("unchecked")
public AbstractDao(){
this.persistentClass =(Class<T>) ((ParameterizedType)
this.getClass().getGenericSuperclass()).getActualTypeArguments()[1];
}
@Autowired
private SessionFactory sessionFactory;
protected Session getSession(){
return sessionFactory.getCurrentSession();
}
@SuppressWarnings("unchecked")
public T getByKey(PK key) {
return (T) getSession().get(persistentClass, key);
}
public void persist(T entity) {
getSession().persist(entity);
}
public void update(T entity) {
getSession().update(entity);
}
public void delete(T entity) {
getSession().delete(entity);
}
protected Criteria createEntityCriteria(){
return getSession().createCriteria(persistentClass);
}
}
BloodGroupDaoImpl.java
@Repository("bloodDao")
public class BloodDaoImpl extends AbstractDao<Integer, BloodGroup>
implements BloodDao {
static final Logger logger = LoggerFactory.getLogger(UserDaoImpl.class);
public void save(BloodGroup bloodgroup) {
persist(bloodgroup);
}
@SuppressWarnings("unchecked")
public List<BloodGroup> listBloodGroup() {
Criteria criteria = createEntityCriteria();
return (List<BloodGroup>) criteria.list();
}
}
SectionDaoImpl.java
@Repository("sectionDao")
public class SectionDaoImpl extends AbstractDao<Integer,Section>implements
SectionDao{
@Override
public void save(Section section) {
persist(section);
}
@Override
public List<Section> allSection() {
Criteria criteria = createEntityCriteria();
return (List<Section>) criteria.list();
}
}
EmployeesDaoImpl.java
@Repository("employeeDao")
public class EmployeeDaoImpl extends AbstractDao<Integer,Employee>
implements EmployeeDao{
@Override
public void save(Employee employee) {
persist(employee);
}
@Override
public List<Employee> allEmployee() {
Criteria criteria = createEntityCriteria();
return (List<Employee>) criteria.list();
/*This shows list of employee
table with foreign key. **I want to show list of employee with correspondent
value of **BloobGroup** and **Section**.*/
}
}
BloodGroupServiceImpl.java
@Service("bloodGroupService")
@Transactional
public class bloodGroupServiceImpl implements bloodGroupService {
@Autowired
private BloodDao dao;
@Override
public void saveBloodGroup(BloodGroup bloodgroup) {
dao.save(bloodgroup);
}
public List<BloodGroup> allBloodGroup() {
return dao.listBloodGroup();
}
SectionServiceImpl.java
@Service("sectionService")
@Transactional
public class sectionServiceImpl implements sectionService{
@Autowired
private SectionDao dao;
@Override
public void save(Section section) {
dao.save(section);
}
@Override
public List<Section> allSection() {
return dao.allSection();
}
EmployeeServiceImpl.java
@Service("EmployeeService")
@Transactional
public class employeeServiceImpl implements employeeService{
@Autowired
private EmployeeDao dao;
@Autowired
private BloodDao bloodDao;
@Autowired
private SectionDao sectionDao;
@Override
public List<Employee> allEmployee() {
return dao.allEmployee();
}
@Override
public void save(Employee employee, int bloodgroup,int section) {
employee.setBloodgroup(bloodDao.findById(bloodgroup));
employee.setSection(sectionDao.findById(section));
dao.save(employee);
}
}
AppController.java
@Controller
@RequestMapping("/")
public class AppController {
@Autowired
employeeService wService;
@RequestMapping(value = { "/employee-list" }, method = RequestMethod.GET)
public String listWaheeb(ModelMap model) {
model.addAttribute("loggedinuser", getPrincipal());
List<Employee> emp = wService.allEmployee();
model.addAttribute("empList", emp);
return "allEmployee";
}
}
allEmployee.jsp
<div class="content">
<table id="mytable">
<tbody>
<c:forEach items="${empList}" var="eList">
<tr>
<td>${eList.wid}</td>
<td>${eList.wname}</td>
<!-- for BloodGroup and Section fields-->
</tr>
</c:forEach>
</tbody>
</table>
</div>
我的查询
选择employee.wname,bloodgroup.blood_name,section.sname 来自员工,血型,部门 employee.bidd = bloodgroup.bid 和employee.sidd = section.sid 和employee.wname ='abc';
如何在休眠标准中写这个以返回List