hibernate注解
2015-12-06 14:11
399 查看
hibernate注解方式使我不需要在创建实体类的同时创建*.hbm.xml来映射数据库中对应表格,配置如下代码中所示:
Dept.java
Emp.java
hibernate.cfg.xml中的配置如下:
View Code
需要注意<mapping>中class属性值的引用方式,这是和引用*.hbm.xml的不同之处。
测试:
需要注意,获取Session工厂的方法与之前的不一样。
以上就是hibernate逐渐的简单使用。
Dept.java
package com.entity; import java.util.HashSet; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.SequenceGenerator; import javax.persistence.Table; import javax.persistence.Transient; @Entity @Table(name="dept") public class Dept implements java.io.Serializable { // Fields private Integer id; private String name; private String location; private Set<Emp> emps = new HashSet<Emp>(0); // Constructors /** default constructor */ public Dept() { } /** full constructor */ public Dept(String name, String location, Set emps) { this.name = name; this.location = location; this.emps = emps; } // Property accessors @Id @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="seq_dept") @SequenceGenerator(name="seq_dept",sequenceName="seq_dept_id",allocationSize=20,initialValue=1) public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } @Column(name="name") public String getName() { return this.name; } public void setName(String name) { this.name = name; } @Transient public String getLocation() { return this.location; } public void setLocation(String location) { this.location = location; } @OneToMany(mappedBy="dept",cascade=CascadeType.ALL) public Set<Emp> getEmps() { return emps; } public void setEmps(Set<Emp> emps) { this.emps = emps; } }
Emp.java
package com.entity; import java.util.Date; 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.ManyToOne; import javax.persistence.NamedQuery; import javax.persistence.SequenceGenerator; import javax.persistence.Table; import javax.persistence.Transient; @Entity @Table(name="emp") public class Emp implements java.io.Serializable { private Integer id; private Dept dept; private String name; private String job; private Double salary; private Date hiredate; // Constructors /** default constructor */ public Emp() { } /** full constructor */ public Emp(Dept dept, String name, String job, Double salary) { this.dept = dept; this.name = name; this.job = job; this.salary = salary; } @Id @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="seq_emp") @SequenceGenerator(name="seq_emp",sequenceName="seq_emp_eno",allocationSize=20,initialValue=1) public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name="dept_id") public Dept getDept() { return this.dept; } public void setDept(Dept dept) { this.dept = dept; } @Column(name="name") public String getName() { return this.name; } public void setName(String name) { this.name = name; } @Transient public String getJob() { return this.job; } public void setJob(String job) { this.job = job; } @Transient public Double getSalary() { return this.salary; } public void setSalary(Double salary) { this.salary = salary; } @Column(name="hiredate") public Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } }
hibernate.cfg.xml中的配置如下:
<?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.PointbaseDialect</property> <property name="connection.url"> jdbc:oracle:thin:@192.168.40.128:1521:orcl </property> <property name="connection.username">thunder</property> <property name="connection.password">admin</property> <property name="connection.driver_class"> oracle.jdbc.OracleDriver </property> <property name="myeclipse.connection.profile">thunder</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <mapping class="com.entity.Dept"/> <mapping class="com.entity.Emp"/> </session-factory> </hibernate-configuration>
View Code
需要注意<mapping>中class属性值的引用方式,这是和引用*.hbm.xml的不同之处。
测试:
public static void main(String[] args) { SessionFactory sf =null; Session session=null; Transaction tx=null; try { //新的获取sf的方式 sf=new AnnotationConfiguration().configure().buildSessionFactory(); //获得session session =sf.openSession(); //获取事务 tx=session.beginTransaction(); Emp emp=new Emp(); emp.setName("神经一枚"); emp.setHiredate(new Date()); session.save(emp); tx.commit(); System.out.println("add ok!"); //根据主键去查询信息,根据many-to-one加载部门信息 Emp emp=(Emp) session.load(Emp.class, new Integer(17)); System.out.println(emp.getName()+" "+emp.getDept().getName()); //通过添加部门的同时把员工添加过去 Emp e1 =new Emp(); e1.setName("思过"); e1.setHiredate(new Date()); Dept d=new Dept(); d.setName("市场部"); d.getEmps().add(e1); e1.setDept(d); session.save(d); tx.commit(); System.out.println("it is successful"); } catch (Exception e) { //tx.rollback(); e.printStackTrace(); }finally{ session.close(); sf.close(); } }
需要注意,获取Session工厂的方法与之前的不一样。
以上就是hibernate逐渐的简单使用。
相关文章推荐
- 编程基础知识之复数类型
- ios实现倒计时的两种方法
- 在Word简历中插入照片(图片)
- hdoj--1034--Hidden String(dfs)
- 有向图(4)--基于深度优先的拓扑排序
- [UVA 10441]Catenyms[欧拉路]
- 基于Wolf32F031 自由评估板的KEY scan(查询方式)实验硬件原理及软件API使用方法(适用于STM32F030/031)
- 深搜与广搜
- 解决RecyclerView中使用UIL加载网络图片,在刷新时出现闪烁问题
- CPP之中介者设计模式
- SpringMVC前端传值到Controller与Controller中传值到View解析
- 在U盘上安装ChromiumOS
- HDOJ 5311-Hidden String【深搜】
- dedecms标签
- js对象的创建和继承
- 基于Wolf32F031 自由评估板的LED Control实验硬件原理及软件API使用方法(适用于STM32F030/031)
- 带你走进ajax(4)
- 带你走进ajax(4)
- 瀑布流效果
- Windows JDK 1.8降级为1.7方法