Hibernate映射联合主键

数据库建表语句为:

CREATE TABLE `T_PERSON` (
  `firstname` VARCHAR(10) NOT NULL,
  `lastname` VARCHAR(10) NOT NULL,
  `address` VARCHAR(20),
  `phone` int(10) unsigned,
  PRIMARY KEY (`firstname`,`lastname`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;

方式一:基于实体类属性

对于实体的PO类,有2个要求:

1,必须继承Serializable接口

2,必须重写equals()方法和hashcode()方法(个人认为不是必须重写,只要其默认hascode能唯一的确定任何一个Person类即可,当然使用联合主键字段firstname和lastname重写equals()方法和hashcode()方法后,即可通过firstname和lastname唯一确定一个Person类,更符合数据库中firstname和lastname做为联合主键的设计)

public class Person implements Serializable{
	private static final long serialVersionUID = 1L;
	private String firstname;
	private String lastname;
	private String address;
	private Integer phone;
	
	public Person(){}
	
	public String getFirstname() {
		return firstname;
	}
	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}
	public String getLastname() {
		return lastname;
	}
	public void setLastname(String lastname) {
		this.lastname = lastname;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public Integer getPhone() {
		return phone;
	}
	public void setPhone(Integer phone) {
		this.phone = phone;
	}
	
	public int hashCode() {
		return getFirstname().hashCode()*11 + 
				getLastname().hashCode();
	}
	
	public boolean equals(Object obj) {
		if(null == obj){
			return false;
		}
		if(this == obj){
			return true;
		}
		if(obj.getClass() == Person.class){
			Person p = (Person)obj;
			if(p.getFirstname().equals(getFirstname()) &&
					p.getLastname().equals(getLastname())){
				return true;
			}
		}
		return false;
	}
}

PO类配置文件Person.hbm.xml:

<?xml version="1.0"?>
<!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.po.Person" table="T_PERSON">
    	<composite-id>
    		<key-property name="firstname" column="firstname" type="string"/>
    		<key-property name="lastname" column="lastname" type="string"/>
    	</composite-id>
        <property name="address" column="address" type="string"/>
        <property name="phone" column="phone" type="integer"/>
    </class>
</hibernate-mapping>

Hibernate配置文件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="dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="connection.url">jdbc:mysql://localhost:3306/mydb</property>
		<property name="connection.username">root</property>
		<property name="connection.password">196428</property>
		
		<property name="hibernate.jdbc.batch_size">50</property>
		
		<mapping resource="com/po/Person.hbm.xml" />
	</session-factory>
</hibernate-configuration>

测试类:

public class Test {
	public static void main(String[] args) {
		Configuration conf = new Configuration();
		SessionFactory sessionFactory = conf.configure().buildSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction tr = session.beginTransaction();
		
		Person p1 = new Person();
		p1.setFirstname("sean");
		p1.setLastname("zou");
		p1.setAddress("address");
		p1.setPhone(123);
		session.save(p1);
		System.out.println("save successfully!");
		
		Person p2 = new Person();
		p2.setFirstname("sean");
		p2.setLastname("zou");
		p2 = (Person)session.get(Person.class,p2);
		p2.setFirstname("angel");
		p2.setLastname("baby");
		p2.setAddress("address2");
		p2.setPhone(123);
		session.merge(p2);
		System.out.println("merge successfully!");
			
		tr.commit();
		session.close();
	}
}

运行结果为:

save successfully!
merge successfully!

最终数据库里只有一条数据:

firstname

lastname

address

phone

angel

baby

address2

123

方式二:基于主键类

public class PersonPK implements Serializable{
	private static final long serialVersionUID = 1L;
	private String firstname;
	private String lastname;
	
	public String getFirstname() {
		return firstname;
	}
	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}
	public String getLastname() {
		return lastname;
	}
	public void setLastname(String lastname) {
		this.lastname = lastname;
	}
	
	public int hashCode() {
		return getFirstname().hashCode()*11 + 
				getLastname().hashCode();
	}
	
	public boolean equals(Object obj) {
		if(null == obj){
			return false;
		}
		if(this == obj){
			return true;
		}
		if(obj.getClass() == PersonPK.class){
			PersonPK pk = (PersonPK)obj;
			if(pk.getFirstname().equals(getFirstname()) &&
					pk.getLastname().equals(getLastname())){
				return true;
			}
		}
		return false;
	}
}
public class Person implements Serializable{
	private static final long serialVersionUID = 1L;
	private PersonPK personPK;
	private String address;
	private Integer phone;
	
	public Person(){}
	
	public PersonPK getPersonPK() {
		return personPK;
	}
	public void setPersonPK(PersonPK personPK) {
		this.personPK = personPK;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public Integer getPhone() {
		return phone;
	}
	public void setPhone(Integer phone) {
		this.phone = phone;
	}
}
<?xml version="1.0"?>
<!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.po.Person" table="T_PERSON">
    	<composite-id name="personPK" class="com.po.PersonPK">
    		<key-property name="firstname" column="firstname" type="string"/>
    		<key-property name="lastname" column="lastname" type="string"/>
    	</composite-id>
        <property name="address" column="address" type="string"/>
        <property name="phone" column="phone" type="integer"/>
    </class>
</hibernate-mapping>
public class Test {
	public static void main(String[] args) {
		Configuration conf = new Configuration();
		SessionFactory sessionFactory = conf.configure().buildSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction tr = session.beginTransaction();
		
		PersonPK pk = new PersonPK();
		pk.setFirstname("sean");
		pk.setLastname("zou");
		
		Person p1 = new Person();
		p1.setPersonPK(pk);
		p1.setAddress("address");
		p1.setPhone(123);
		session.save(p1);
		System.out.println("save successfully!");
		
		Person p2 = (Person)session.get(Person.class,pk);
		p2.setAddress("address2");
		p2.setPhone(123);
		session.merge(p2);
		System.out.println("merge successfully!");
			
		tr.commit();
		session.close();
	}
}

运行结果为:

save successfully!
merge successfully!

最终数据库里只有一条数据:

firstname

lastname

address

phone

sean

zou

address2

123

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hibernate映射配置文件用于定义 Java 实体类和数据库表之间的映射关系。通常情况下,映射文件的名称和对应的实体类名称相同,只是后缀名不同。比如,对于一个名为 `User` 的实体类,映射文件的名称可以为 `User.hbm.xml`。 下面是一个简单的 Hibernate 映射配置文件的示例: ```xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.example.User" table="users"> <id name="id" column="id"> <generator class="native"/> </id> <property name="name" column="name" /> <property name="age" column="age" /> <property name="email" column="email" /> </class> </hibernate-mapping> ``` - `hibernate-mapping`:根元素,用于指定文档类型和版本。 - `class`:表示一个 Java 实体类和数据库表之间的映射关系,它包含了实体类的名称和对应的数据库表的名称。 - `id`:表示实体类的主键,它包含了主键的名称和对应的数据库表中的列名,以及主键生成策略。 - `property`:表示一个实体类属性和对应的数据库表中的列之间的映射关系,它包含了属性的名称和对应的数据库表中的列名。 映射文件中每一个 `class` 元素对应一个 Java 实体类和数据库表之间的映射关系,其中可以包含 `id` 和多个 `property` 元素。根据需要,还可以使用其他元素来定义实体类和数据库表之间更复杂的映射关系。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值