注解,关联映射
一、一对多
[java] @Id
private Integer deptno;
@Column(name="dname")
private String dname;
@Column(name="loc")
private String loc;
@OneToMany(cascade={CascadeType.ALL},targetEntity=Emp.class)
@JoinColumn(name="deptno",updatable=false)
private Set emps = new HashSet();
[java]
@Column(name="deptno")
private Integer dept;
@Column(name = "ename")
private String ename;
@Column(name = "job")
private String job;
@Column(name = "mgr")
private Short mgr;
@Column(name = "hiredate")
private Date hiredate;
@Column(name = "sal")
private Double sal;
@Column(name = "comm")
private Double comm;
@Version
@Column(name = "version")
private Integer version;
二、多对一
[java]
<span style="white-space:pre"> </span>@Id
@GeneratedValue(generator = "emp", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name="emp",sequenceName="emp_seq",allocationSize=1)
private Integer empno;
@ManyToOne(targetEntity=Dept.class)
@JoinColumn(name = "deptno", updatable = false)
@Basic(fetch = FetchType.LAZY)
/* @Basic注解可以声明属性的获取策略(fetch strategy) */
private Dept dept;
@Column(name = "ename")
private String ename;
@Column(name = "job")
private String job;
@Column(name = "mgr")
private Short mgr;
@Column(name = "hiredate")
private Date hiredate;
@Column(name = "sal")
private Double sal;
@Column(name = "comm")
private Double comm;
@Version
@Column(name = "version")
private Integer version;
[java]
@Id
private Integer deptno;
@Column(name="dname")
private String dname;
@Column(name="loc")
private String loc;
三、多对一、一对多双向
[java]
@Id
private Integer deptno;
@Column(name="dname")
private String dname;
@Column(name="loc")
private String loc;
@OneToMany(cascade={CascadeType.ALL},targetEntity=Emp.class)
@JoinColumn(name="deptno",updatable=false)
private Set emps = new HashSet();
[java]
<span style="white-space:pre"> </span>@Id
@GeneratedValue(generator = "emp", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name="emp",sequenceName="emp_seq",allocationSize=1)
private Integer empno;
@ManyToOne(targetEntity=Dept.class)
@JoinColumn(name = "deptno", updatable = false)
@Basic(fetch = FetchType.LAZY)
/* @Basic注解可以声明属性的获取策略(fetch strategy) */
private Dept dept;
@Column(name = "ename")
private String ename;
@Column(name = "job")
private String job;
@Column(name = "mgr")
private Short mgr;
@Column(name = "hiredate")
private Date hiredate;
@Column(name = "sal")
private Double sal;
@Column(name = "comm")
private Double comm;
@Version
@Column(name = "version")
private Integer version;
注:当属性名和数据库字段名不一致时,要显示指定@Column
使用 @Column 注解可将属性映射到列. 使用该注解来覆盖默认值(关于默认值请参考EJB3规范). 在属性级使用该注解的方式如下:
①不进行注解
②和 @Basic一起使用
③和 @Version一起使用
④和 @Lob一起使用
⑤和 @Temporal一起使用
⑥和 @org.hibernate.annotations.CollectionOfElements一起使用 (只针对Hibernate )
四、本地化查询
[java]
@NamedNativeQuery(name="dept&emp",query="select d.*,e.* from dept d,emp e where e.deptno=d.deptno",resultSetMapping="joinMapping")
@SqlResultSetMapping(name="joinMapping",entities={
@EntityResult(entityClass=Dept.class),
@EntityResult(entityClass=Emp.class)
}
)
[java]
List<Object[]> l=sess.getNamedQuery("dept&emp").list();
for (Object[] objects : l) {
Dept d=(Dept)objects[0];
Emp e=(Emp)objects[1];
System.out.println(d.getDname()+" "+e.getEname());
}
五、HQL查询
[java]
@NamedQuery(name = "sqlquery", query = "select e from Emp e where e.dept.deptno = :dno")
[java]
sess.getNamedQuery("sqlquery").setInteger("dno", 10).list().size()
摘自 hanzhou4519
补充:软件开发 , Java ,