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(); } }
时间: 2024-11-06 10:06:17