首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Hibernate Annotation (Hibernate JPA注脚) 实例

2012-08-30 
Hibernate Annotation (Hibernate JPA注解) 实例hibernate: 3.6?数据库 oracle?CUSTOMER表-- Create table

Hibernate Annotation (Hibernate 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 />
? ?? <mapping />
?</session-factory>
</hibernate-configuration>

?

?

利用Hibernate的逆向工程生成:

?

?Customer.java

?

?

OrderAction.java

?

测试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);}}

?

?

热点排行