Hibernate-复合主键案例
来源:互联网 发布:php二进制转换字符串 编辑:程序博客网 时间:2024/06/06 00:15
1.设计建立后的User表(下图为hibernate自动创建的表)
userName和address作为联合主键
2.写复合主键类
package com.cx.compositeKey;import java.io.Serializable;/** * Created by cxspace on 16-7-24. 复合主键类 */public class CompositeKeys implements Serializable{ private String userName; private String address; 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; }}
3.写User类
package com.cx.compositeKey;/** * Created by cxspace on 16-7-24. */public class User { //名字跟地址,不能重复 private CompositeKeys keys; private int age; public CompositeKeys getKeys() { return keys; } public void setKeys(CompositeKeys keys) { this.keys = keys; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User{" + "age=" + age + ", keys=" + keys + '}'; }}
4.配置主键映射
<?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.cx.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>
5.在总配置文件中加载映射配置和自动建表配置
<!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:///learnstruts</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">33269456.cx</property> <!-- 数据库方言配置,hibernate会根据不同的方言生成符合当前数据库语法的sql --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <!--2.其他相关配置-->
<!--2.1显示运行时的sql语句-->
<property name="hibernate.show_sql">true</property> <!--2.2格式化sql-->
<property name="hibernate.format_sql">true</property> <!--2.3自动建表,写好映射就可以--> <property name="hibernate.hbm2ddl.auto">update</property> <!--3.加载所有映射--> <mapping resource="com/cx/compositeKey/User.hbm.xml"></mapping> </session-factory></hibernate-configuration>
6.编写测试类测试
package com.cx.compositeKey;import com.cx.utils.HibernateUtils;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;/** * Created by cxspace on 16-7-24. */public class Test { public void testSave() throws Exception{ Session session = HibernateUtils.getSession(); Transaction tx = session.beginTransaction(); CompositeKeys keys = new CompositeKeys(); keys.setAddress("aa"); keys.setUserName("bb"); User user = new User(); user.setAge(20); user.setKeys(keys); session.save(user); tx.commit(); session.close(); } @org.junit.Test public void testGet() throws Exception{ Session session = HibernateUtils.getSession(); Transaction tx = session.beginTransaction(); //构建联合主键 CompositeKeys keys = new CompositeKeys(); keys.setAddress("aa"); keys.setUserName("bb"); //查询 User user = (User) session.get(User.class , keys); if (user != null){ System.out.println(user.getAge()); } tx.commit(); session.close(); }}
0 0
- Hibernate-复合主键案例
- JPA复合主键案例
- Hibernate复合主键
- hibernate 复合主键
- hibernate复合主键
- Hibernate复合主键例子
- Hibernate复合主键
- Hibernate复合主键
- Hibernate 复合主键
- hibernate中复合主键
- hibernate复合主键映射
- Hibernate复合主键映射
- Hibernate 使用复合主键
- Hibernate复合主键映射
- hibernate复合主键
- hibernate复合主键代码
- hibernate配置复合主键
- Hibernate复合主键映射
- container_of分析
- 解构C#游戏框架uFrame兼谈游戏架构设计
- Hibernate学习-4-各种查询方式
- Hibernate-简单的crud案例
- Hibernate学习-5-配置文件详解
- Hibernate-复合主键案例
- Hibernate学习-6-集合映射保存和集合数据获取案例
- Hibernate学习-7-关联映射(一对多,多对一)
- Hibernate学习-7-关系映射(多对多)
- JAVA之旅(三十四)——自定义服务端,URLConnection,正则表达式特点,匹配,切割,替换,获取,网页爬虫
- Hibernate学习-8-缓存,懒加载
- Hibernate学习-9-一对一映射
- Hibernate学习-10-组件映射和继承映射
- Hibernate学习-11-几种查询详解