hibernate之初学复合主键
Jar包什么的就看前面几篇文章,接下来介绍复合主键知识
首先是hibernate的配置文件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> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/webproject</property><!-- 反正下面这段话我直接跑去hibernate.dialect包下复制文件路径的时候gg了要指定特定的方言 --><property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <property name="hibernate.show_sql">true</property><!-- 如果不存在就自动创建一个,如果存在就修改 --><property name="hibernate.hbm2ddl.auto">update</property> <!-- 如果不加下面这句话,引入你自己写的配置文件,你将会很绝望 --><mapping resource="com/shaoxin/cn/User.hbm.xml"/> </session-factory> </hibernate-configuration>
然后是User表的配置文件用于映射User.hbm.xml这里要注意主键不再是id
<?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.shaoxin.cn"> <class name="User" table="user"><!-- 复合主键 --><composite-id name="compositionUser" > <key-property name="username" column="username"></key-property> <key-property name="address" column="address"></key-property> </composite-id> <!-- 下面的分别是实体类中的属性对应数据库中的字段 --><property name="age" column="age"></property> </class> </hibernate-mapping>
然后是User实体类,这里面要把符合主键单独作为一个类写出去,并作为User实体类的一个属性
package com.shaoxin.cn; public class User { private CompositionUser compositionUser; private int age; public CompositionUser getCompositionUser() { return compositionUser; } public void setCompositionUser(CompositionUser compositionUser) { this.compositionUser = compositionUser; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
符合主键类要注意了一定要实现Serializable这个接口
package com.shaoxin.cn; import java.io.Serializable; public class CompositionUser 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; } }
接下类就是测试类,没什么好说的
package com.shaoxin.cn; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.Test; public class UserTest { @Test public void testUser() throws Exception { Session session = new Configuration().configure().buildSessionFactory() .openSession(); Transaction tx = session.beginTransaction(); CompositionUser compositionUser = new CompositionUser(); compositionUser.setAddress("江西南昌"); compositionUser.setUsername("邵欣"); User user = new User(); user.setCompositionUser(compositionUser); user.setAge(23); session.save(user); tx.commit(); session.close(); } }