Student.java类
package cn.com.edu;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class Student {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String name;
private String sex;
@ManyToOne(cascade={CascadeType.MERGE},fetch=FetchType.EAGER)
private Project pro;
public Student() {
super();
}
public Student(String name, String sex) {
super();
this.name = name;
this.sex = sex;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Project getPro() {
return pro;
}
public void setPro(Project pro) {
this.pro = pro;
}
@Override
public String toString() {
return “Student [id=” + id + “, name=” + name + “, sex=” + sex + “]”;
}
}
Project.java类
package cn.com.edu;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class Project {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String name;
@OneToMany(cascade={CascadeType.ALL},mappedBy=“pro”,fetch=FetchType.LAZY)
private Set students=new HashSet();
public Project() {
super();
// TODO Auto-generated constructor stub
}
public Project(String name) {
super();
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set getStudents() {
return students;
}
public void setStudents(Set students) {
this.students = students;
}
}
hibernate.cfg.xml
< -hibernate-configuration>
org.hibernate.dialect.MySQLDialect
jdbc:mysql:///test?characterEncoding=utf8
root
root
com.mysql.jdbc.Driver
firstTest
true
true
< /hibernate-configuration>
hiberTest.java 测试类
package cn.com.edu;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class hiberTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Configuration con=new Configuration().configure();//读取解析配置文件
SessionFactory sf=con.buildSessionFactory(); //创建工厂,读取映射信息
Session session=sf.openSession();//打开Session
Transaction tr=session.beginTransaction(); //创建并开始一个事务
// function1(session);//全部查询
// function2(session);//参数绑定
// function3(session);//模糊查询
function4(session);//查询某些属性 投影查询
tr.commit();
session.close();
sf.close();
}
private static void function4(Session session) {
String hql="select s.name,s.sex from Student s";
Query query =session.createQuery(hql);
List
}
转自https://blog.csdn.net/Bancroft_boy/article/details/81348058