- 浏览: 511155 次
- 性别:
- 来自: 宁波
文章分类
- 全部博客 (137)
- JQuery component (3)
- Web编程控件 (7)
- java (19)
- database (9)
- Web Service (6)
- XML (2)
- FTP (2)
- SSH2 (18)
- javascript (3)
- arcgis (7)
- geoserver (1)
- Openscales (2)
- FME (6)
- HttpClient (3)
- portal (6)
- oracle goldengate (7)
- oracle (3)
- 命令锦集 (1)
- web容器 (3)
- NB博客链接 (2)
- 设计模式 (0)
- ext (2)
- 小工具 (12)
- Flex (1)
- LINUX (1)
- Android (1)
- Maven 入门 (2)
- 负载均衡 (1)
- openlayers (2)
- Flexkkjk (0)
最新评论
-
lgh1992314:
ApplicationContext context = We ...
Spring中的ContextLoaderListener使用 -
lgh1992314:
ContextLoaderServletSpring3.0后删 ...
Spring中的ContextLoaderListener使用 -
gaoqiangjava:
...
Spring中的ContextLoaderListener使用 -
liude33:
好资料啊,网上这方面的资料好少。
goldengate 学习资料 -
AKka:
在实体类中加上@Transient 注解后,为什么还会在数据库 ...
JPA注解 转
hibernate: 3.6
数据库 oracle
CUSTOMER表
-- Create table
create table CUSTOMER
(
ID NUMBER not null,
NAME VARCHAR2(20)
)
tablespace FM
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64
minextents 1
maxextents unlimited
);
-- Create/Recreate primary, unique and foreign key constraints
alter table CUSTOMER
add constraint COUSTOMER_PKID primary key (ID)
disable;
ORDERS表
-- Create table
create table ORDERS
(
ID NUMBER not null,
ORDERNUMBER VARCHAR2(20),
CUSTOMERID NUMBER not null
)
tablespace FM
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64
minextents 1
maxextents unlimited
);
-- Create/Recreate primary, unique and foreign key constraints
alter table ORDERS
add constraint ORDER_PKID primary key (ID)
disable;
alter table ORDERS
add constraint CUSTOMER_FK foreign key (CUSTOMERID)
references CUSTOMER (ID) on delete cascade
disable;
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="foo">
<property name="show_sql">true</property>
<property name="myeclipse.connection.profile">
oraclejdbc
</property>
<property name="connection.url">
jdbc:oracle:thin:@10.8.205.70:1521:orcl
</property>
<property name="connection.username">sspm</property>
<property name="connection.password">sspm</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<!-- 不使用JPA
<mapping resource="com/sspm/hibernate/test/Customer.hbm.xml" />
<mapping resource="com/sspm/hibernate/test/Order.hbm.xml" />
-->
<mapping class="com.hibernate.annotation.test.Customer" />
<mapping class="com.hibernate.annotation.test.Order" />
</session-factory>
</hibernate-configuration>
利用Hibernate的逆向工程生成:
Customer.java
package com.hibernate.annotation.test;
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;
/**
* Customer entity.
*
* @author MyEclipse Persistence Tools
*/
@Entity
@Table(name="Customer",catalog="sspm")
public class Customer implements java.io.Serializable {
// Fields
/**
*
*/
private static final long serialVersionUID = 6947291600466684677L;
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="SQ_CUSTOMER")//通过Sequence来实现表主键自增,这种方式依赖于数据库是否有SEQUENCE,如果没有就不能用
//SequenceGenerator 的名称必须匹配其 startegy 设置为 SEQUENCE 的 GeneratedValue 的名称
//如果oracle程序没有按照hibernater设置的sequence自增长。可以在@SequenceGenerator中加入allocationSize = 1,默认情况下,JPA 持续性提供程序使用的分配大小为 50
@SequenceGenerator(name="SQ_CUSTOMER",sequenceName="SQ_CUSTOMER_ID",allocationSize = 1)
@Column(name = "id", nullable = false)
private Long id;
@Column(name = "NAME")
private String name;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "customer")//指向多的那方的pojo的关联外键字段对应的对象属性名称
private Set<Order> orderses = new HashSet(0);
// Constructors
/** default constructor */
public Customer() {
}
/** full constructor */
public Customer(String name, Set orderses) {
this.name = name;
this.orderses = orderses;
}
// Property accessors
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Set getOrderses() {
return this.orderses;
}
public void setOrderses(Set orderses) {
this.orderses = orderses;
}
}
Order.java
package com.hibernate.annotation.test; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.SequenceGenerator; import javax.persistence.Table; /** * Orders entity. * * @author MyEclipse Persistence Tools */ @Entity @Table(name = "ORDERS") public class Order implements java.io.Serializable { /** * */ private static final long serialVersionUID = 6146682583191452711L; // Fields @Id @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="SQ_ORDER")//通过Sequence来实现表主键自增,这种方式依赖于数据库是否有SEQUENCE,如果没有就不能用 @SequenceGenerator(name="SQ_ORDER",sequenceName="SQ_ORDER_ID",allocationSize = 1) @Column(name = "id", nullable = false) private Long id; @JoinColumn(name = "CUSTOMERID", referencedColumnName = "id")//设置对应数据表的列名和引用的数据表的列名 @ManyToOne//设置在“一方”pojo的外键字段上 private Customer customer; @Column(name = "ORDERNUMBER") private String ordernumber; // Constructors /** default constructor */ public Order() { } /** minimal constructor */ public Order(Customer customer) { this.customer = customer; } /** full constructor */ public Order(Customer customer, String ordernumber) { this.customer = customer; this.ordernumber = ordernumber; } // Property accessors public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public Customer getCustomer() { return this.customer; } public void setCustomer(Customer customer) { this.customer = customer; } public String getOrdernumber() { return this.ordernumber; } public void setOrdernumber(String ordernumber) { this.ordernumber = ordernumber; } }
CustomerAction.java
package com.hibernate.annotation.test; import java.util.Iterator; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; public class CustomerAction { private Customer customer; private List<Customer> listCustomer; public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } public List<Customer> getListCustomer() { return listCustomer; } public void setListCustomer(List<Customer> listCustomer) { this.listCustomer = listCustomer; } /** * 添加客户 * */ public void addCustomer(Customer customer) { Session s = null; Transaction tx = null; try { s = HibernateUtil.getSession(); tx = s.beginTransaction(); s.save(customer); tx.commit(); } catch (Exception e) { if (tx != null) { tx.rollback(); } e.printStackTrace(); } finally { if (s != null) { s.close(); } } } /** * 删除客户 * */ public void deleteCustomer(Customer customer) { Session s = null; Transaction tx = null; try { s = HibernateUtil.getSession(); tx = s.beginTransaction(); s.delete(customer); tx.commit(); } catch (Exception e) { if (tx != null) { tx.rollback(); } e.printStackTrace(); } finally { if (s != null) { s.close(); } } } /** * 更新客户 * */ public void update(Customer customer, String name) { Session s = null; Transaction tx = null; try { s = HibernateUtil.getSession(); tx = s.beginTransaction(); customer.setName(name); s.update(customer); tx.commit(); } catch (Exception e) { if (tx != null) { tx.rollback(); } e.printStackTrace(); } finally { if (s != null) { s.close(); } } } /** * 查询客户 * */ public Customer findCustomer(Long id) { Session s = null; Transaction tx = null; try { s = HibernateUtil.getSession(); tx = s.beginTransaction(); customer = (Customer) s.get(Customer.class, id); tx.commit(); } catch (Exception e) { if (tx != null) { tx.rollback(); } e.printStackTrace(); } finally { if (s != null) { s.close(); } } return customer; } /** * 查找所有的客户 * */ public List<Customer> findAll() { Session s = null; Transaction tx = null; try { s = HibernateUtil.getSession(); tx = s.beginTransaction(); Query query = s .createQuery("from Customer as a order by id asc"); listCustomer = query.list(); for (Iterator iter = listCustomer.iterator(); iter.hasNext();) { Customer customer = (Customer) iter.next(); System.out.println("客户ID是:" + customer.getId() + "客户姓名是:" + customer.getName()); } tx.commit(); } catch (Exception e) { if (tx != null) { tx.rollback(); } e.printStackTrace(); } finally { if (s != null) { s.close(); } } return listCustomer; } }
OrderAction.java
package com.hibernate.annotation.test; import java.util.Iterator; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; public class OrderAction { private Order order; private List<Order> listorder; public Order getorder() { return order; } public void setorder(Order order) { this.order = order; } public List<Order> getListorder() { return listorder; } public void setListorder(List<Order> listorder) { this.listorder = listorder; } public void addorder(Order order) { Session s = null; Transaction tx = null; try { s = HibernateUtil.getSession(); tx = s.beginTransaction(); s.save(order); tx.commit(); } catch (Exception e) { if (tx != null) { tx.rollback(); } e.printStackTrace(); } finally { if (s != null) { s.close(); } } } /** * 删除用户 * */ public void deleteorder(Order order) { Session s = null; Transaction tx = null; try { s = HibernateUtil.getSession(); tx = s.beginTransaction(); s.delete(order); tx.commit(); } catch (Exception e) { if (tx != null) { tx.rollback(); } e.printStackTrace(); } finally { if (s != null) { s.close(); } } } public void update(Order order, String number) { Session s = null; Transaction tx = null; try { s = HibernateUtil.getSession(); tx = s.beginTransaction(); order.setOrdernumber(number); s.update(order); tx.commit(); } catch (Exception e) { if (tx != null) { tx.rollback(); } e.printStackTrace(); } finally { if (s != null) { s.close(); } } } public Order findorder(Long id) { Session s = null; Transaction tx = null; try { s = HibernateUtil.getSession(); tx = s.beginTransaction(); order = (Order) s.get(Order.class, id); tx.commit(); } catch (Exception e) { if (tx != null) { tx.rollback(); } e.printStackTrace(); } finally { if (s != null) { s.close(); } } return order; } public List<Order> findAll() { Session s = null; Transaction tx = null; try { s = HibernateUtil.getSession(); tx = s.beginTransaction(); Query query = s.createQuery("from Order as a order by id asc"); listorder = query.list(); for (Iterator iter = listorder.iterator(); iter.hasNext();) { Order order = (Order) iter.next(); System.out.println("订单ID是:" + order.getId() + "订单数目是:" + order.getOrdernumber()); } tx.commit(); } catch (Exception e) { if (tx != null) { tx.rollback(); } e.printStackTrace(); } finally { if (s != null) { s.close(); } } return listorder; } }
测试test.java
package com.hibernate.annotation.test; public class Test { public static void main(String args[]) { Customer customer = new Customer(); customer.setName("baidu6"); CustomerAction ca = new CustomerAction(); /** * * 添加对象 * */ ca.addCustomer(customer); OrderAction oa = new OrderAction(); Order order = new Order(); order.setOrdernumber("6zz"); order.setCustomer(customer); oa.addorder(order); } }
发表评论
-
spring 线程池
2013-10-24 07:30 0spring 配置文件 <!-- app ... -
在javaee项目里集成了hibernate,在使用注解映射表时,老是出现的错误
2012-02-06 14:11 2764在javaee项目里集成了hibernate,在使用注解映射表 ... -
nested exception is org.hibernate.AnnotationException: mappedBy reference an unk
2012-02-06 14:10 4929现有部门与用户两个表 一对多关系 在进行hibern ... -
JPA注解 转
2012-01-17 09:53 61691、@Entity(name="EntityName ... -
hibernate反向映射 no entries available
2012-01-16 15:07 3987做项目时,有时需要用hibernate反向映射表,很多时候会出 ... -
hibernate 3配置
2012-01-16 13:29 982hibernate 搭建 1、所需jar包antlr-2.7 ... -
Struts2+JSON+jQuery实现异步交互数据时选择要序列化的属性(二使用XML配置方式)
2012-01-13 09:46 1246只需在XML配置就可以了,配置方式是: Xml代码 ... -
Struts2+JSON+jQuery实现异步交互数据时选择要序列化的属性(一注解方式转)
2012-01-13 09:45 1595在使用Struts2的JSON插件,实现Action中的属性序 ... -
Struts2练习--基于注解方式Action配置
2012-01-11 16:59 18033还是已登录来说明下这个Action的配置,这里要说的Actio ... -
Struts2练习-最基本的示例 —转
2012-01-11 16:52 1281转-(查看更多内容请点击http://javacrazyer. ... -
struts2.3.1 注解 HTTP Status 404 - There is no Action mapped for namespace
2012-01-11 16:49 3605使用struts2.3.1 注解配置时 自己将配置文件stru ... -
Spring JDBC模板类—org.springframework.jdbc.core.JdbcTemplate
2011-12-01 14:44 1925转:http://just-do-myself.iteye.c ... -
DWR + Spring 配置问题 No class by name
2011-11-23 16:28 4017[framework] 2011-07-26 16:51:0 ... -
DWR Spring 注解 整合
2011-09-22 23:18 14081、添加相应jar包 2、配置web.xml <?x ... -
项目无法使用Hibernate Reverse Engineering ,解决办法
2011-04-13 18:22 1775手工自己编写hibernater的配置文件。可惜当采用Hibe ... -
用AnnotationSessionFactoryBean生成sessionFactory时报错不能创建sessionFactoyBean(转)
2011-02-16 11:04 4062报错信息: [junit] Testcase: testCr ... -
Spring中的ContextLoaderListener使用
2011-01-18 10:30 40183最近 项目中使用Spring 时,applicationCon ... -
Struts2.1.6+Spring2.5.6+Hibernate3.3.1全注解实例详解(一)(转载)
2010-12-16 13:40 1149在JavaEE企业级开发中,以SSH2框架为核心的应用 ...
相关推荐
学习JPA——Hibernate_Annotation使用实例学习JPA——Hibernate_Annotation使用实例学习JPA——Hibernate_Annotation使用实例
Hibernate Annotation 笔记 总结 注解 注解事务,声明事务,详解。
hibernate annotation hibernate3
现在我们公司在做web项目的时候,已经不用*.hbm.xml这种映射文件了,都是用Annotation(注解)方式来完成实体与表之间的映射关系,这样看起来比用xml文件来映射更具有可读性,自我感觉以后Hibernate Annotation的...
hibernate annotation中文文档
使用学生(student)、老师(teacher)、班级(class)反映出Hibernate annotation JPA 的一对多、多对多、多对一的关联。或许对您有用
hibernate annotation 中文文档
hibernate 注解 annotation 教程
很全的Hibernate Annotation API文档,页面也很漂亮,太强大了。真的很实用!我以前找了好多,都不全,这个真的很不错,推荐一下。
hibernate annotation 注解编程 官网文档 hibernate annotation 注解编程 官网文档 hibernate annotation 注解编程 官网文档
HibernateAnnotation注解 api手册
简介: 在过去几年里,Hibernate不断发展,...Hibernate annotation使用了ejb JPA的注解,所以,下面安装配置hibernate annotation环境时,需要导入ejb的包。许多网上的资料都是jpa hibernate annotation方面的资料。
hibernate annotation库所需要引入的三个jar包。
通过HibernateAnnotation技术就不用xml文件了
Hibernate Annotation
逐渐的Hibernate Annotation的映射方式也越来越盛行,相信过不了多久有可能会取代*.hbm.xml映射方式
Hibernate annotation 3.4 api CHM
hibernate annotation reference CHM hibernate jpa
现在已经不用*.hbm.xml这种映射文件了,都是用Annotation(注解)方式来完成实体与表之间的映射关系,这样看起来比用xml文件来映射更具有可读性,自我感觉以后Hibernate Annotation的映射方式将代替hibernate 的*....
myeclipse的自带hibernate jar包不支持注解;自己找的hibernate注解所需的jar包:hibernate-core;hibernate-annotation;hbm-cfg-xml;log4j.properties