Hibernate之复合主键映射
程序员文章站
2022-04-24 22:37:37
...
一,概述
复合主键也是开发中经常遇到的需求,这篇博客就是关于复合主键映射,开始吧!
二,实体类准备
1)复合主键类
package com.bighuan.d_compositeKey;
import java.io.Serializable;
/**
* 复合主键类:用户名和地址唯一标识一条记录
*
* @author bighuan
*
*/
public class CompositeKeys implements Serializable {
private String userName;
private String address;
public CompositeKeys() {
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
2)表对应的实体类User.java
package com.bighuan.d_compositeKey;
public class User {
public User() {
}
private int age;
private CompositeKeys keys;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public CompositeKeys getKeys() {
return keys;
}
public void setKeys(CompositeKeys keys) {
this.keys = keys;
}
}
三,配置文件准备
1)User.java对应的映射文件User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.bighuan.d_compositeKey"
auto-import="true">
<class name="User">
<!-- 复合主键映射 -->
<composite-id name="keys">
<key-property name="userName" type="string"></key-property>
<key-property name="address" type="string"></key-property>
</composite-id>
<property name="age" type="int"></property>
</class>
</hibernate-mapping>
配置复合主键主要是通过<composite-id>来完成!
2)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>
<!--1, 数据库连接配置 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hib_demo</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">abc</property>
<!--2, 数据库方法配置:告诉hibernate使用的是什么数据库 -->
<!-- hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!--3, 其他相关配置:3.1展示hibernate在运行时执行的sql语句-->
<property name="hibernate.show_sql">true</property>
<!-- 3.2格式化sql语句配置 -->
<property name="hibernate.format_sql">true</property>
<!-- 3.3自动建表 ,先删除后创建,不管有没有表-->
<!-- <property name="hibernate.hbm2ddl.auto">create</property> -->
<!-- 3.4有表就不创建,反之就创建 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!--映射配置: 加载所有映射 -->
<mapping resource="com/bighuan/d_compositeKey/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
四,测试
1)插入数据,查询数据
package com.bighuan.d_compositeKey;
import java.util.Date;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class App2 {
private static SessionFactory sf = null;
static {
sf = new Configuration().configure().buildSessionFactory();
}
@Test
public void testSave() throws Exception {
// 创建对象
CompositeKeys keys = new CompositeKeys();
keys.setUserName("bighuan");
keys.setAddress("江西南昌2");
User user = new User();
user.setAge(25);
user.setKeys(keys);
// 创建session(代表一个与数据库连接的会话)
org.hibernate.Session session = sf.openSession();
// 4,开启事务
Transaction tx = session.beginTransaction();
// 5,保存数据
session.save(user);
tx.commit();
// 7,关闭
session.close();
}
@Test
public void testQuery() throws Exception {
// 创建对象
CompositeKeys keys = new CompositeKeys();
keys.setUserName("bighuan");
keys.setAddress("江西南昌");
// 创建session(代表一个与数据库连接的会话)
org.hibernate.Session session = sf.openSession();
// 4,开启事务
Transaction tx = session.beginTransaction();
// 5,查询数据
User user = (User) session.get(User.class, keys);
if (user != null) {
System.out.println(user.getAge() + ","
+ user.getKeys().getAddress() + ","
+ user.getKeys().getUserName());
}
tx.commit();
// 7,关闭
session.close();
}
}
2)插入两条数据到数据库,数据库的user表3)通过主键查询
25,江西南昌,bighuan