package com.employee;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
@Entity
@Table(name ="Employee")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "sequence")
@SequenceGenerator(name = "sequence", sequenceName = "seq1", allocationSize = 1)
@Column(name = "SYS_I")
private Integer id;
@Column(name = "emp_id")
private String empId;
@Column(name = "emp_name")
private String empName;
@Column(name = "branch")
private String emp_branch;
@Column(name = "course")
private String course;
@Column(name = "head")
private String head;
}
package com.employeebranch;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
@Entity
@Table(name ="EmployeeBranch")
public class EmployeeBranch {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "branchSequence")
@SequenceGenerator(name = "branchSequence", sequenceName = "bran_seq1", allocationSize = 1)
@Column(name = "SYS_I")
private Integer id;
@Column(name = "emp_branch")
private String emp_branch;
@Column(name = "emp_course")
private String course;
@Column(name = "emp_head")
private String empHead;
}enter image description here
谁能展示我如何使用 @OneToOne 或 @OneToMany 或 @ManyToOne 等注释映射字段。在这种情况下是一种什么样的关系?
所以现在在employee分支表中,值如下所示。id emp_branch emp_course emp_head 1 机械物理 校长 2 机械化学 校长 3 电气物理 校长 4 电气化学 校长
在员工表中,值如下所示。id emp_id emp_name 分支课程负责人 1 10 sachin 机械物理 001 4 12 Yuvraaj 机械化学 002在此处输入图像描述
select e.*, b.emp_head from EmployeeBranch b , Employee e where e.branch=b.emp_branch
and e.course = b.emp_course and e.branch = 'mechanical';
我想使用 createQuery 在 Employee 实体中设置这个结果。我怎样才能实现这个?
注意:我只想通过映射 onetomany 或 onetoone 或 manytoone 来使用条件或 createQuery。当数据属于上述类型时,我很困惑。我不想使用 JPA 存储库或 HQL 或本机查询