有关复合主键的id策略

在hibernate里,如何处理复合主键呢,下面我们就来学习一下:

首先建立一张表,

Code:
  1. create table cart(   
  2. userid  varchar2(50) not null,   
  3. productid varchar2(50) not null,   
  4. toatl number(15),not null,   
  5. primary key(userid ,productid )   
  6. )  

然后就可以建立一个POJO类了,

Code:
  1. package myclass.entity;   
  2.   
  3. import java.io.Serializable;   
  4.   
  5. public class Cart implements Serializable{   
  6.     private String userid;   
  7.     private String productid;   
  8.     private int total;   
  9.     public String getUserid() {   
  10.         return userid;   
  11.     }   
  12.     public void setUserid(String userid) {   
  13.         this.userid = userid;   
  14.     }   
  15.     public String getProductid() {   
  16.         return productid;   
  17.     }   
  18.     public void setProductid(String productid) {   
  19.         this.productid = productid;   
  20.     }   
  21.     public int getTotal() {   
  22.         return total;   
  23.     }   
  24.     public void setTotal(int total) {   
  25.         this.total = total;   
  26.     }   
  27.     public Cart(){   
  28.            
  29.     }   
  30.                
  31. }   

要注意的是,该类必须得实现Serializable接口,这是hibernate的需要吧。然后可以建立映射文件,cart.hbm.xml

Code:
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <!DOCTYPE hibernate-mapping PUBLIC    
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">   
  5.     <hibernate-mapping package="myclass.entity">   
  6.      <class name="Cart" table="cart">   
  7.     <!-- 配置对角标识符和表的主键 -->   
  8.     <!-- column表示与数据库对应的字段 -->   
  9.     <composite-id>   
  10.     <key-property name="userid" column="userid" length="20"></key-property>   
  11.     <key-property name="productid" column="productid" length="20"></key-property>   
  12.     </composite-id>   
  13.     <!--配置其他的属性与字段 -->   
  14.     <property name="total" column="total" length="20"></property>   
  15.     </class>   
  16.     </hibernate-mapping>  

然后写hibernate.cfg.xml配置文件,如下:

Code:
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <!DOCTYPE hibernate-configuration PUBLIC   
  3.     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">   
  5. <hibernate-configuration>   
  6.     <session-factory>   
  7.     <property name="hibernate.connection.username">scott</property>   
  8.     <property name="hibernate.connection.password">liping</property>   
  9.     <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>   
  10.     <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcle</property>   
  11.     <!-- 数据库方言 -->   
  12.     <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>   
  13.     <property name="show_sql">true</property>   
  14.     <mapping resource="myclass/entity/Student.hbm.xml"/>   
  15.     </session-factory>   
  16. </hibernate-configuration>  

到这步,我们就可以写测试类了,如下:

Code:
  1. package myclass.test;   
  2. import myclass.entity.Cart;   
  3. import org.hibernate.Session;   
  4. import org.hibernate.SessionFactory;   
  5. import org.hibernate.cfg.Configuration;   
  6.   
  7. public class CartTest {   
  8. public static Session session=null;   
  9. public static void CartTest(){   
  10.     Configuration conf=new Configuration();   
  11.     conf.configure();   
  12.     SessionFactory sf=conf.buildSessionFactory();   
  13.     session=sf.openSession();      
  14. }   
  15.     /**  
  16.      * @测试Cart  
  17.      */  
  18.     public static void main(String[] args) {   
  19.         CartTest ct=new CartTest();   
  20.         ct.insert();   
  21.     }   
  22.     public void insert(){   
  23.         Cart cart=new Cart();   
  24.         cart.setProductid("p_00002");   
  25.         cart.setUserid("u_00002");   
  26.         cart.setTotal(1000);   
  27.         session.save(cart);   
  28.         session.beginTransaction().commit();   
  29.         session.close();   
  30.     }   
  31. }   

这样复合主键的插入操作就完成了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值