在hibernate里,如何处理复合主键呢,下面我们就来学习一下:
首先建立一张表,
- create table cart(
- userid varchar2(50) not null,
- productid varchar2(50) not null,
- toatl number(15),not null,
- primary key(userid ,productid )
- )
然后就可以建立一个POJO类了,
- package myclass.entity;
- import java.io.Serializable;
- public class Cart implements Serializable{
- private String userid;
- private String productid;
- private int total;
- public String getUserid() {
- return userid;
- }
- public void setUserid(String userid) {
- this.userid = userid;
- }
- public String getProductid() {
- return productid;
- }
- public void setProductid(String productid) {
- this.productid = productid;
- }
- public int getTotal() {
- return total;
- }
- public void setTotal(int total) {
- this.total = total;
- }
- public Cart(){
- }
- }
要注意的是,该类必须得实现Serializable接口,这是hibernate的需要吧。然后可以建立映射文件,cart.hbm.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="myclass.entity">
- <class name="Cart" table="cart">
- <!-- 配置对角标识符和表的主键 -->
- <!-- column表示与数据库对应的字段 -->
- <composite-id>
- <key-property name="userid" column="userid" length="20"></key-property>
- <key-property name="productid" column="productid" length="20"></key-property>
- </composite-id>
- <!--配置其他的属性与字段 -->
- <property name="total" column="total" length="20"></property>
- </class>
- </hibernate-mapping>
然后写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">
- <hibernate-configuration>
- <session-factory>
- <property name="hibernate.connection.username">scott</property>
- <property name="hibernate.connection.password">liping</property>
- <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
- <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcle</property>
- <!-- 数据库方言 -->
- <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
- <property name="show_sql">true</property>
- <mapping resource="myclass/entity/Student.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
到这步,我们就可以写测试类了,如下:
- package myclass.test;
- import myclass.entity.Cart;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- public class CartTest {
- public static Session session=null;
- public static void CartTest(){
- Configuration conf=new Configuration();
- conf.configure();
- SessionFactory sf=conf.buildSessionFactory();
- session=sf.openSession();
- }
- /**
- * @测试Cart
- */
- public static void main(String[] args) {
- CartTest ct=new CartTest();
- ct.insert();
- }
- public void insert(){
- Cart cart=new Cart();
- cart.setProductid("p_00002");
- cart.setUserid("u_00002");
- cart.setTotal(1000);
- session.save(cart);
- session.beginTransaction().commit();
- session.close();
- }
- }
这样复合主键的插入操作就完成了。