Hibernate注解的使用
配置Weapon.Java,代码如下:
- package com.hibernate.annotations.entity;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.EntityResult;
- import javax.persistence.FieldResult;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.NamedNativeQuery;
- import javax.persistence.NamedQuery;
- import javax.persistence.SequenceGenerator;
- import javax.persistence.SqlResultSetMapping;
- import javax.persistence.Table;
- @Entity
- @Table(name = "WEAPON")
- //通过注解的方式使用命名查询
- @NamedQuery(name = "namedQuery",query="from Weapon where id < :id")
- //通过注解的方式使用本地查询
- @SqlResultSetMapping(name = "all_fields",entities={
- @EntityResult(entityClass= com.hibernate.annotations.entity.Weapon.class,
- fields={
- @FieldResult(name = "id",column="ID"),
- @FieldResult(name = "name",column="NAME"),
- }
- )
- }
- )
- @NamedNativeQuery(name = "nameeNativeQuery", query = "select * from WEAPON where id < :id",
- resultSetMapping="all_fields")
- public class Weapon {
- @SequenceGenerator(name = "seq",allocationSize = 1,initialValue = 1,sequenceName = "seq_weapon_pk")
- @Id
- @Column(name = "ID")
- @GeneratedValue(strategy=GenerationType.SEQUENCE,generator = "seq")
- private int id;
- private String 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;
- }
- }
关于注解的测试举例,TestAnnotations.java,代码如下:
[java] view plain copy print?
- package com.hibernate.annotations.entity;
- import java.util.Iterator;
- import java.util.List;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.AnnotationConfiguration;
- public class TestAnnotations {
- public static AnnotationConfiguration config = new AnnotationConfiguration();
- public static SessionFactory sessionFactory ;
- public static String config_file = "/hibernate.cfg.xml";
- public static void main(String[] args) {
- // annotationTest1();
- // annotationTest2();
- annotationTest3();
- }
- public static void annotationTest1(){
- //获得配置文件信息
- config.configure(config_file);
- //创建session工厂
- sessionFactory = config.buildSessionFactory();
- //创建Session对象(注意:正常开发环境下,应该优先当前进程中的session)
- Session session = sessionFactory.openSession();
- //开启事务
- Transaction tran = session.beginTransaction();
- Weapon weapon = new Weapon();
- weapon.setName("屠鹰刀");
- session.save(weapon);
- tran.commit();
- session.close();
- }
- //使用命名查询进行数据查询
- public static void annotationTest2(){
- //获得配置文件信息
- config.configure(config_file);
- //创建session工厂
- sessionFactory = config.buildSessionFactory();
- //创建Session对象(注意:正常开发环境下,应该优先当前进程中的session)
- Session session = sessionFactory.openSession();
- Query query = session.getNamedQuery("namedQuery");
- query.setParameter("id", 100);
- List<Weapon> list = query.list();
- Iterator<Weapon> it = list.iterator();
- while(it.hasNext()){
- Weapon w = it.next();
- System.out.println(w.getId() + "\t" + w.getName());
- }
- session.close();
- }
- //使用本地查询进行数据查询
- public static void annotationTest3(){
- //获得配置文件信息
- config.configure(config_file);
- //创建session工厂
- sessionFactory = config.buildSessionFactory();
- //创建Session对象(注意:正常开发环境下,应该优先当前进程中的session)
- Session session = sessionFactory.openSession();
- Query query = session.getNamedQuery("nameeNativeQuery");
- query.setParameter("id", 100);
- List<Weapon> list = query.list();
- Iterator<Weapon> it = list.iterator();
- while(it.hasNext()){
- Weapon w = it.next();
- System.out.println(w.getId() + "\t" + w.getName());
- }
- session.close();
- }
- }