package org.hzy.entity; import java.util.HashSet; import java.util.Set; import javax.persistence.Basic; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToMany; import javax.persistence.SequenceGenerator; import javax.persistence.Table; @Entity @Table(name = "Dept", schema = "SCOTT") @SequenceGenerator(name = "ds", allocationSize = 1, sequenceName = "..dept_seq") public class Dept implements java.io.Serializable { private Integer deptno; private String dname; private String loc; private Set<Emp> emps=new HashSet<Emp>(); @Id @GeneratedValue(generator = "ds", strategy = GenerationType.SEQUENCE) public Integer getDeptno() { return this.deptno; } @Basic // 不加的话默认是@Basic,如果列名不同,则要加上@Column指定 public void setDeptno(Integer deptno) { this.deptno = deptno; } public String getDname() { return this.dname; } public void setDname(String dname) { this.dname = dname; } public String getLoc() { return this.loc; } public void setLoc(String loc) { this.loc = loc; } @OneToMany(fetch=FetchType.LAZY,targetEntity=Emp.class) @JoinColumn(name="deptno") public Set<Emp> getEmps() { return emps; } public void setEmps(Set<Emp> emps) { this.emps = emps; } public Dept() { } public Dept(Integer deptno, String dname, String loc) { this.deptno = deptno; this.dname = dname; this.loc = loc; } }
2.pojo(EMP)
package org.hzy.entity; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; /** * Emp entity. @author MyEclipse Persistence Tools */ @Entity @Table(name = "Emp", schema = "SCOTT") public class Emp implements java.io.Serializable { // Fields private Integer empno; private String ename; private String job; private Integer mgr; private Date hiredate; private Double sal; private Double comm; // @Column(name="deptno") // private Integer dept;//单表则可以,且是Integer类型 private Dept dept; @Id public Integer getEmpno() { return this.empno; } public void setEmpno(Integer empno) { this.empno = empno; } @ManyToOne(fetch = FetchType.LAZY, targetEntity = Dept.class) @JoinColumn(name = "deptno") public Dept getDept() { return this.dept; } public void setDept(Dept dept) { this.dept = dept; } public String getEname() { return this.ename; } public void setEname(String ename) { this.ename = ename; } public String getJob() { return this.job; } public void setJob(String job) { this.job = job; } public Integer getMgr() { return this.mgr; } public void setMgr(Integer mgr) { this.mgr = mgr; } public Date getHiredate() { return this.hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } public Double getSal() { return this.sal; } public void setSal(Double sal) { this.sal = sal; } public Double getComm() { return this.comm; } public void setComm(Double comm) { this.comm = comm; } public Emp() { } /* public Emp(Integer empno, Integer dept, String ename, String job, Integer mgr, Date hiredate, Double sal, Double comm) { this.empno = empno; this.dept = dept; this.ename = ename; this.job = job; this.mgr = mgr; this.hiredate = hiredate; this.sal = sal; this.comm = comm; }*/ public Emp(Integer empno, Dept dept, String ename, String job, Integer mgr, Date hiredate, Double sal, Double comm) { this.empno = empno; this.dept = dept; this.ename = ename; this.job = job; this.mgr = mgr; this.hiredate = hiredate; this.sal = sal; this.comm = comm; } }3.主配置文件:
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="dialect"> org.hibernate.dialect.Oracle9Dialect </property> <property name="hibernate.current_session_context_class"> thread </property> <property name="connection.url"> jdbc:oracle:thin:@localhost:1521:orcl </property> <property name="connection.username">hzy</property> <property name="connection.password">accp</property> <property name="connection.driver_class"> oracle.jdbc.OracleDriver </property> <property name="myeclipse.connection.profile"> oracle.jdbc.OracleDriver </property> <property name="show_sql">true</property> <property name="format_sql">true</property> <mapping class="org.hzy.entity.Dept" /> <mapping class="org.hzy.entity.Emp" /> </session-factory> </hibernate-configuration>
package org.hzy.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.cfg.Configuration; import org.hzy.entity.Dept; import org.hzy.entity.Emp; public class test1 { /** * @param args */ static Configuration config=new AnnotationConfiguration().configure(); static SessionFactory fac=config.buildSessionFactory(); static Session sess=fac.getCurrentSession(); public static void main(String[] args) { // TODO Auto-generated method stub Transaction tr=sess.beginTransaction(); Dept d=(Dept) sess.get(Dept.class, 10); System.out.println(d.getEmps().size()); // Emp emp=(Emp) sess.get(Emp.class, 7369); // System.out.println(emp.getDept().getDname()); tr.commit(); } }