以下是我的代码在父母和学生之间有一对多的关系。 我的要求是:
class_name
时,应显示各自的父母信息 id
,则该父级的相应子级应已删除 我已经在测试类中保留了编码,因为我不知道如何编写查询以获得示例选择操作。如果DTO课程中有任何错误,请告诉我
select
操作查询:select pt.P_MOBILE,pt.P_EMAIL,st.S_FIRSTNAME,st.REF_ID
from parent_info pt join student_info st on pt.REF_ID = st.REF_ID
where st.S_CLASS_TO_JOIN= ?;
@Table(name = "parent_info")
public class ParentDTO {
@Id
@GenericGenerator(name = "j", strategy = "increment")
@GeneratedValue(generator = "j")
@Column(name = "P_ID")
private int p_id;
@Column(name = "P_NAME")
private String p_name;
@Column(name = "P_PHONE")
private String p_phone;
@Column(name = "P_EMAIL")
private String p_email;
@Column(name = "REF_ID")
private String ref_id;
@OneToMany(cascade={CascadeType.ALL})
@JoinColumn(name="parent_id")
private List<StudentDTO> students;
//getters and setters
}
@Table(name = "student_info")
public class StudentDTO {
@Id
@GenericGenerator(name = "j", strategy = "increment")
@GeneratedValue(generator = "j")
@Column(name = "S_ID")
private int s_id;
@Column(name = "S_NAME")
private String s_name;
@Column(name = "S_PHONE")
private String s_phone;
@Column(name = "S_EMAIL")
private String s_email;
@Column(name = "REF_ID")
private String ref_id;
@Column(name = "S_CLASS_NAME")
private String s_class_name;
@ManyToOne
@JoinColumn(name="parent_id")
private ParentDTO parent;
//getters and settrs
}
选择查询的测试类: 现在尝试:意外的令牌:靠近第1行,第90列[从com.pradeepdemo.StudentDTO中选择s连接获取s.parent,其中s.s_class_name = 10th]
public class Test {
public static void main(String[] args) {
Session session = null;
Transaction tx = null;
List<StudentDTO> groupList = null;
try {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
session = sessionFactory.openSession();
session.beginTransaction();
Query query = session.createQuery("select s from StudentDTO s join fetch s.parent where s.s_class_name = 10th");
groupList = query.list();
session.getTransaction().commit();
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
session.close();
}
}
}
要删除查询的测试类:
public class Test {
public static void main(String[] args) {
Session session = null;
Transaction tx = null;
try {
SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
session = sessionFactory.openSession();
session.beginTransaction();
session.getTransaction().commit();
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
session.close();
}
}
}
答案 0 :(得分:0)
尝试阅读此链接上的示例,以了解如何使用HQL: Hibernate - Query Language&amp; Hibernate Query examples (HQL)
编辑1
正如您可以在文档中阅读:Forms of join syntax
HQL支持两种形式的关联加入:隐式和显式。
答案 1 :(得分:0)
您可以使用@Entity注释您的模型类,所有内容都应该自行排序。 Hibernate应该独立完成基本的CRUD操作,而不必编写HQL。
http://www.tutorialspoint.com/hibernate/hibernate_annotations.htm
答案 2 :(得分:0)
工作代码
首先在两个实体之间进行映射,如下所示
<强>父强>
@Entity
@Table(name="PARENT")
public class ParentDTO {
@Id
@GeneratedValue
@Column(name="ID")
private Long parentId;
@Column(name="firstname")
private String parentName;
@Column(name="ParentPhone")
private Long parentPhone;
@Column(name="ParentEMail")
private String parentEMail;
@Column(name="ReferenceID")
private String referenceID;
@OneToOne(mappedBy="parent", cascade=CascadeType.ALL)
private Student student;
public ParentDTO() {
}
public ParentDTO(Long parentId, String parentName, Long parentPhone,
String parentEMail, String referenceID, Student student) {
super();
this.parentId = parentId;
this.parentName = parentName;
this.parentPhone = parentPhone;
this.parentEMail = parentEMail;
this.referenceID = referenceID;
this.student = student;
}
//getters and setters
}
<强>学生强>
@Entity
@Table(name = "STUDENT")
public class StudentDTO {
@Id
@Column(name="ID", unique=true, nullable=false)
@GeneratedValue(generator="gen")
@GenericGenerator(name="gen", strategy="foreign", parameters=@Parameter(name="property", value="parent"))
private Long id;
@Column(name = "StudentName")
private String studentName;
@Column(name = "StudentClass")
private int studentClass;
@Column(name = "ReferenceID")
private String referenceID;
@OneToOne
@PrimaryKeyJoinColumn
private ParentDTO parent;
public Student() {
}
public Student(Long id, String studentName, int studentClass,
String referenceID, ParentDTO parent) {
super();
this.id = id;
this.studentName = studentName;
this.studentClass = studentClass;
this.referenceID = referenceID;
this.parent = parent;
}
//getters and setters
}
选择查询
public void select()
{
Session session = null;
Transaction tx = null;
try {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
String hql = "from ParentDTO as p, StudentDTO as s where p.referenceID=s.referenceID and s.referenceID='ReferencID-1'";
List<?> list = session.createQuery(hql).list();
for(int i=0; i<list.size(); i++) {
Object[] row = (Object[]) list.get(i);
ParentDTO parent = (ParentDTO)row[0];
Student student = (Student)row[1];
System.out.println(parent.getParentEMail());
System.out.println(student.getStudentName());
}
tx.commit();
} catch (HibernateException he) {
he.printStackTrace();
tx.rollback();
}
}