Hibernate联合主键的使用

本文通过一个实际的Java项目演示了如何利用Hibernate处理联合主键。首先,创建了一个包含所需Hibernate库的Java项目,接着展示了关键实体类`Student`及其对应的`Student.hbm.xml`映射文件。此外,还提到了数据库表结构,该结构包含两个主键字段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用一个Demo来说明,联合主键的使用

1.新建一个java project,将hibernate需要的jar包,添加进去。目录如下:

2.Student.java

package com.lx.domain;

import java.io.Serializable;

/**
 * 联合主键要实现implements
 * 重写hashCode和equals
 * @author Administrator
 *
 */
public class Student implements Serializable{

	private String cardId;
	
	private String name;
	
	private int age;

	public String getCardId() {
		return cardId;
	}

	public void setCardId(String cardId) {
		this.cardId = cardId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((cardId == null) ? 0 : cardId.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (cardId == null) {
			if (other.cardId != null)
				return false;
		} else if (!cardId.equals(other.cardId))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
}

Student.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>
     
     <class name="com.lx.domain.Student" table="combin_student">
       
       <composite-id>
          
          <key-property name="cardId" column="cardId" type="string"></key-property>
          
          <key-property name="name" column="name" type="string"></key-property>
          
       </composite-id>
       
       <property name="age" column="age" type="integer"></property>
       
     </class>

</hibernate-mapping>


CreateTable.java

package com.lx.test;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class CreateTable {

	public static void main(String[] args) {
		
		SchemaExport export = new SchemaExport(new Configuration().configure());
		export.create(true, true);
	}
}

HibernateTest.java

package com.lx.test;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.junit.Test;

import com.lx.domain.Student;
import com.lx.util.HibernateUtil;
/**
 *  联合主键的使用
 */
public class HibernateTest {

	
	@Test
	public void testAdd()
	{
		 Student student = new Student();
		 student.setName("lzyer");
		 student.setCardId("001");
		 student.setAge(20);
		SessionFactory factory= HibernateUtil.getSessionFactory();
		Session session = factory.openSession();
		Transaction tx = null;
		try
		{
		  tx = session.beginTransaction();
		 
		  session.save(student);
		  
		  tx.commit();
		}catch(Exception e)
		{
		   if(null != tx)
		   {
		       tx.rollback();	   
		   }
		}finally{
			session.close();
		}
	}
}


HibernateUtil.java

package com.lx.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class HibernateUtil {

	public static SessionFactory factory;
	static
	{
		factory= new Configuration().configure().buildSessionFactory();
	}
	public static SessionFactory getSessionFactory()
	{
		return factory;
	}
}

数据库表的结构:


可以看到有2个主键。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值